您的位置:首页 > 其它

hdu 3549 Flow Problem(EK)

2015-08-03 21:27 302 查看

Flow Problem

Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 10403 Accepted Submission(s): 4901



[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<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int map[20][20],pre[20],vis[20];
int n,m;
int bfs(int start,int end){
int i,t;
memset(pre,-1,sizeof(pre));
memset(vis,0,sizeof(vis));
queue <int> q;
while(!q.empty()) q.pop();
q.push(start);
vis[start]=1;
while(!q.empty()){
t=q.front();
if(t==end) return 1;
q.pop();
for(i=0;i<=n+1;i++){
if(!vis[i]&&map[t][i]){
vis[i]=1;
pre[i]=t;
q.push(i);
}
}
}
return 0;
}
int maxflow(int start,int end){
int v,temp,ans=0;
while(bfs(start,end)){
temp=0x3f3f3f3f;
v=end;
while(pre[v]!=-1){
temp=min(temp,map[pre[v]][v]);
v=pre[v];
}
ans+=temp;
v=end;
while(pre[v]!=-1){
map[pre[v]][v]-=temp;
map[v][pre[v]]+=temp;
v=pre[v];
}
}
return ans;
}
int main(){
int t,i,u,v,w,Case=1;
scanf("%d",&t);
while(t--){
memset(map,0,sizeof(map));
scanf("%d%d",&n,&m);
for(i=0;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
map[u][v]+=w;
}
printf("Case %d: %d\n",Case++,maxflow(1,n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: