【HDU】4289 Control 最大流

来源:互联网 发布:win7连接网络打印机 编辑:程序博客网 时间:2024/06/05 04:10

传送门:【HDU】4289 Control


题目分析:将一个点x拆成两个点x、x',建边为点的容量,每一条无向边<u,v>建成两条有向边<u,v'>、<v,u'>,容量无穷大,跑一遍最大流既是答案。


代码如下:


#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )#define REV( i , a , b ) for ( int i = a - 1 ; i >= b ; -- i )#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define FOV( i , a , b ) for ( int i = a ; i >= b ; -- i )#define CLR( a , x ) memset ( a , x , sizeof a )#define CPY( a , x ) memcpy ( a , x , sizeof a )typedef int type_c ;const int MAXN = 405 ;const int MAXQ = 405 ;const int MAXE = 100004 ;const int INF = 0x3f3f3f3f ;struct Edge {int v , n ;type_c c ;Edge () {}Edge ( int v , type_c c , int n ) : v ( v ) , c ( c ) , n ( n ) {}} ;struct Net {Edge E[MAXE] ;int H[MAXN] , cntE ;int d[MAXN] , num[MAXN] , pre[MAXN] , cur[MAXN] ;int Q[MAXQ] , head , tail ;int s ,t , nv ;type_c flow ;int n , m ;void init () {cntE = 0 ;CLR ( H , -1 ) ;}void addedge ( int u , int v , type_c c ) {E[cntE] = Edge ( v , c , H[u] ) ;H[u] = cntE ++ ;E[cntE] = Edge ( u , 0 , H[v] ) ;H[v] = cntE ++ ;}void rev_bfs () {CLR ( d , -1 ) ;CLR ( num , 0 ) ;head = tail = 0 ;Q[tail ++] = t ;d[t] = 0 ;num[d[t]] = 1 ;while ( head != tail ) {int u = Q[head ++] ;for ( int i = H[u] ; ~i ; i = E[i].n ) {int v = E[i].v ;if ( ~d[v] )continue ;d[v] = d[u] + 1 ;num[d[v]] ++ ;Q[tail ++] = v ;}}}type_c ISAP () {CPY ( cur , H ) ;rev_bfs () ;flow = 0 ;int u = pre[s] = s , i , pos , mmin ;while ( d[s] < nv ) {if ( u == t ) {type_c f = INF ;for ( i = s ; i != t ; i = E[cur[i]].v )if ( f > E[cur[i]].c ) {f = E[cur[i]].c ;pos = i ;}for ( i = s ; i != t ; i = E[cur[i]].v ) {E[cur[i]].c -= f ;E[cur[i] ^ 1].c += f ;}u = pos ;flow += f ;}for ( i = cur[u] ; ~i ; i = E[i].n )if ( E[i].c && d[u] == d[E[i].v] + 1 )break ;if ( ~i ) {cur[u] = i ;pre[E[i].v] = u ;u = E[i].v ;}else {if ( 0 == -- num[d[u]] )break ;mmin = nv ;for ( i = H[u] ; ~i ; i = E[i].n )if ( E[i].c && mmin > d[E[i].v] ) {mmin = d[E[i].v] ;cur[u] = i ;}d[u] = mmin + 1 ;num[d[u]] ++ ;u = pre[u] ;}}return flow ;}void read ( int &x ) {char c ;do {c = getchar () ;} while ( c < '0' || c > '9' ) ;x = c - '0' ;while ( ( c = getchar () ) >= '0' && c <= '9' )x = x * 10 + c - '0' ;}void solve () {int u , v , c ;nv = n << 1 | 1 ;init () ;read ( s ) ;read ( t ) ;t += n ;FOR ( i , 1 , n ) {read ( c ) ;addedge ( i , i + n , c ) ;}while ( m -- ) {read ( u ) ;read ( v ) ;addedge ( u + n , v , INF ) ;addedge ( v + n , u , INF ) ;}printf ( "%d\n" , ISAP () ) ;}} e ;int main () {while ( ~scanf ( "%d%d" , &e.n , &e.m ) )e.solve () ;return 0 ;}


0 0
原创粉丝点击