码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3549 Flow Problem (最大流入门题)

时间:2016-05-14 15:34:52      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

 1 /************************************************************
 2            Flow Problem (hdu 3549)
 3            最大流入门
 4            http://acm.hdu.edu.cn/showproblem.php?pid=3549
 5 
 6 
 7 ************************************************************/
 8 
 9 
10 #include<iostream>
11 #include<cstdio>
12 #include<cstring>
13 #include<algorithm>
14 #include<vector>
15 #include<queue>
16 using namespace std;
17 
18 const int mx=20;
19 int cap[mx][mx],father[mx];
20 int c[mx];
21 
22 int bfs(int n)
23 {
24     queue<int>q;
25     int ans=0;
26 
27     while (true)
28     {
29         while (!q.empty()) q.pop();
30         father[1]=1;
31         q.push(1);
32         memset(c,0,sizeof(c));
33 
34         c[1]=100000000;
35         while (!q.empty())
36         {
37             int u=q.front();
38             q.pop();
39             int i;
40             for (i=1;i<=n;i++)
41             {
42                 if (!c[i]&&cap[u][i])                 ///一定要加!c[i]不然会无限循环
43                 {
44                     c[i]=min(c[u],cap[u][i]);         ///找i到n最小值
45                     q.push(i);
46                     father[i]=u;
47                     if (i==n) break;
48                 }
49             }
50             if (i==n) break;
51         }
52         if (c[n]==0) return ans;                    ///i到n没有流量了
53         ans+=c[n];
54 
55         int u=n;
56         while (u!=1)                             
57         {
58             cap[father[u]][u]-=c[n];
59             cap[u][father[u]]+=c[n];
60             u=father[u];
61         }
62     }
63 }
64 
65 int main()
66 {
67     int t,n,m,k=0;
68     scanf("%d",&t);
69     while (t--)
70     {
71         int n,m;
72         scanf("%d%d",&n,&m);
73         memset(cap,0,sizeof(cap));
74 
75         int x,y,z;
76         while (m--)
77         {
78             scanf("%d%d%d",&x,&y,&z);
79             cap[x][y]+=z;
80         }
81 
82         printf("Case %d: %d\n",++k,bfs(n));
83     }
84 }

 

hdu 3549 Flow Problem (最大流入门题)

标签:

原文地址:http://www.cnblogs.com/pblr/p/5492614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!