您的位置:首页 > 理论基础 > 计算机网络

POJ 1273 Drainage Ditches 网络流基础题

2012-08-18 23:56 435 查看
题目扫了一眼,直接EK求解,1A。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 205
#define inf 1<<28
#define LL(x) (x<<1)
#define RR(x)(x<<1|1)
using namespace std;

int Map[Max][Max];
int flow[Max];
int start,end;
int q[Max*100];
int path[Max];
int bfs()
{
flow[1]=inf;
int num=0,cnt=0;
q[num++]=1;
memset(path,-1,sizeof(path));
path[1]=1;
int n=end;
while(num>cnt)
{
int temp=q[cnt++];
if(temp==end)break;
for(int i=1;i<=n;i++)
{
if(path[i]==-1&&Map[temp][i])
{
flow[i]=min(flow[temp],Map[temp][i]);
q[num++]=i;
path[i]=temp;
}
}
}
if(path[end]==-1)return -1;
return flow[end];
}
long long  Edmonds_Karp()
{
int now,step,pre;
long long max_flow=0;
while((step=bfs())!=-1)
{
now=end;
max_flow+=step;
while(now!=start)
{
pre=path[now];
Map[pre][now]-=step;
Map[now][pre]+=step;
now=pre;
}
}
return max_flow;
}
int main()
{
int i,j,k,l,n,m,a,b;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(Map,0,sizeof(Map));
while(m--)
{
scanf("%d%d%d",&a,&b,&l);
Map[a][b]+=l;
}
start=1,end=n;
printf("%lld\n",Edmonds_Karp());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  网络 path