您的位置:首页 > 大数据 > 人工智能

POJ-1273 Drainage Ditches

2017-01-22 12:54 309 查看
题意:

n个河流,m个点,询问从1->m最大的流量是多少

思路:

网络流基础知识,http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html

去这个看吧,写的真够好。然后就是。。。板子

#include <iostream>
#include <queue>
#include<cstring>
#include <stdio.h>
using namespace std;

const int N = 205;
const int INF = 0x3f3f3f3f;
int n,m,mp

,path
,flow
,st,ed;
queue<int> q;

int bfs()
{
while(!q.empty())
q.pop();
memset(path,-1,sizeof(path));
path[st]=0,flow[st]=INF;
q.push(st);
while(!q.empty())
{
int t=q.front();
q.pop();
for(int i=1;i<=m;i++)
{
if(i!=st&&mp[t][i]&&path[i]==-1)
{
flow[i]=min(flow[t],mp[t][i]);
path[i]=t;
q.push(i);
}
}
}
if(path[ed]==-1)
return -1;
return flow[ed];
}
int Edmonds_Karp()
{
int max_flow=0,step,now,pre;
while((step=bfs())!=-1)
{
max_flow+=step;
now=ed;
while(now!=st)
{
pre=path[now];
mp[pre][now]-=step;
mp[now][pre]+=step;
now=pre;
}
}
return max_flow;
}
int main()
{
int i,u,v,cost;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(mp,0,sizeof(mp));
for(int i=1; i<=n; i++)
{
scanf("%d%d%d",&u,&v,&cost);
mp[u][v]+=cost;
}
st=1,ed=m;
printf("%d\n",Edmonds_Karp());
}
return 0;
}
/*
2 3
1 2 10
2 3 5
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: