传送门:【ZOJ】2332 Gems
题目分析:首先我们设立源点s,汇点t,s向所有宝石建边,容量为题目中给出的,然后所有可行的转换,向两个宝石之间建无向边,容量为INF,接下来所有的宝石向自己相应的类型建边,容量INF,所有的宝石向自己相应的颜色建边,容量INF。最后,所有的类型以及颜色向汇点建边,容量为题目中给出的。最后跑一遍最大流,如果满流,说明所有的宝石都成功的限制条件下分给了男主角以及女主角,输出Yes,否则输出No。
代码如下:
#include
#include
#include
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 ;
}