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

HDU 3549 最大流入门

时间:2014-10-13 18:23:39      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   java   for   

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)
 

 

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 20
10 #define inf 999999999
11 
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 最大流入门

标签:des   style   blog   color   io   os   ar   java   for   

原文地址:http://www.cnblogs.com/qq1012662902/p/4022129.html

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