USACO SECTION 4.2 Drainage Ditches

Drainage Ditches
Hal Burch

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. Note however, that there can be more than one ditch between two intersections.

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.

PROGRAM NAME: ditch

INPUT FORMAT

Line 1: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.
Line 2..N+1:Each of 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.

SAMPLE INPUT (file ditch.in)

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

OUTPUT FORMAT

One line with a single integer, the maximum rate at which water may emptied from the pond.

SAMPLE OUTPUT (file ditch.out)

50

 

/*
ID: conicoc1
LANG: C
TASK: ditch
*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define INIFINITY 10000000

int Link[201][201];
int Flow[201][201];
int Flag[201];
int Vertex,Edge;
int Ans=0;

int Min(int a,int b)
{
	if(a>b)
		return b;
	return a;	
}

int DFS(int V,int NowFlow)
{
	Flag[V]=1;
		printf("%d %d\n",V,NowFlow);
//			getchar();
	if(V==Vertex)
		return NowFlow;
	int W,i,j,temp;
	temp=NowFlow;
	for(i=1;i<=Link[V][0];i++){	
		W=Link[V][i];
		NowFlow=Min(temp,Flow[V][W]);
		if(NowFlow==0||Flag[W]){
			NowFlow=0;
			continue;
		}
		if( NowFlow=Min(NowFlow,DFS(W,NowFlow)) ){
			Flow[V][W]-=NowFlow;
			for(j=1;j<=Link[W][0];j++)
				if(Link[W][j]==V)
					break;
			if(j==Link[W][0]+1)
				Link[W][++Link[W][0]]=V;
			Flow[W][V]+=NowFlow;
			return NowFlow;
		}
	}
	return 0;
}

int main()
{
	FILE *fin,*fout;
	fin=fopen("ditch.in","r");
	fout=fopen("ditch.out","w");
	memset(Link,0,sizeof(Link));
	memset(Flow,0,sizeof(Flow));
	memset(Flag,0,sizeof(Flag));
	int i,j,k,Val;
	fscanf(fin,"%d %d",&Edge,&Vertex);
	for(k=0;k<Edge;k++){
		fscanf(fin,"%d %d %d",&i,&j,&Val);
		Flow[i][j]+=Val;
		Link[i][++Link[i][0]]=j;
	}	
	
	while(Val=DFS(1,INIFINITY)){
		memset(Flag,0,sizeof(Flag));
		Ans+=Val;
		printf("%d\n",Val);
	}
	
	fprintf(fout,"%d\n",Ans);
	return 0;
}


裸的不能再裸的最大流。。

 

下面是几种最大流算法

/*************求最大流的几种算法***********/ 
/*所有边都用邻接矩阵保存,如果是稀疏图可用邻接表优化*/
 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define INIFINITY 10000000
#define Min(a,b) a>b?b:a

int Map[201][201];
int Vertex,Edge;
int Path[201];
int Queue[201];
int front=0,rear=0;

void DeleteQueue()
{
	front=0;
	rear=0;
}

void Enqueue(int V)
{
	Queue[rear]=V;
	rear=(rear+1)%201;
}

int Dequeue()
{
	int V;
	V=Queue[front];
	front=(front+1)%201;
	return V;
}

/*一、Edmonds-Karp,通过BFS每次找最短的增广路径*/
int BFS()
{
	int V,i,Flow[201];
	memset(Flow,0,sizeof(Flow));
	memset(Path,0,sizeof(Path));
	Flow[1]=INIFINITY;
	DeleteQueue();
	Enqueue(1);
	while(front!=rear){
		V=Dequeue();
		if(V==Vertex)
			break;
		for(i=1;i<=Vertex;i++){
			if(!Flow[i]&&Map[V][i]){
				Flow[i]=Min(Flow[V],Map[V][i]);
				Path[i]=V;
				Enqueue(i);
			}
		}
	}
	if(V==Vertex)
		return Flow[V];   //返回该增广路径的最大可行流 
	return 0;			
}

int EdmondsKarp()
{
	int Flow,V,Ans=0;
	while(Flow=BFS()){
		Ans+=Flow;
		V=Vertex;
		while(Path[V]){        //通过路径更新网络 
			Map[V][Path[V]]+=Flow;
			Map[Path[V]][V]-=Flow;
			V=Path[V];
		}
	}
	return Ans;
}
 
/*二、Dicnic算法,先用BFS建立分层网络(距离标号),然后用DFS寻找增广路径*/ 
/*可以用路径回溯来代替递归的DFS寻找增广路径*/
 
/*三、Improved SAP 算法,对顶点距离进行重标号,避免多次的BFS*/ 

void BuildDist(int *Dist)
{
	int Flag[201],V,i;
	memset(Flag,0,sizeof(Flag))	;
	Flag[Vertex]=1;
	Enqueue(Vertex);
	while(front!=rear){
		V=Dequeue();
		for(i=1;i<=Vertex;i++)
			if(Map[i][V]&&!Flag[i]){
				Dist[i]=Dist[V]+1;
				Flag[i]=1;
				Enqueue(i);
			}
	}
}

int ImprovedSap() 
{
	int Ans=0,Flow,i;
	int Dist[201],V=1;
	memset(Dist,0,sizeof(Dist));
	memset(Path,0,sizeof(Path));
	BuildDist(Dist);    //初始化标号 
	while(Dist[1]<Vertex){
		if(V==Vertex){             //找到一条增广路径 
			Flow=INIFINITY;
			while(Path[V]!=0){       //求最大可增广流 
				Flow=Min(Flow,Map[Path[V]][V]);
				V=Path[V];
			}
			V=Vertex;
			while(Path[V]!=0){      //更新残留网络 
				Map[Path[V]][V]-=Flow;
				Map[V][Path[V]]+=Flow;
				V=Path[V];
			}
			Ans+=Flow;
//			printf("%d\n",Flow);
		}
		else{
//			printf("%d\n",V);
			for(i=1;i<=Vertex;i++){
				if(Map[V][i]&&Dist[V]==Dist[i]+1){  //向前进一个点 
					Path[i]=V;
					V=i;
					break;
				}
			}
			if(i==Vertex+1){        //重标号
				Dist[V]=Vertex;
				for(i=1;i<=Vertex;i++)
					if(Map[V][i])
						Dist[V]=Min(Dist[V],Dist[i]+1);	
				if(V!=1)           //回溯 
					V=Path[V];
			}
		}
	}
	return Ans;
}


 

SAP算法有个GAP优化,就是用一个数组保存标号出现的次数,当重标号过程中出现某个标号的次数为0时,直接退出循环

貌似还有个预流推进的方法。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值