现在的位置: 首页 > 综合 > 正文

【POJ】1698 Alice’s Chance 最大流

2017年11月20日 ⁄ 综合 ⁄ 共 2018字 ⁄ 字号 评论关闭

传送门:【POJ】1698 Alice's Chance

题目分析:没想出二分匹配的方法。。偷个懒。。最大流求解,最大流的话就是模板题了。。。

代码如下:

#include <cmath>
#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 ;
typedef int type_c ;
typedef LL type_f ;

const int MAXN = 405 ;
const int MAXQ = 405 ;
const int MAXE = 150000 ;
const int INF = 0x3f3f3f3f ;

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

struct Net {
	Edge E[MAXE] ;
	int H[MAXN] , cntE ;
	int d[MAXN] , cur[MAXN] , pre[MAXN] , num[MAXN] ;
	int Q[MAXQ] , head , tail ;
	int s , t , nv ;
	type_f flow ;
	
	int n , m ;
	bool vis[MAXN] ;
	
	void init () {
		cntE = 0 ;
		CLR ( H , -1 ) ;
	}
	
	void addedge ( int u , int v , type_c c , type_c rc = 0 ) {
		E[cntE] = Edge ( v ,  c , H[u] ) ;
		H[u] = cntE ++ ;
		E[cntE] = Edge ( 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 ) {
					Q[tail ++] = v ;
					d[v] = d[u] + 1 ;
					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 ;
				}
				u = pos ;
				flow += f ;
			}
			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 dfs ( int u ) {
		vis[u] = 1 ;
		for ( int i = H[u] ; ~i ; i = E[i].n )
			if ( !vis[E[i].v] && E[i].c )
				dfs ( E[i].v ) ;
	}
	
	void solve () {
		int day[7] , D , W , sum = 0 ;
		init () ;
		s = 400 , t = s + 1 , nv = t + 1 ;
		scanf ( "%d" , &n ) ;
		REP ( i , 0 , n ) {
			REP ( j , 0 , 7 )
				scanf ( "%d" , &day[j] ) ;
			scanf ( "%d%d" , &D , &W ) ;
			addedge ( s , i + 350 , D ) ;
			sum += D ;
			REP ( j , 0 , W )
				REP ( k , 0 , 7 )
					if ( day[k] )
						addedge ( i + 350 , j * 7 + k , 1 ) ;
		}
		REP ( i , 0 , 350 )
			addedge ( i , t , 1 ) ;
		ISAP () ;
		if ( flow == sum )
			printf ( "Yes\n" ) ;
		else
			printf ( "No\n" ) ;
	}
} e ;

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

抱歉!评论已关闭.