HDU 3549 Flow Problem
Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2857 Accepted Submission(s): 1348
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)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#define N 1003
using namespace std;
int flow[N][N],cap[N][N];
int f[N];
int n,m;
int BFS()
{
int u,v;
int a[N];
int sc=0;
for(int i=1;i<=n;i++)//把里面的这种初始化过程改成memset,那么将变成4900+Ms,而这样就60+Ms
for(int j=1;j<=n;j++)//相差好大呀
flow[i][j]=0;
for(;;)
{
queue<int>Q;
for(int i=1;i<=n;i++)
a[i]=0;
a[1]=N;
Q.push(1);
while(!Q.empty())
{
u=Q.front();Q.pop();
for(v=1;v<=n;v++)
if(!a[v]&&cap[u][v]-flow[u][v]>0)
{
f[v]=u;
Q.push(v);
a[v]=a[u]<cap[u][v]-flow[u][v]?a[u]:cap[u][v]-flow[u][v];
}
}
if(a[n]==0) break;
for(u=n;u!=1;u=f[u])
{
flow[f[u]][u]+=a[n];
flow[u][f[u]]-=a[n];
}
sc+=a[n];
}
return sc;
}
int main()
{
int t,T=1;
int u,v,c;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cap[i][j]=0;
while(m--)
{
scanf("%d%d%d",&u,&v,&c);
cap[u][v]+=c;
}
printf("Case %d: %d\n",T++,BFS());
}
return 0;
}