本文主要是介绍【BZOJ】1324 Exca王者之剑 最大权独立集,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
传送门:【BZOJ】1324 Exca王者之剑
题目分析:赤裸裸的最大权独立集。。。最小割解决
代码如下:
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std ;#define REP( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define FOR( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define REV( 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 )const int MAXN = 10005 ;
const int MAXE = 80005 ;
const int INF = 0x3f3f3f3f ;struct Edge {int v , c , n ;Edge () {}Edge ( int v , int c , int n ) : v ( v ) , c ( c ) , n ( n ) {}
} ;struct NetWork {Edge E[MAXE] ;int H[MAXN] , cntE ;int d[MAXN] , cur[MAXN] , pre[MAXN] , cnt[MAXN] ;int Q[MAXN] , head , tail ;int s , t , nv ;int flow ;void clear () {cntE = 0 ;CLR ( H , -1 ) ;}void addedge ( int u , int v , int 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 ( cnt , 0 ) ;head = tail = 0 ;Q[tail ++] = t ;d[t] = 0 ;cnt[0] = 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 ) {d[v] = d[u] + 1 ;Q[tail ++] = v ;cnt[d[v]] ++ ;}}}}int ISAP () {CPY ( cur , H ) ;rev_bfs () ;flow = 0 ;int u = pre[s] = s , i , pos , minv , f ;while ( d[s] < nv ) {if ( u == t ) {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 ;}flow += f ;u = pos ;}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 == -- cnt[d[u]] ) break ;for ( minv = nv , i = H[u] ; ~i ; i = E[i].n ) if ( E[i].c && minv > d[E[i].v] ) {minv = d[E[i].v] ;cur[u] = i ;}d[u] = minv + 1 ;cnt[d[u]] ++ ;u = pre[u] ;}}return flow ;}
} e ;int n , m ;void scanf ( int& x , char c = 0 ) {while ( ( c = getchar () ) < '0' || c > '9' ) ;x = c - '0' ;while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
}void solve () {int x , sum = 0 ;e.clear () ;e.s = n * m ;e.t = e.s + 1 ;e.nv = e.t + 1 ;REP ( i , 0 , n ) REP ( j , 0 , m ) {scanf ( x ) ;sum += x ;int ij = i * m + j ;if ( ( i + j ) & 1 ) {e.addedge ( e.s , ij , x ) ;if ( i ) e.addedge ( ij , ij - m , INF ) ;if ( j ) e.addedge ( ij , ij - 1 , INF ) ;if ( i < n - 1 ) e.addedge ( ij , ij + m , INF ) ;if ( j < m - 1 ) e.addedge ( ij , ij + 1 , INF ) ;} else e.addedge ( i * m + j , e.t , x ) ;}printf ( "%d\n" , sum - e.ISAP () ) ;
}int main () {scanf ( "%d%d" , &n , &m ) ;solve () ;return 0 ;
}
这篇关于【BZOJ】1324 Exca王者之剑 最大权独立集的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!