/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2011 panyanyany All rights reserved. URL : http://acm.hdu.edu.cn/showproblem.php?pid=3549 Name : 3549 Flow Problem Date : Wednesday, January 4, 2012 Time Stage : one hour Result: 5228624 2012-01-04 00:37:54 Accepted 3549 62MS 204K 1905 B C++ pyy Test Data : Review : 网络流-最大流模板题…… //----------------------------------------------------------------------------*/ #include <stdio.h> #include <string.h> #include <queue> using namespace std ; #define min(x, y) ((x) < (y) ? (x) : (y)) #define INF 0x3f3f3f3f #define MAXN 20 int tcase, n, m ; int map[MAXN][MAXN], path[MAXN], flow[MAXN] ; int Make_Point (const int start, const int end) { queue<int> q ; int t, i ; memset (path, 0, sizeof (path)) ; flow[start] = INF ; path[start] = INF ; q.push (start) ; while (!q.empty ()) { t = q.front () ; q.pop () ; if (t == end) break ; for (i = 1 ; i <= n ; ++i) { if (map[t][i] && !path[i] && i != start) { path[i] = t ; flow[i] = min (flow[t], map[t][i]) ; q.push (i) ; } } } if (path[end] == 0) return -1 ; return flow[end] ; } int Edmonds_Kard (const int start, const int end) { int i, j ; int incr, step, curr, prev ; incr = 0 ; while ((step = Make_Point (start, end)) != -1) { incr += step ; curr = end ; // 倒向追踪 while (curr != start) { prev = path[curr] ; // printf ("%d, %d\n", curr, prev) ; map[prev][curr] -= step ; map[curr][prev] += step ; curr = prev ; } } return incr ; } int main (void) { int i, j, k, x, y, c ; while (~scanf ("%d", &tcase)) { for (k = 1 ; k <= tcase ; ++k) { memset (map, 0, sizeof (map)) ; scanf ("%d%d", &n, &m) ; for (i = 1 ; i <= m ; ++i) { scanf ("%d%d%d", &x, &y, &c) ; map[x][y] += c ; } printf ("Case %d: %d\n", k, Edmonds_Kard (1, n)) ; } } return 0 ; }