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_A2.test.cpp

Depends on

Code

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

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

#include "../../../graph/dinic.hpp"

int main(){
  int n,m;
  cin >> n >> m;
  dinic<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_A2.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_A2.test.cpp"

#line 2 "graph/template.hpp"

/**
 * @brief Graph Template
*/
template <class T>
struct Edge {
  int from,to;
  T cost;
  int idx;
  Edge(){};
  Edge(int f, int t, T c=1, int i=-1) : from(f), to(t), cost(c), idx(i){}
  Edge(int t) : to(t), from(-1), cost(1), idx(-1){}
  operator int() const{ return to; }
  bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
  Graph(){}
  Graph(const int &n) : vector<vector<Edge<T>>>(n){}
  void add_edge(int a, int b, T c=1, int i=-1){
    (*this)[a].push_back({ a, b, c, i });
  }
};
using graph = Graph<int>;
#line 2 "graph/dinic.hpp"

template<class T>
struct dinic {
  const T INF;
  struct edge {
    int to,rev;
    T cap;
    bool isrev;
    int idx;
  };

  int n;
  vector<vector<edge>> graph;
  vector<int> min_cost, iter;

  dinic(int n): INF(numeric_limits<T>::max()), graph(n), n(n){}

  void add_edge(int from, int to, T cap, int idx = -1){
    graph[from].emplace_back((edge){to, (int)graph[to].size(), cap, false, idx});
    graph[to].emplace_back((edge){from, (int)graph[from].size() - 1, 0, true, idx});
  }
  T max_flow(int s, int t){
    T flow = 0;
    while(bfs(s, t)){
      iter.assign(n, 0);
      T f = 0;
      while((f = dfs(s, t, INF)) > 0) flow += f;
    }
    return flow;
  }
  private:
  bool bfs(int s, int t){
    min_cost.assign(n, -1);
    queue<int> que;
    min_cost[s] = 0;
    que.push(s);
    while(!que.empty() && min_cost[t] == -1){
      int p = que.front();
      que.pop();
      for(auto &e : graph[p]){
        if(e.cap > 0 && min_cost[e.to] == -1){
          min_cost[e.to] = min_cost[p] + 1;
          que.push(e.to);
        }
      }
    }
    return min_cost[t] != -1;
  }
  T dfs(int idx, const int t, T flow){
    if(idx == t) return flow;
    for(int &i = iter[idx]; i < graph[idx].size(); i++){
      edge &e = graph[idx][i];
      if(e.cap > 0 && min_cost[idx] < min_cost[e.to]){
        T d = dfs(e.to, t, min(flow, e.cap));
        if(d > 0){
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }
  public:
  void debug(){
    for(int i = 0; i < n; i++){
      for(auto &e : graph[i]){
        if(e.isrev) continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
      }
    }
  }
};
#line 6 "test/aoj/GRL/GRL_6_A2.test.cpp"

int main(){
  int n,m;
  cin >> n >> m;
  dinic<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