Algorithm-Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub UScuber/Algorithm-Library

:heavy_check_mark: test/aoj/GRL/GRL_6_A.test.cpp

Depends on

Code

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/GRL_6_A"

#include "../../../template/template.hpp"

#include "../../../graph/fordfulkerson.hpp"

int main(){
  int n,m;
  cin >> n >> m;
  maxflow<int> g(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    g.add_edge(a, b, c);
  }
  cout << g.max_flow(0, n-1) << "\n";
}
#line 1 "test/aoj/GRL/GRL_6_A.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/GRL_6_A"

#line 1 "template/template.hpp"
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#include <cstring>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr int mod = 1000000007;
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
#line 4 "test/aoj/GRL/GRL_6_A.test.cpp"

#line 1 "graph/fordfulkerson.hpp"
template <class T>
struct maxflow {
  struct edge {
    int to,rev; T cap;
  };
  maxflow(int n) : n(n), G(n), used(n){}
  maxflow(const vector<vector<edge>> &g): n(g.size()), G(g){}
  void add_edge(int from, int to, T cap){
    G[from].push_back({to, (int)G[to].size(), cap});
    G[to].push_back({from, (int)G[from].size()-1, 0});
  }
  T max_flow(int s, int t){
    T flow = 0;
    while(true){
      used.assign(n, false);
      T f = dfs(s, t, mod);
      if(f == 0) return flow;
      flow += f;
    }
  }
  private:
  int n;
  vector<vector<edge>> G;
  vector<bool> used;

  T dfs(int v, int t, T f){
    if(v == t) return f;
    used[v] = true;
    for(edge &e : G[v]){
      if(!used[e.to] && e.cap > 0){
        T d = dfs(e.to, t, min(f, e.cap));
        if(d > 0){
          e.cap -= d;
          G[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }
};
#line 6 "test/aoj/GRL/GRL_6_A.test.cpp"

int main(){
  int n,m;
  cin >> n >> m;
  maxflow<int> g(n);
  rep(i, m){
    int a,b,c;
    cin >> a >> b >> c;
    g.add_edge(a, b, c);
  }
  cout << g.max_flow(0, n-1) << "\n";
}
Back to top page