//2012成都网赛E题 food
思路: 题目一看就知道是网络流啦。题目要求一个人既要得到一份喜欢的food有要有一份喜欢的drink,要处理其实也很简单,将一个人拆成两个点,如A,A‘,利用A‘来限流,假如food流到A的流量是3,我们连一条A到A’的边,边的权为1,这样就将流量限制成为1了。 网络流很多地方都可以多建立几个点。。。本菜太弱,建议弱友们可以在前期多多练习建图,至于算法 网上模板多多,选择一个速度比较快的熟练使用......
#include<stdio.h>
#include<string.h>
#include <string>
#include <cmath>
#include <iostream>
#include <map>
#include<vector>
#include<queue>
#include<algorithm>
#define fr(i,s,n) for(int i=s;i<n;i++)
#define pf printf
#define sf scanf
#define sfv1(a) scanf("%d",&a)
#define sfv2(n,m) scanf("%d%d",&n,&m)
#define sfv3(u,v,w) scanf("%d%d%d",&u,&v,&w)
#define sfstr(c) scanf("%s",c)
#define pfv1(a) printf("%d\n",a)
#define fi freopen("in.txt","r",stdin)
#define fo freopen("out.txt","w",stdout)
#define cl(a) memset(a,0,sizeof(a))
#define me(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 2005;
const int maxm = 300000;
const int inf = 1e9;
int m;
int gap[maxn],arc[maxn],cf[maxn],dis[maxn],pre[maxn];
int po[maxn],tol;
struct node{
int y,f,next;
}edge[maxm];
int sap(int n,int s,int t){
memset(dis,0,sizeof(dis));
memset(gap,0,sizeof(gap));
gap[0] = n;
for(int i=1;i<=n;i++)
arc[i] = po[i];
int MIN;
int ans = 0;
bool ok;
int aug = inf;
int i = s;
while(dis[s]<n){
cf[i] = aug;
ok = false;
for(int j=arc[i];j;j=edge[j].next){
if(edge[j].f>0&&dis[edge[j].y]+1==dis[i]){
ok = true;
if(aug>edge[j].f)
aug = edge[j].f;
arc[i] = j;
i = edge[j].y;
pre[i] = j;
if(i==t){
ans += aug;
for(;i!=s;i=edge[pre[i]^1].y){
edge[pre[i]].f -= aug;
edge[pre[i]^1].f += aug;
}
aug = inf;
}
break;
}
}
if(ok)
continue;
MIN = n-1;
for(int j=po[i];j;j=edge[j].next)
if(edge[j].f>0&&dis[edge[j].y]<MIN){
MIN = dis[edge[j].y];
arc[i] = j;
}
if(--gap[dis[i]]==0)
break;
dis[i] = MIN+1;
gap[dis[i]]++;
if(i!=s){
i = edge[pre[i]^1].y;
aug = cf[i];
}
}
return ans;
}
void add(int x,int y,int f){
edge[++tol].y = y;
edge[tol].f = f;
edge[tol].next = po[x];
po[x] = tol;
edge[++tol].y = x;
edge[tol].f = 0;
edge[tol].next = po[y];
po[y] = tol;
}
void init(){
memset(po,0,sizeof(po));
tol = 1;
}
int main(){
int x;
char c[200];
int N,F,D;
while(sfv3(N,F,D)!=EOF){
init();
int goal=2*N+F+D+1;
fr(i,1,F+1){
sfv1(x);
add(0,2*N+i,x);
}
fr(i,1,D+1){
sfv1(x);
add(2*N+F+i,goal,x);
}
fr(i,1,N+1){
add(i,i+N,1);
}
fr(i,1,N+1){
sfstr(c);
fr(j,0,strlen(c)){
if (c[j]=='Y'){
add(2*N+j+1,i,1);
}
}
}
fr(i,1,N+1){
sfstr(c);
fr(j,0,strlen(c)){
if (c[j]=='Y'){
add(N+i,2*N+F+j+1,1);
}
}
}
int ans=0;
ans=sap(goal+1,0,goal);
pfv1(ans);
}
return 0;
}