【最大流+模板题】杭电 hdu 3549 Flow Problem

来源:互联网 发布:淘宝如何免费自动发货 编辑:程序博客网 时间:2024/05/19 04:25


/* THE PROGRAM IS MADE BY PYY *//*----------------------------------------------------------------------------//Copyright (c) 2011 panyanyany All rights reserved.URL   : http://acm.hdu.edu.cn/showproblem.php?pid=3549Name  : 3549 Flow ProblemDate  : Wednesday, January 4, 2012Time Stage : one hourResult: 52286242012-01-04 00:37:54Accepted354962MS204K1905 BC++pyyTest Data :Review :网络流-最大流模板题……//----------------------------------------------------------------------------*/#include <stdio.h>#include <string.h>#include <queue>using namespace std ;#define min(x, y)((x) < (y) ? (x) : (y))#define INF0x3f3f3f3f#define MAXN20inttcase, n, m ;intmap[MAXN][MAXN], path[MAXN], flow[MAXN] ;int Make_Point (const int start, const int end){queue<int>q ;intt, 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){inti, 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 ;}


原创粉丝点击