【HDU 3549】Flow Problem 网络流
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
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
#include<cmath>
#include<set>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define closeio std::ios::sync_with_stdio(false)
int t,n,m,ans,s,e;
int c[2005][2005];
int depth[2005];
int bfs()
{
queue<int>q;
memset(depth,-1,sizeof(depth));
depth[s]=0;
q.push(s);
while(!q.empty())
{
int temp=q.front();
q.pop();
for(int i=1;i<=n;i++)
{
if(c[temp][i]&&depth[i]<0)
{
depth[i]=depth[temp]+1;
q.push(i);
}
}
}
if(depth[e]>0)
return 1;
return 0;
}
int dfs(int point,int maxflow)
{
if(point==e)
return maxflow;
int findflow;
for(int i=1;i<=n;i++)
{
if(c[point][i]&&depth[i]==depth[point]+1&&(findflow=dfs(i,min(maxflow,c[point][i]))))
{
c[point][i]-=findflow;
c[i][point]+=findflow;
return findflow;
}
}
return 0;
}
int main()
{
closeio;
cin>>t;
for(int Case=1;Case<=t;Case++)
{
memset(c,0,sizeof(c));
cin>>n>>m;
s=1,e=n;
int input_u,input_v,input_c;
while(m--)
{
cin>>input_u>>input_v>>input_c;
c[input_u][input_v]+=input_c;
}
ans=0;
while(bfs())
{
ans+=dfs(s,inf);
}
cout<<"Case "<<Case<<": "<<ans<<endl;
}
return 0;
}