【POJ】2584 T-Shirt Gumbo 最大流

传送门:【POJ】2584 T-Shirt Gumbo


题目分析:可多重匹配,我偷懒了就用最大流了。。。因为一个逗比错误调了一会。。。。一个数还没赋值我就拿来给另一个数赋值去了= =。。


代码如下:


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

const int MAXN = 30 ;
const int MAXQ = 30 ;
const int MAXE = 10000 ;
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 ;
	char str[128] ;
	char shirt[128] ;
	
	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 solve () {
		int x ;
		init () ;
		shirt['S'] = 0 ;
		shirt['M'] = 1 ;
		shirt['L'] = 2 ;
		shirt['X'] = 3 ;
		shirt['T'] = 4 ;
		scanf ( "%d" , &n ) ;
		s = n + 5 , t = s + 1 , nv = t + 1 ;
		REP ( i , 0 , n ) {
			addedge ( s , i , 1 ) ;
			scanf ( "%s" , str ) ;
			FOR ( j , shirt[str[0]] , shirt[str[1]] )
				addedge ( i , n + j , 1 ) ;
		}
		REP ( i , 0 , 5 ) {
			scanf ( "%d" , &x ) ;
			addedge ( n + i , t , x ) ;
		}
		if ( ISAP () == n )
			printf ( "T-shirts rock!\n" ) ;
		else
			printf ( "I'd rather not wear a shirt anyway...\n" ) ;
		scanf ( "%*s" ) ;
	}
} e ;

int main () {
	while ( ~scanf ( "%s" , e.str ) && !strcmp ( e.str , "START" ) )
		e.solve () ;
	return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值