【ZOJ】2332 Gems 最大流——判断满流

传送门:【ZOJ】2332 Gems


题目分析:首先我们设立源点s,汇点t,s向所有宝石建边,容量为题目中给出的,然后所有可行的转换,向两个宝石之间建无向边,容量为INF,接下来所有的宝石向自己相应的类型建边,容量INF,所有的宝石向自己相应的颜色建边,容量INF。最后,所有的类型以及颜色向汇点建边,容量为题目中给出的。最后跑一遍最大流,如果满流,说明所有的宝石都成功的限制条件下分给了男主角以及女主角,输出Yes,否则输出No。


代码如下:


#include <cstdio>
#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 )

typedef int type_c ;
typedef int type_f ;

const int MAXN = 128 ;
const int MAXQ = 128 ;
const int MAXE = 1024 ;
const int INF = 0x3f3f3f3f ;

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

struct Net {
	Edge E[MAXE] ;
	int H[MAXN] , cntE ;
	int d[MAXN] , num[MAXN] , pre[MAXN] , cur[MAXN] ;
	int Q[MAXQ] , head , tail ;
	int s , t , nv ;
	type_f flow ;
	
	int n , m , q ;
	
	void init () {
		cntE = 0 ;
		CLR ( H , -1 ) ;
	}
	
	void addedge ( int u , int v , type_c c , type_c rc = 0 ) {
		E[cntE] = Edge ( u , v ,  c , H[u] ) ;
		H[u] = cntE ++ ;
		E[cntE] = Edge ( v , u , rc , 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] == -1 ) {
					d[v] = d[u] + 1 ;
					Q[tail ++] = v ;
					num[d[v]] ++ ;
				}
			}
		}
	}
	
	type_f ISAP () {
		CPY ( cur , H ) ;
		rev_bfs () ;
		flow = 0 ;
		int u = pre[s] = s , i , pos , mmin ;
		while ( d[s] < nv ) {
			if ( u == t ) {
				type_f 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 == -- num[d[u]] )
					break ;
				for ( mmin = nv , i = H[u] ; ~i ; i = E[i].n )
					if ( E[i].c && mmin > d[E[i].v] ) {
						mmin = d[E[i].v] ;
						cur[u] = i ;
					}
				d[u] = mmin + 1 ;
				num[d[u]] ++ ;
				u = pre[u] ;
			}
		}
		return flow ;
	}
	
	void solve () {
		int r1 , c1 , r2 , c2 , x ;
		scanf ( "%d%d" , &n , &m ) ;
		int nm = n * m , sum = 0 ;
		s = nm + n + m , t = s + 1 , nv = t + 1 ;
		init () ;
		REP ( i , 0 , n ) {
			int tmp = 0 ;
			REP ( j , 0 , m ) {
				scanf ( "%d" , &x ) ;
				addedge ( s , i * m + j , x ) ;
				addedge ( i * m + j , nm + i , INF ) ;
				addedge ( i * m + j , nm + n + j , INF ) ;
				sum += x ;
			}
		}
		scanf ( "%d" , &q ) ;
		while ( q -- ) {
			scanf ( "%d%d%d%d" , &r1 , &c1 , &r2 , &c2 ) ;
			addedge ( r1 * m + c1 , r2 * m + c2 , INF , INF ) ;
		}
		REP ( i , 0 , n ) {
			scanf ( "%d" , &x ) ;
			addedge ( nm + i , t , x ) ;
		}
		REP ( i , 0 , m ) {
			scanf ( "%d" , &x ) ;
			addedge ( nm + n + i , t , x ) ;
		}
		ISAP () ;
		if ( sum == flow )
			printf ( "Yes\n" ) ;
		else
			printf ( "No\n" ) ;
	}
} e ;

int main () {
	int T ;
	scanf ( "%d" , &T ) ;
	while ( T -- )
		e.solve () ;
	return 0 ;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值