Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:85327 | Accepted: 33196 |
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.
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
Source
http://poj.org/problem?id=1273
当板子用了,注意前向星从1开始编号的,反边对应的id不是^1,自己写了个pairid(),然后初始化建边的时候反边的cap是0不是负的,别的没了-。-
#include<cstdio>
#include<algorithm>
#include<cstring>
#define rep(i, j, k) for (int i=j; i<k; i++)
#define ll long long
#define dprintf if (debug) printf
using namespace std;
const int maxn = 2000;
const int INF = 0x7fffffff;
const int debug = 0;
int Q[maxn], dis[maxn], head[maxn], n, m, fr, to, cap, source, sink, cnt, add, ans;
struct Edge{
int to, cap, next;
}edges[maxn];
void addEdge(int fr, int to, int cap){
edges[++cnt].to = to;
edges[cnt].cap = cap;
edges[cnt].next = head[fr];
head[fr] = cnt;
}
inline int pairid(int x){
if (x&1) return x+1;
else return x-1;
}
int BFS(){
int h = 1, t = 1;
memset(dis, -1, sizeof(dis));
Q[h] = source; dis[source] = 0;
while (h<=t){
int now = Q[h++];
for (int i=head[now]; i; i=edges[i].next){
int to = edges[i].to;
if (dis[to] >= 0 || edges[i].cap <= 0) continue;
dprintf("fr = %d to = %d\n", now, to);
dis[to] = dis[now] + 1;
Q[++t] = to;
}
}
dprintf("EXIT BFS|\n");
if (dis[sink]>0) return true;
return false;
}
int DFS(int u, int low){
dprintf("DFS %d %d\n", u, low);
if (u == sink) return low;
for (int i=head[u]; i; i=edges[i].next){
int to = edges[i].to, cap = edges[i].cap, add;
if (dis[to] == dis[u]+1 && cap>0 && (add = DFS(to, min(cap, low)))){
dprintf("return to dfs %d %d\n", u, low);
dprintf("edges[%d].to = %d edges[%d].to = %d add = %d\n",
i, edges[i].to, pairid(i), edges[pairid(i)].to, add);
edges[i].cap -= add;
edges[pairid(i)].cap += add;
return add;
}
}
return 0;
}
int main(){
while(~scanf("%d%d", &n, &m)){
ans = cnt = 0; memset(head, 0, sizeof(head));
rep(i, 0, n){
scanf("%d%d%d", &fr, &to, &cap);
addEdge(fr, to, cap);
addEdge(to, fr, 0);//注意这里不是负的
}
source = 1; sink = m;
while (BFS()){
while(add = DFS(source, INF)){
ans += add;
}
}
printf("%d\n", ans);
}
}