【HDU】3996 Gold Mine 最大权闭合子图

传送门:【HDU】3996 Gold Mine


题目分析:最大权闭合子图模板题。

说一下坑爹的输入描述。

第一行T组样例。

接下来每组数据第一行一个n,表示有n层金矿。

每层第一行一个数x,表示该层有多少块金子。

接下来每块金子(编号从1~x)三个数cost,val,w,表示挖取这块金子花费cost,得到val,依赖于w块金子。

接下来w行每行两个数a,b,表示这块金子依赖于a层的编号为b的金子。

挖取每块金子必须先将他所依赖的金子先挖取。

会爆int。


代码如下:


#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 ;

const int MAXN = 2600 ;
const int MAXQ = 2600 ;
const int MAXE = 500005 ;
const LL INF = 1e15 ;

struct Edge {
	int v , n ;
	LL c ;
	Edge () {}
	Edge ( int v , LL 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 ;
	LL flow ;
	
	void init () {
		cntE = 0 ;
		CLR ( H , -1 ) ;
	}
	
	void addedge ( int u , int v , LL 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 ;
			}
		}
	}
	
	LL ISAP () {
		CPY ( cur , H ) ;
		rev_bfs () ;
		flow = 0 ;
		int u = pre[s] = s , i ;
		while ( d[s] < nv ) {
			if ( u == t ) {
				LL f = INF ;
				int 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 x , c , w , p , a , b ;
		LL sum = 0 ;
		init () ;
		scanf ( "%d" , &n ) ;
		s = 25 * n ;
		t = s + 1 ;
		nv = t + 1 ;
		REP ( i , 0 , n ) {
			scanf ( "%d" , &x ) ;
			REP ( j , 0 , x ) {
				int ij = i * 25 + j ;
				scanf ( "%d%d%d" , &c , &w , &p ) ;
				if ( w - c > 0 )
					addedge ( s , ij , ( LL ) w - c ) , sum += w - c ;
				if ( w - c < 0 )
					addedge ( ij , t , ( LL ) c - w ) ;
				while ( p -- ) {
					scanf ( "%d%d" , &a , &b ) ;
					addedge ( ij , ( a - 1 ) * 25 + b - 1 , INF ) ;
				}
			}
		}
		printf ( "%I64d\n" , sum - ISAP () ) ;
	}
} x ;

int main () {
	int T , cas = 0 ;
	scanf ( "%d" , &T ) ;
	while ( T -- ) {
		printf ( "Case #%d: " , ++ cas ) ;
		x.solve () ;
	}
	return 0 ;
}


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值