dinic复习 poj1273

博客围绕农场主约翰的排水渠系统展开,已知各排水渠每分钟输水量及布局,要确定池塘水流入溪流的最大速率。给出了输入输出格式及示例,还提醒使用时前向星编号、反边id及容量的注意事项。

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

USACO 93

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);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值