1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
   | #include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " << x << endl
  using namespace std; typedef long long LL; struct Dinic {       static const int maxn = 3000 + 16;     static const int INF = 0x7F7F7F7F;     struct Edge {         int from, to;         LL cap, flow;         Edge(int from, int to, LL cap, LL flow):             from(from), to(to), cap(cap), flow(flow) {}         Edge() {}     };     vector<Edge> edges;     vector<int> G[maxn];     bool vis[maxn];     int d[maxn], cur[maxn];
      int n, m, s, t; 
      void init(int n = maxn - 1) {         this->n = n;         for(int i = 0; i <= n; ++i) G[i].clear();         edges.clear();     }
      void add_edge(int from, int to, LL cap) {         edges.push_back(Edge(from, to, cap, 0));         edges.push_back(Edge(to, from, 0, 0));         m = edges.size();         G[from].push_back(m - 2);         G[to].push_back(m - 1);     }
      bool BFS() {         memset(vis, false, sizeof(vis));         queue<int> Q;         Q.push(s);         d[s] = 0;         vis[s] = true;         while(!Q.empty()) {             int x = Q.front(); Q.pop();             for(int i = 0; i < G[x].size(); ++i) {                 Edge &e = edges[G[x][i]];                 if(!vis[e.to] && e.cap > e.flow) {                     vis[e.to] = true;                     d[e.to] = d[x] + 1;                     Q.push(e.to);                 }             }         }         return vis[t];     }
      LL DFS(int x, LL a) {         if(x == t || a == 0) return a;         LL flow = 0, f;         for(int &i = cur[x]; i < G[x].size(); ++i) {             Edge &e = edges[G[x][i]];             if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {                 e.flow += f;                 edges[G[x][i] ^ 1].flow -= f;                 flow += f;                 a -= f;                 if(a == 0) break;             }         }         return flow;     }     LL MaxFlow(int s, int t) {         this->s = s; this->t = t;         LL flow = 0;         while(BFS()) {             memset(cur, 0, sizeof(cur));             flow += DFS(s, INF);         }         return flow;     } } Dinic;
  const LL BIG = 10000000LL;
  int main(int argc, char **argv) {     int T; scanf("%d", &T);     while(T--) {         int n, m; scanf("%d%d", &n, &m);         int s, t; scanf("%d%d", &s, &t);         Dinic.init(n);         while(m--) {             int u, v, c; scanf("%d%d%d", &u, &v, &c);             Dinic.add_edge(u, v, c * BIG + 1);         }         printf("%lld\n", Dinic.MaxFlow(s, t) % BIG);     }     return 0; }
   |