和poj3281完全一样的题。都是人(牛)拆成两个点跑一发最大流。
蓝而在套模板的时候原模板有一个地方没改过来,整整交了一个小时的wa。那么猜一共交了多少发wa- -
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define ls rt << 1
#define rs rt << 1 | 1
#define pi acos(-1.0)
#define eps 1e-8
#define asd puts("sdfsdfsdfsdfsdfsdf");
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int N = 2010;
const int st = 0;
struct node{
int v, w, nxt;
}e[88888888];
int head[N];
int cur[N];
int gap[N];
int s[N], top;
int dep[N];
int n, F, D, cnt, ed;
queue <int> q;
void init()
{
cnt = 0;
memset( head, -1, sizeof( head ) );
}
void add( int u, int v, int w )
{
e[cnt].v = v;
e[cnt].w = w;
e[cnt].nxt = head[u];
head[u] = cnt++;
e[cnt].v = u;
e[cnt].w = 0;
e[cnt].nxt = head[v];
head[v] = cnt++;
}
void rev_bfs()
{
while( !q.empty() ) q.pop();
memset( gap, 0, sizeof( gap ) );
memset( dep, -1, sizeof( dep ) );
q.push( ed );
dep[ed] = 0;
gap[0] = 1;
while( !q.empty() ) {
int u = q.front();
q.pop();
for( int i = head[u]; ~i; i = e[i].nxt ) {
int v = e[i].v;
if( ~dep[v] )
continue;
q.push(v);
dep[v] = dep[u] + 1;
gap[dep[v]]++;
}
}
}
int isap()
{
int flow = 0, u = st, i;
memcpy( cur, head, sizeof head );
rev_bfs();
top = 0;
int nv = ed + 2;
while( dep[st] < nv ) {
if( u == ed ) {
int tmp, minn = inf;
for( i = 0; i < top; ++i ) {
if( minn > e[s[i]].w ) {
minn = e[s[i]].w;
tmp = i;
}
}
for( i = 0; i < top; ++i ) {
e[s[i]].w -= minn;
e[s[i]^1].w += minn;
}
flow += minn;
top = tmp;
u = e[s[top]^1].v;
}
for( i = cur[u]; ~i; i = e[i].nxt ) {
if( e[i].w > 0 && dep[u] == dep[e[i].v] + 1 ) {
cur[u] = i;
break;
}
}
if( ~i ) {
s[top++] = cur[u];
u = e[i].v;
}
else {
if( 0 == (--gap[dep[u]]) )
break;
int minn = nv;
for( i = head[u]; ~i; i = e[i].nxt ) {
int v = e[i].v;
if( e[i].w > 0 && minn > dep[v] ) {
minn = dep[v];
cur[u] = i;
}
}
dep[u] = minn + 1;
gap[dep[u]]++;
if( u != st ) {
u = e[ s[--top]^1 ].v;
}
}
}
return flow;
}
int main()
{
while( ~scanf("%d%d%d", &n, &F, &D) ) {
init();
ed = n*2+F+D+1;
int x;
char a[N];
for( int i = 1; i <= F; ++i ) {
scanf("%d", &x);
if( x == 0 )
continue;
add( st, i, x );
}
for( int i = 1; i <= D; ++i ) {
scanf("%d", &x);
if( x == 0 )
continue;
add( 2*n+F+i, ed, x );
}
for( int i = 1; i <= n; ++i ) {
scanf("%s", a+1);
for( int j = 1; j <= F; ++j )
if( a[j] == 'Y' )
add( j, F+i, 1 );
}
for( int i = 1; i <= n; ++i ) {
scanf("%s", a+1);
for( int j = 1; j <= D; ++j )
if( a[j] == 'Y' )
add( F+n+i, F+2*n+j, 1 );
}
for( int i = 1; i <= n; ++i )
add( F+i, F+i+n, 1 );
int ans = isap();
printf("%d\n", ans);
}
return 0;
}