poj 1273 Drainage Ditches
http://poj.org/problem?id=1273
题意:有一些流水的水沟,求从起点1到达终点的最大水量,每条水沟都有一个最大流水量;
思路:基础的网络流问题(EK算法),注意边的重复;
View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define max 205
using namespace std;
int s[max][max] = {0};
int n = 0;
int m = 0;
int a = 0;
int b = 0;
int ss = 0;
void ek()
{
int ans = 0;
int pre[max] = {0};
int flow[max] = {0};
while(1)
{
memset(pre,0,sizeof(pre));
memset(flow,0,sizeof(flow));
queue<int>que;
que.push(1);
flow[1] = 99999999;
while(!que.empty())
{
a=que.front();
que.pop();
for(int i =1;i <= m; ++i)
if(!pre[i]&&s[a][i])
{
flow[i] = min(flow[a],s[a][i]);
pre[i] = a;
que.push(i);
}
}
if(pre[m] == 0)
break;
ans += flow[m];
for(int i = m; i != 1;i = pre[i])
{
s[pre[i]][i]-=flow[m];
s[i][pre[i]]+=flow[m];
}
}
printf("%d\n",ans);
}
int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
memset(s,0,sizeof(s));
for(int i = 1;i <= n ;++i)
{
scanf("%d%d%d",&a,&b,&ss);
s[a][b] += ss;
}
ek();
}
return 0;
}