[HDU] 3061 배틀의 대권 클로즈업 서브맵

3213 단어 HDU
전송문:[HDU] 3061 Battle
제목 분석: 최대권 폐합자도 =...또 템플릿 문제야, 해 보니 힘이 하나도 없어...
코드는 다음과 같습니다.
#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 )

const int MAXN = 505 ;
const int MAXQ = 505 ;
const int MAXE = 500005 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
	int v , n , c ;
	Edge () {}
	Edge ( int v , int c , int n ) : v ( v ) , c ( c ) , n ( n ) {}
} ;

struct Net {
	Edge E[MAXE] ;
	int H[MAXN] , cntE ;
	int d[MAXN] , num[MAXN] , cur[MAXN] , pre[MAXN] ;
	int Q[MAXQ] , head , tail ;
	int s , t , nv ;
	int n , m ;
	int flow ;
	
	void init () {
		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 ( 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 ;
			}
		}
	}
	
	int ISAP () {
		CPY ( cur , H ) ;
		rev_bfs () ;
		flow = 0 ;
		int u = pre[s] = s , i ;
		while ( d[s] < nv ) {
			if ( u == t ) {
				int f = INF , pos ;
				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 == ( -- num[d[u]] ) )
					break ;
				int mmin = nv ;
				for ( i = H[u] ; ~i ; i = E[i].n )
					if ( E[i].c && mmin > d[E[i].v] ) {
						cur[u] = i ;
						mmin = d[E[i].v] ;
					}
				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 sum = 0 ;
		int u , v , x ;
		init () ;
		s = 0 ;
		t = n + 1 ;
		nv = t + 1 ; 
		FOR ( i , 1 , n ) {
			scanf ( "%d" , &x ) ;
			if ( x > 0 )
				addedge ( s , i , x ) , sum += x ;
			else
				addedge ( i , t , -x ) ;
		}
		REP ( i , 0 , m ) {
			read ( u ) , read ( v ) ;
			addedge ( u , v , INF ) ;
		}
		printf ( "%d
" , sum - ISAP () ) ; } } x ; int main () { while ( ~scanf ( "%d%d" , &x.n , &x.m ) ) x.solve () ; return 0 ; }

좋은 웹페이지 즐겨찾기