标签:最大流
题意:有一个池塘因为下雨,通过修通渠道来排水,怎么排的水最多?
解析:经典最大流,通过改变容量,来减少流量的使用,实现了空间优化
#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
标签:最大流
原文地址:http://blog.csdn.net/bo_jwolf/article/details/37822743