【最大流】POJ-1273 Drainage Ditches

Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000K
   

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
————————————————————惭愧的分割线————————————————————
前言:刚学会Dinic,蓝皮书就给了我PIGS那道题,我死活建不出图来,但是我发誓这书上所有网络流建图我都自己思考决不看题解。因为直接套模板的题目,看别人怎样建图就没有意义了。先跳过PIGS。
思路:真真是模板题。因为给出了图,不用建图……那就直接上模板吧。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
/****************************************/
const int N = 222, M = 444;
int n, m, tot, head[N], S, T;
int lev[N], q[N], s[N], cur[N];
struct Node {
	int u, v, c;
	int next;
}edge[M];

void init()
{
	tot = 0;
	memset(head, -1, sizeof(head));
}

void add(int u, int v, int c)
{
	edge[tot].u = u; edge[tot].v = v; edge[tot].c = c;
	edge[tot].next = head[u];
	head[u] = tot++;
}

bool bfs()
{
	memset(lev, -1, sizeof(lev));//初始化标号
	int fron = 0, rear = 0;
	lev[S] = 0;
	q[rear++] = S;//起点标号进队
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		for(int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].v;
			if(edge[i].c && lev[v] == -1) {
				lev[v] = lev[u] + 1;
				q[rear%N] = v; rear++;
				if(v == T) return true;
			}//新的点标号进队
		}
	}
	return false;
}

int Dinic()
{
    int ret = 0;
    while(bfs()) {
        memcpy(cur, head, sizeof(head));
        int u = S, top = 0;
        while(1) {
            if(u == T) {//找到一条增广路
                int mini = INF, loc;
                for(int i = 0; i < top; i++) {
                    if(mini > edge[s[i]].c) {
                        mini = edge[s[i]].c;
                        loc = i;
                    }
				}//找到该增广路上的最小值
                for(int i = 0; i < top; i++) {
                    edge[s[i]].c -= mini;
                    edge[s[i]^1].c += mini;
                }//更新该路径上的容量
                ret += mini;
                top = loc;//回溯到阻塞流点
                u = edge[s[top]].u;
            }
			int &i = cur[u];
            for(; i != -1; i = edge[i].next) {
				int v = edge[i].v;
                if(edge[i].c && lev[u] + 1 == lev[v]) break;
			}//找到允许弧,并将其作为当前弧
            if(i != -1) {
                s[top] = i; top++;
                u = edge[i].v;//入栈并更新u
            }
            else {
                if(top == 0) break;
                lev[u] = -1;
                top--; u = edge[s[top]].u;
            }//找不到允许弧,废了当前点并回退一个点,更新u
        }
    }
    return ret;
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	while(~scanf("%d%d", &m, &n)) {
		init();
		S = 1; T = n;
		int u, v, c;
		for(int i = 1; i <= m; i++) {
			scanf("%d%d%d", &u, &v, &c);
			add(u, v, c); add(v, u, 0);
		}
		printf("%d\n", Dinic());
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值