【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
- 【topcoder】570div1 900 CurvyonRails【费用流】
- [LP对偶 & 最大费用可行流] TopCoder SRM 676 div1 Farmville
- TopCoder SRM 612 DIV1 900
- Topcoder.SRM527.Div1.T2
- topcoder SRM500 div1 Level3
- topcoder SRM495 div1 level2
- topcoder SRM495 div1 level3
- Topcoder SRM552 Div1 500
- TopCoder SRM480 DIV1 Practise
- Topcoder--SRM144 div1
- topcoder SRM598 div1
- topcoder srm609 div1
- TOPCODER--SRM615 div1 AmebaDiv1
- Topcoder Srm 648 DIV1
- Topcoder Srm 649 DIV1
- topcoder SRM500 div1 Level3
- TopCoder 603 div1 & div2
- Topcoder Srm 654 DIV1
- LeetCode 58. Length of Last Word
- JavaScript中的匿名函数及函数的闭包
- 题目<2016/11/30>
- MySql常用命令
- Linux磁盘管理基础
- 【topcoder】570div1 900 CurvyonRails【费用流】
- 月末随笔
- Activity启动的四种模式
- ScrollView嵌套listview出现的问题
- javascript数据类型
- 坚持#第111天~rexhang都优秀成传说了,分分钟搞定一个项目,这有什么好苦逼的,我也要变优秀!
- 【数据结构】二叉树部分面试题解法
- RocketMQ与kafka对比
- JavaScript谁动了你的代码