【POJ】3084 Panic Room 最小割

传送门:【POJ】3084 Panic Room


题目分析:POJ今天的C++逗我玩呢。。G++AC,C++RE。。。

对于终点,和汇点建边,容量INF,对所有有入侵者的房间和源点建边,容量INF。

对所有房间A可以绝对进入房间B的关系,建边(A,B,INF),(B,A,1)。

这样如果最终答案为INF,说明无论怎么上锁,入侵者都能达到终点,否则最小割就是答案。


代码如下:


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

const int MAXN = 25 ;
const int MAXQ = 25 ;
const int MAXE = 1000000 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
	int v , n ;
	type_c c ;
	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_c flow ;
	
	int n , ed ;
	int G[25][25] ;
	
	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_c ISAP () {
		CPY ( cur , H ) ;
		rev_bfs () ;
		flow = 0 ;
		int u = pre[s] = s , i , pos , mmin ;
		while ( d[s] < nv ) {
			if ( u == t ) {
				type_c 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 () {
		char str[5] ;
		int x , v ;
		CLR ( G , 0 ) ;
		init () ;
		scanf ( "%d%d" , &n , &ed ) ;
		s = n ;
		t = s + 1 ;
		nv = t + 1 ;
		addedge ( ed , t , INF ) ;
		REP ( i , 0 , n ) {
			scanf ( "%s%d" , str , &x ) ;
			if ( str[0] == 'I' )
				addedge ( s , i , INF ) ;
			while ( x -- ) {
				scanf ( "%d" , &v ) ;
				++ G[i][v] ;
			}
		}
		REP ( i , 0 , n )
			REP ( j , 0 , n )
				if ( G[i][j] )
					addedge ( i , j , INF , G[i][j] ) ;
		ISAP () ;
		if ( flow == INF )
			printf ( "PANIC ROOM BREACH\n" ) ;
		else
			printf ( "%d\n" , flow ) ;
	}
} 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、付费专栏及课程。

余额充值