码迷,mamicode.com
首页 > 其他好文 > 详细

Drainage Ditches

时间:2014-07-16 10:55:10      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:最大流

点击打开链接

题意:有一个池塘因为下雨,通过修通渠道来排水,怎么排的水最多?

解析:经典最大流,通过改变容量,来减少流量的使用,实现了空间优化

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<queue>

using namespace std;

const  int maxn = 1005;
#define INF 0xfffffff
int n, m, u, v, value, sum, start, end;
int pre[ maxn ], cap[ maxn ][ maxn ], flow[ maxn ][ maxn ], dis[ maxn ];

int EK (){
	
	queue< int > Q;
	sum = 0;
	while( 1 ){
		Q.push( start );
		memset( dis, 0, sizeof( dis ) );
		dis[ start ] = INF;
		while( !Q.empty() ){
			int temp = Q.front();
			Q.pop();
			for( int i = 1; i <= m; ++i ){
				if( ! dis[ i ] && cap[ temp ][ i ] > 0 ){
					dis[ i ] = min( dis[ temp ], cap[ temp ][ i ]  );	
					pre[ i ] = temp;
					Q.push( i );
				}
			}
		}
		if( dis[ m ] == 0 )
			break;
		sum += dis[ m ];
		for( int i = end; i != start; i = pre[ i ] ){
			cap[ pre[ i ] ][ i ] -= dis[ m ];
			cap[ i ][ pre[ i ] ] += dis[ m ];
		}
	}
	printf( "%d\n", sum );
}

int main(){
	while( scanf( "%d%d", &n, &m ) != EOF ){
		memset( cap, 0, sizeof( cap ) );
		memset( flow, 0, sizeof( flow ) );
		start = 1, end = m;
		for( int i = 0; i < n; ++i ){
			scanf( "%d%d%d", &u, &v, &value );
			cap[ u ][ v ] += value;
		}
		EK( );
	}
}

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

50
*/


Drainage Ditches,布布扣,bubuko.com

Drainage Ditches

标签:最大流

原文地址:http://blog.csdn.net/bo_jwolf/article/details/37822743

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!