首页 > 代码库 > HDU 3549 最大流入门
HDU 3549 最大流入门
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 8394 Accepted Submission(s): 3910
Problem Description
Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
Input
The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
Output
For each test cases, you should output the maximum flow from source 1 to sink N.
Sample Input
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
Sample Output
Case 1: 1
Case 2: 2
题目意思:
给一n个结点m条边的有向图,求从1到n的最大流。
思路:
最大流入门。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 #include <vector> 7 using namespace std; 8 9 #define N 2010 #define inf 99999999911 12 vector<int>ve[N];13 int map[N][N], flow[N], father[N];14 int n, m;15 16 int bfs(){17 int i, j, k, u, v, st=1, end=n;18 queue<int>Q;19 while(!Q.empty()) Q.pop();20 Q.push(st);21 memset(father,-1,sizeof(father));22 father[st]=st;flow[st]=inf;23 while(!Q.empty()){24 u=Q.front();Q.pop();25 for(i=0;i<ve[u].size();i++){26 v=ve[u][i];27 if(v!=st&&father[v]==-1&&map[u][v]!=0){28 flow[v]=min(flow[u],map[u][v]);29 father[v]=u;30 Q.push(v);31 }32 }33 }34 if(father[end]==-1) return -1;35 return flow[end];36 }37 38 int solve(){39 int st=1, end=n, max_flow=0;40 int step;41 while((step=bfs())!=-1){42 max_flow+=step;43 int u=end;44 while(u!=st){45 map[father[u]][u]-=step;46 map[u][father[u]]+=step;47 u=father[u];48 }49 }50 return max_flow;51 }52 main()53 {54 int t, i, j, k, kase=1;55 int x, y, z;56 cin>>t;57 while(t--){58 scanf("%d %d",&n,&m);59 for(i=0;i<=n;i++) ve[i].clear();60 memset(map,0,sizeof(map));61 while(m--){62 scanf("%d %d %d",&x,&y,&z);63 ve[x].push_back(y);ve[y].push_back(x);64 map[x][y]+=z;65 }66 printf("Case %d: %d\n",kase++,solve());67 }68 }
HDU 3549 最大流入门
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。