您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: