意甲冠军:有因雨池塘,通过修通渠道流失。如何污水排放达?
解决:古典最大流量,通过改变容量,为了减少使用流量,了空间优化
#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 ", 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 */