【HDOJ】3549 Flow Problem

网络流基础题目,Edmonds_Karp可解。

  1 /* 3549 */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <set>
  7 #include <stack>
  8 #include <vector>
  9 #include <deque>
 10 #include <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 using namespace std;
 20 
 21 #define rep(i, a, n)     for (int i=a;i<n;++i)
 22 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 23 #define pb                 push_back
 24 #define mp                 make_pair
 25 #define all(x)             (x).begin(),(x).end()
 26 #define SZ(x)             ((int)(x).size())
 27 #define lson            l, mid, rt<<1
 28 #define rson            mid+1, r, rt<<1|1
 29 
 30 const int maxn = 20;
 31 int F[maxn][maxn];
 32 int P[maxn], a[maxn];
 33 int n, m, ans;
 34 int s, t;
 35 
 36 bool bfs() {
 37     int u, v;
 38     queue<int> Q;
 39     
 40     memset(a, 0, sizeof(a));
 41     a[s] = INT_MAX;
 42     Q.push(s);
 43     P[s] = s;
 44     
 45     while (!Q.empty()) {
 46         u = Q.front();
 47         Q.pop();
 48         for (v=1; v<=n; ++v) {
 49             if (!a[v] && F[u][v]) {
 50                 P[v] = u;
 51                 Q.push(v);
 52                 a[v] = min(a[u], F[u][v]);
 53             }
 54         }
 55     }
 56     
 57     return a[t] == 0;
 58 }
 59 
 60 void Edmonds_Karp() {
 61     int u, v;
 62     
 63     s = 1;
 64     t = n;
 65     ans = 0;
 66     
 67     while (true) {
 68         if (bfs())
 69             break;
 70         for (u=t; u!=s; u=P[u]) {
 71             F[u][P[u]] += a[t];
 72             F[P[u]][u] -= a[t];
 73         }
 74         ans += a[t];
 75     }
 76 }
 77 
 78 int main() {
 79     int i, j, k;
 80     int t, tt;
 81     
 82     #ifndef ONLINE_JUDGE
 83         freopen("data.in", "r", stdin);
 84         freopen("data.out", "w", stdout);
 85     #endif
 86     
 87     scanf("%d", &tt);
 88     for (t=1; t<=tt; ++t) {
 89         scanf("%d %d", &n, &m);
 90         memset(F, 0, sizeof(F));
 91         while (m--) {
 92             scanf("%d %d %d", &i, &j, &k);
 93             F[i][j] += k;
 94         }
 95         Edmonds_Karp();
 96         printf("Case %d: %d\n", t, ans);
 97     }
 98     
 99     #ifndef ONLINE_JUDGE
100         printf("time = %d.\n", (int)clock());
101     #endif
102     
103     return 0;
104 }

 

posted on 2015-05-20 17:07  Bombe  阅读(154)  评论(0编辑  收藏  举报

导航