POJ 1273 Drainage Ditches 网络流基础

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

红果果的网络流,Edmonds-Karp
最短增广路算法。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
#define maxe 210
int n,m,G[maxe][maxe],PreSta[maxe];
bool visit[maxe];
void init()
{
	int temp,from,to;
	memset(G,0,sizeof(G));
	for(int i=0;i<n;i++)
	{
		scanf("%d %d %d",&from,&to,&temp);
		G[from][to]+=temp;
	}
	return ;
}
int Augment()
{
	queue<int> Q;

	memset(visit,0,sizeof(visit));

	memset(PreSta,0,sizeof(PreSta));
	Q.push(1);
	visit[1]=1;
	bool findPath=0;
	while (!Q.empty())
	{
		int v=Q.front();
		Q.pop();
		for(int i=1;i<=m;i++)
		{
			if(G[v][i]>0&&visit[i]==0)
			{
				PreSta[i]=v;
				visit[i]=1;
				if(i==m)
				{
					findPath=1;
					while(!Q.empty())
					{
						Q.pop();
					}
					break;
				}
				else {
					Q.push(i);
				}
			}
		}
	}
	if(!findPath) return 0;
	int minflow=999999999;
	int tp=m;
	while(PreSta[tp])
	{
		minflow=min(minflow,G[PreSta[tp]][tp]);
		tp=PreSta[tp];
	}
	tp=m;
	while (PreSta[tp])
	{
		G[PreSta[tp]][tp]-=minflow;
		G[tp][PreSta[tp]]+=minflow;
		tp=PreSta[tp];
	}
	return minflow;
}

void slove()
{
	int ans=0;
	int a;
	while (a=Augment())
	{
		ans+=a;

	}
	printf("%d\n",ans);

}

int main()
{
	//freopen("data.in","r",stdin);

	while (scanf("%d %d",&n,&m)!=EOF)
	{
		init();
		slove();
	}
	return 0;
}

POJ 1273 Drainage Ditches 网络流基础

时间: 2024-11-07 00:19:55

POJ 1273 Drainage Ditches 网络流基础的相关文章

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 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

POJ 1273 Drainage Ditches (网络流最大流基础 Edmonds_Karp算法)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59219   Accepted: 22740 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

poj 1273 Drainage Ditches 网络流最大流基础

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59176   Accepted: 22723 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

POJ 1273 Drainage Ditches (网络流Dinic模板)

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

NYOJ 323 &amp;&amp; HDU 1532 &amp;&amp; POJ 1273 Drainage Ditches (网络流之最大流入门)

链接:click here 题意:给出n个河流,m个点,以及每个河流的流量,求从1到m点的最大流量. 思路:最裸的网络流题目  意思就是求从源点到汇点的最大流. 第一道网络流,一边看着书上的介绍,一边敲下代码: 用的是网络流算法ford-fulkerson 题目数据量小,邻接表和邻接矩阵都可以过 代码: #include <ctype.h> //最大流 入门 #include <stdio.h> #include <vector> #include <stdlib

POJ 1273 Drainage Ditches(初识网络流)

开始研究网络流了,看了两个晚上吧,今天总算动手实践一下,有了更深的理解 总结一下:在最大流中,容量与实际流量满足3点: 1.实际流量<=容量 2.任意两点之间   : 流量(a->b)==流量(b->a) 3.流量守恒原则   :从s流出的流量 == t流入的流量 一.为什么叫增广路,因为在所有的流量网络中,会存在一个残量,所以在整个残量网络中,找到一个最小值,加到所有的流量线路里,便叫增广. 二.为什么要修改反向流量,因为在更新流量网时,当前选择的并不一定就是最优解,比如u->v

POJ 1273 Drainage Ditches (网络最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55235   Accepted: 21104 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means

POJ 1273 Drainage Ditches 最大流

很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

hdu 1532 poj 1273 Drainage Ditches (最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55276   Accepted: 21122 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