【topcoder】570div1 900 CurvyonRails【费用流】

来源:互联网 发布:g3营销软件 编辑:程序博客网 时间:2024/05/23 00:10
#include <bits/stdc++.h>using namespace std ;#define clr( a , x ) memset ( a , x , sizeof a )const int MAXN = 3505 ;const int MAXE = 100005 ;const int INF = 0x3f3f3f3f ;struct Edge {    int v , c , w , n ;    Edge () {}    Edge ( int v , int c , int w , int n ) : v ( v ) , c ( c ) , w ( w ) , n ( n ) {}} ;Edge E[MAXE] ;int H[MAXN] , cntE ;int d[MAXN] , cur[MAXN] , vis[MAXN] ;int Q[MAXN] , head , tail ;int s , t , flow , cost ;int n , m ;int cnt ;void init () {    cntE = 0 ;    clr ( H , -1 ) ;}void addedge ( int u , int v , int c , int w ) {    E[cntE] = Edge ( v , c , +w , H[u] ) ;    H[u] = cntE ++ ;    E[cntE] = Edge ( u , 0 , -w , H[v] ) ;    H[v] = cntE ++ ;}int spfa () {    head = tail = 0 ;    for ( int i = 0 ; i <= cnt ; ++ i ) {        d[i] = INF ;        vis[i] = 0 ;    }    d[s] = 0 ;    Q[tail ++] = s ;    cur[s] = -1 ;    while ( head != tail ) {        int u = Q[head ++] ;        if ( head == MAXN ) head = 0 ;        vis[u] = 0 ;        for ( int i = H[u] ; ~i ; i = E[i].n ) {            int v = E[i].v ;            if ( E[i].c && d[v] > d[u] + E[i].w ) {                d[v] = d[u] + E[i].w ;                cur[v] = i ;                if ( !vis[v] ) {                    vis[v] = 1 ;                    Q[tail ++] = v ;                    if ( tail == MAXN ) tail = 0 ;                }            }        }    }    if ( d[t] == INF ) return 0 ;    cost += d[t] ;    flow ++ ;    for ( int i = cur[t] ; ~i ; i = cur[E[i ^ 1].v] ) {        E[i].c -- ;        E[i ^ 1].c ++ ;    }    return 1 ;}int mcmf () {    flow = cost = 0 ;    while ( spfa () ) ;    return cost ;}bool check ( int x , int y , int n , int m ) {    return ~x && x < n && ~y && y < m ;}class CurvyonRails {    public :    int getmin ( vector < string > G ) {        init () ;        cnt = 0 ;        int n = G.size () , m = G[0].length () , nm = n * m ;        /*        for ( int i = 0 ; i < n ; ++ i ) {            for ( int j = 0 ; j < m ; ++ j ) {                printf ( "%c" , G[i][j] ) ;            }            puts ( "" ) ;        }        */        s = nm * 3 ;        t = s + 1 ;        cnt = t ;        int p[4][2] = { { 0 , -1 } , { 0 , 1 } , { -1 , 0 } , { 1 , 0 } } ;        int L = 0 , R = 0 , tot = 0 ;        for ( int i = 0 ; i < n ; ++ i ) {            for ( int j = 0 ; j < m ; ++ j ) if ( G[i][j] != 'w' ) {                int idx = i * m + j , v = G[i][j] == 'C' ;                tot += v ;                if ( ( i + j ) % 2 == 1 ) {                    ++ R ;                    addedge ( idx , t , 2 , 0 ) ;                    addedge ( idx + nm , idx , 1 , -v ) ;                    addedge ( idx + nm + nm , idx , 1 , v ) ;                } else {                    ++ L ;                    addedge ( s , idx , 2 , 0 ) ;                    addedge ( idx , idx + nm , 1 , -v ) ;                    addedge ( idx , idx + nm + nm , 1 , v ) ;                }                for ( int k = 0 ; k < 4 ; ++ k ) {                    int x = i + p[k][0] ;                    int y = j + p[k][1] ;                    if ( !check ( x , y , n , m ) || G[i][j] == 'w' ) continue ;                    int idy = x * m + y ;                    if ( ( i + j ) % 2 == 0 ) {                        if ( k >= 2 ) addedge ( idx , idy , 1 , 0 ) ;                        else {                            ++ cnt ;                            addedge ( idx + nm , cnt , 1 , 0 ) ;                            addedge ( idx + nm + nm , cnt , 1 , 0 ) ;                            ++ cnt ;                            addedge ( cnt , idy + nm , 1 , 0 ) ;                            addedge ( cnt , idy + nm + nm , 1 , 0 ) ;                            addedge ( cnt - 1 , cnt , 1 , 0 ) ;                        }                    }                }            }        }        //printf ( "%d %d\n" , L , R ) ;        if ( L != R ) return -1 ;        mcmf () ;        //printf ( "%d %d\n" , flow , cost ) ;        if ( flow != R * 2 ) return -1 ;        return tot + cost ;    }} ;int main () {    while ( ~scanf ( "%d" , &n ) ) {        CurvyonRails* it = new CurvyonRails ;        vector < string > s ;        s.clear () ;        for ( int i = 0 ; i < n ; ++ i ) {            s.push_back ( "" ) ;            cin >> s[i] ;        }        printf ( "%d\n" , it->getmin ( s ) ) ;    }    return 0 ;}
0 0
原创粉丝点击