【POJ】3204 Ikki's Story I - Road Reconstruction 最大流

来源:互联网 发布:tensorflow网络微调 编辑:程序博客网 时间:2024/05/16 11:40

传送门:【POJ】3204 Ikki's Story I - Road Reconstruction


题目分析:首先按照题目要求建图。跑一遍最大流。

接下来有两种解法。

一.枚举所有割边,增加1的容量,再在残余网络上跑看是否最大流为1(说明找到增广路),是的话res++。

最后输出res即可。

二.从源点开始沿着i%2==0 且E[ i ] > 0的边,将遇到的点标记1,再从汇点开始沿着i%2==1且E[ i ] > 0的边,将遇到的点标记2。最后扫一下所有的i%2==0的边,如果E[ i ] == 0且边的左右端点分别被标记1和2,说明给这条边增加容量一定可以增大流量,res++。

最后输出res即可。


代码如下:


#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 long long LL ;typedef int type_c ;const int MAXN = 505 ;const int MAXQ = 505 ;const int MAXE = 10005 ;const int INF = 0x3f3f3f3f ;struct Edge {int v , n ;type_c c , rc ;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] , cur[MAXN] , pre[MAXN] , num[MAXN] ;int Q[MAXQ] , head , tail ;int s , t , nv ;type_c flow ;int n , m ;int a[MAXE] ;int vis[MAXN] ;void init () {cntE = 0 ;CLR ( H , -1 ) ;}void addedge ( int u , int v , type_c c , type_c rc = 0 ) {E[cntE] = Edge ( v ,  c , H[u] ) ;H[u] = cntE ++ ;E[cntE] = Edge ( u , rc , 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] == -1 ) {Q[tail ++] = v ;d[v] = d[u] + 1 ;num[d[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 ;for ( mmin = nv , 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 solve_1 () {REP ( i , 0 , cntE )E[i].rc = E[i].c ;int cnt = 0 , res = 0 ;REP ( u , 0 , n )for ( int i = H[u] ; ~i ; i = E[i].n ) {if ( i & 1 )continue ;if ( E[i].c == 0 )a[cnt ++] = i ;}REP ( i , 0 , cnt ) {REP ( j , 0 , cntE )E[j].c = E[j].rc ;E[a[i]].c = E[a[i]].rc + 1 ;if ( ISAP () == 1 )++ res ;}printf ( "%d\n" , res ) ;}void dfs ( int u , int x ) {vis[u] = x + 1 ;for ( int i = H[u] ; ~i ; i = E[i].n )if ( i % 2 == x && E[i ^ x].c && vis[E[i].v] != x + 1 )dfs ( E[i].v , x ) ;}void solve_2 () {int res = 0 ;CLR ( vis , 0 ) ;dfs ( s , 0 ) ;dfs ( t , 1 ) ;REP ( u , 0 , n )if ( vis[u] == 1 )for ( int i = H[u] ; ~i ; i = E[i].n )if ( i % 2 == 0 && !E[i].c && vis[E[i].v] == 2 )++ res ;printf ( "%d\n" , res ) ;}void solve () {int u , v , c ;s = 0 , t = n - 1 , nv = n + 1 ;init () ;REP ( i , 0 , m ) {scanf ( "%d%d%d" , &u , &v , &c ) ;addedge ( u , v , c ) ;}ISAP () ;//solve_1 () ;solve_2 () ;}} e ;int main () {while ( ~scanf ( "%d%d" , &e.n , &e.m ) )e.solve () ;return 0 ;}


0 0