| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 56466 | Accepted: 21694 |
Description
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
Source
可以用来当做网络流的入门题。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 500
#define INF 100000000
int n, m, s, t;
int a[maxn];
int p[maxn];
int cap[maxn][maxn];
int flow[maxn][maxn];
int main()
{
while(~scanf("%d%d",&m,&n))
{
memset(cap,0,sizeof(cap));
for(int i = 0; i < m; i++)
{
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
cap[u][v] += c;
}
s = 1;
t = n;
int ans = 0;
queue<int> q;
memset(flow, 0, sizeof(flow));
for(;;)
{
memset(a, 0, sizeof(a));
a[s] = INF;
q.push(s);
while(!q.empty()) //bfs找增广路
{
int u = q.front();
q.pop();
for(int v = 1; v <= n; v++) if(!a[v] && cap[u][v]>flow[u][v])
{ //找到新节点V并入队
p[v] = u; q.push(v);
if(a[u]<cap[u][v]-flow[u][v]) //s-v上最小残量
a[v]=a[u];
else
a[v]=cap[u][v]-flow[u][v];
}
}
if(a[t] == 0) break; //找不到,则当前已经是最小残量
for(int u = t; u != s; u = p[u]) //更新流量
{
flow[p[u]][u] += a[t];
flow[u][p[u]] -= a[t];
}
ans += a[t]; //更新从S流出的总流量
}
printf("%d\n", ans);
}
return 0;
}
原文:http://blog.csdn.net/u013712847/article/details/38866721