传送门:【HDU】4292 Food
题目分析:建立超级源汇,源点和食物建边,汇点和饮料建边,食物和人建边,饮料和人都建边,由于每个人只接受一份食物和饮料,因此将人拆成两个点,建边容量为1限制。
代码如下:
#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 int type_c ; const int MAXN = 805 ; const int MAXQ = 805 ; const int MAXE = 200004 ; 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] , num[MAXN] , pre[MAXN] , cur[MAXN] ; int Q[MAXQ] , head , tail ; int s ,t , nv ; type_c flow ; int N , F , D ; void init () { cntE = 0 ; CLR ( H , -1 ) ; } void addedge ( int u , int v , type_c c ) { E[cntE] = Edge ( v , c , H[u] ) ; H[u] = cntE ++ ; E[cntE] = Edge ( u , 0 , 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] ) continue ; d[v] = d[u] + 1 ; num[d[v]] ++ ; Q[tail ++] = 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 ; mmin = nv ; for ( 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 read ( int &x ) { char c ; do { c = getchar () ; } while ( c < '0' || c > '9' ) ; x = c - '0' ; while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ; } void solve () { int x ; int FD = F + D ; int NFD = N + FD ; s = N + NFD ; t = s + 1 ; nv = t + 1 ; init () ; REP ( i , 0 , N ) addedge ( FD + i , NFD + i , 1 ) ; REP ( i , 0 , F ) { read ( x ) ; addedge ( s , i , x ) ; } REP ( i , 0 , D ) { read ( x ) ; addedge ( i + F , t , x ) ; } REP ( i , 0 , N ) { REP ( j , 0 , F ) if ( getchar () == 'Y' ) addedge ( j , FD + i , 1 ) ; getchar () ; } REP ( i , 0 , N ) { REP ( j , 0 , D ) if ( getchar () == 'Y' ) addedge ( NFD + i , j + F , 1 ) ; getchar () ; } printf ( "%d\n" , ISAP () ) ; } } e ; int main () { while ( ~scanf ( "%d%d%d" , &e.N , &e.F , &e.D ) ) e.solve () ; return 0 ; }