【网络流】poj 1273 Drainage Ditches

第一次写网络流的算法,巨水的题目......利用残留网络不断的寻找增广路径就行了。

#include<iostream>
#include<queue>
using namespace std;

bool augment();
 
//capacity of pipe
int capacity[205][205];
//previous pipe
int record[1005];
int n;
bool checked[205];
queue<int> q;

int main(){
	int pipeNumber,maximum,minimum;
	int x,y,cap,tmpPre,tmpNext;
	while(cin>>pipeNumber>>n){
		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			capacity[i][j]=0;
			//node[i][j].capacity=0;
			//node[i][j].pre=-1;
			record[i]=-1;
		}
		for(int i=0;i<pipeNumber;i++){
			cin>>x>>y>>cap;
			//if(cap>capacity[x][y])
			capacity[x][y]+=cap;
		}
		maximum=0;
		while(augment()==true){
			tmpPre=record[n];
			tmpNext=n;
			minimum=999999;
			while(tmpPre!=-1){
				if(capacity[tmpPre][tmpNext]<minimum)
					minimum=capacity[tmpPre][tmpNext];
				tmpNext=tmpPre;
				tmpPre=record[tmpPre];
			}
			tmpPre=record[n];
			tmpNext=n;		
			while(tmpPre!=-1){
				//更新残留网络 
				capacity[tmpPre][tmpNext]-=minimum;
				capacity[tmpNext][tmpPre]+=minimum;
				tmpNext=tmpPre;
				tmpPre=record[tmpPre];
			}
			maximum+=minimum;
		}
		cout<<maximum<<endl;
	}
	return 0;
}

bool augment(){
	bool havePath;
	for(int i=1;i<=n;i++)
		checked[i]=false;
	while(q.empty()==false)
		q.pop();
	checked[1]=true;
	q.push(1);
	havePath=false;
	//广搜寻找增广路径 
	while(q.empty()==false){
		int tmp=q.front();
		//找到增广路径则退出循环 
		if(tmp==n){
			havePath=true;	
			break;
		}	
		q.pop();
		for(int i=1;i<=n;i++){
			if(checked[i]==false&&capacity[tmp][i]>0){
				checked[i]=true;
				q.push(i);
				record[i]=tmp;
			}
		}
	}
	return havePath;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值