您的位置:首页 > 其它

hdu 3549 Flow Problem(增广路算法)

2014-02-11 20:28 351 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549

模板题,白书上的代码。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

const int INF=1<<28;
int cap[30][30],flow[30][30],n;

int Edmonds_Karp(int s,int t)
{
int a[30],p[30];
int f;
queue<int>q;
memset(flow,0,sizeof(flow));
f=0;
while(1)
{
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);         //记录v的父亲,并加入FIFO队列
a[v]=min(a[u],cap[u][v]-flow[u][v]);  //s-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];     //更新反向流量
}
f+=a[t];                    //更新从s流出的总流量
}
return f;
}
int main()
{
int t,m,x=1;
int u,v,w;
scanf("%d",&t);
while(t--)
{
memset(cap,0,sizeof(cap));
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
cap[u][v]+=w; //考虑重边
}

printf("Case %d: ",x++);
printf("%d\n",Edmonds_Karp(1,n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: