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

POJ1273 HDU1532 Drainage Ditches

2015-11-25 14:00 405 查看
题目链接:http://poj.org/problem?id=1273

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network.
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle.
Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.
裸最大流Dinic多路增广模版。CodeVS上AC,POJ上没看到The input includes several cases. WA了n次呵呵……

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#define rep(i,l,r) for(int i=l; i<=r; i++)
#define clr(x,y) memset(x,y,sizeof(x))
#define travel(x) for(int i=last[x]; i!=-1; i=edge[i].pre)
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 210;
struct Edge{
int pre,to,cost;
}edge[maxn<<1];
int m,n,x,y,z,now,tot,ans,last[maxn],d[maxn],cur[maxn];
queue <int> q;
inline void addedge(int x,int y,int z){
edge[++tot].pre = last[x];
edge[tot].to = y;
edge[tot].cost = z;
last[x] = tot;
edge[++tot].pre = last[y];
edge[tot].to = x;
edge[tot].cost = 0;
last[y] = tot;
}
bool bfs(){
while (!q.empty()) q.pop();
clr(d,-1); d[1] = 0; q.push(1);
while (!q.empty()){
now = q.front(); q.pop();
travel(now){
if (d[edge[i].to] == -1 && edge[i].cost > 0){
d[edge[i].to] = d[now] + 1;
q.push(edge[i].to);
}
}
}
return d
!= -1;
}
int dfs(int x,int in){
if (x == n) return in; int w = 0;
for(int i=cur[x]; i != -1 && w < in; i=edge[i].pre){
if (d[edge[i].to] == d[x] + 1 && edge[i].cost > 0){
int delta = dfs(edge[i].to,min(in-w,edge[i].cost));
edge[i].cost -= delta;
edge[i^1].cost += delta;
if (edge[i].cost) cur[x] = i;
w += delta;
}
}
if (w < in) d[x] = -1;
return w;
}
int main(){
while (scanf("%d%d",&m,&n) == 2){
tot = -1; ans = 0; clr(last,-1);
rep(i,1,m){
scanf("%d%d%d",&x,&y,&z);
addedge(x,y,z);
}
while (bfs()){
rep(i,1,n) cur[i] = last[i];
int nowans = dfs(1,0x7fffffff);
ans += nowans;
}
printf("%d\n",ans);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: