hdu------(3549)Flow Problem(最大流(水体))
2014-09-12 21:31
597 查看
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 8203 Accepted Submission(s): 3817
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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)
[align=left]Output[/align]
For each test cases, you should output the maximum flow from source 1 to sink N.
[align=left]Sample Input[/align]
2
3 2
1 2 1
2 3 1
3 3
1 2 1
2 3 1
1 3 1
[align=left]Sample Output[/align]
Case 1: 1
Case 2: 2
[align=left]Author[/align]
HyperHexagon
[align=left]Source[/align]
HyperHexagon's Summer Gift (Original tasks)
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<queue> using namespace std; const int inf=0x3f3f3f3f; int mat[30][30]; int dist[31]; int n,m; int min(int a,int b) { return a<b?a:b; } bool bfs(int st,int to){ memset(dist,-1,sizeof(dist)); queue<int>q; q.push(st); dist[st]=0; int t; while(!q.empty()){ t=q.front(); q.pop(); for(int i=1;i<=n;i++){ if(dist[i]<0&&mat[t][i]>0){ dist[i]=dist[t]+1; if(i==to)return 1; q.push(i); } } } return 0; } int dfs(int st,int to,int flow) { int tem; if(st==to||flow==0) return flow; for(int i=1;i<=n;i++){ if((dist[i]==dist[st]+1)&&mat[st][i]>0&&(tem=dfs(i,to,min(mat[st][i],flow)))) { mat[st][i]-=tem; mat[i][st]+=tem; return tem; } } return 0; } int Dinic(int st,int en) { int ans=0; while(bfs(st,en)) ans+=dfs(st,en,inf); return ans; } int main(){ int cas,i,a,b,c; scanf("%d",&cas); for(i=1;i<=cas;i++){ scanf("%d%d",&n,&m); memset(mat,0,sizeof(mat)); while(m--){ scanf("%d%d%d",&a,&b,&c); mat[a][b]+=c; } printf("Case %d: %d\n",i,Dinic(1,n)); } return 0; }
View Code
相关文章推荐
- 【最大流+模板题】杭电 hdu 3549 Flow Problem
- hdu 3549 Flow Problem【最大流】
- HDU 3549--Flow Problem【最大流】
- HDU 3549 Flow Problem 最大流 最小增广路 EK算法 传说中的入门算法
- hdu 3549 Flow Problem 最大流
- hdu 3549 Flow Problem (最大流)
- hdu 3549 Flow Problem【最大流增广路入门模板题】
- HDU 3549 Flow Problem(最大流模版EK算法)
- hdu 3549 最大网络流 Flow Problem
- HDU 3549 Flow Problem 最大流 最小增广路 DINIC算法 也是46MS
- HDU 3549 Flow Problem【Ford-Fulkerson+DFS 最大流】
- hdu 3549 Flow Problem (网络最大流)
- hdu 3549 Flow Problem(最大流模版题 Edmonds-Karp算法)
- HDU 3549 Flow Problem (用一道最裸的最大流开启网络流算法之路)
- HDU 3549 Flow Problem(最大流最短路径增广)
- HDU-3549 Flow Problem【最大流】
- HDU--杭电--3549--Flow Problem--最大流
- 【最大流(EK)】hdu 3549 Flow Problem
- HDU 3549 Flow Problem 流问题(最大流,入门)
- hdu 3549 Flow Problem【最大流增广路入门模板题】