很裸的一道最大流
格式懒得排了,注意把人拆成两份,一份连接食物,一份连接饮料
4 3 3 //4个人,3种食物,3种饮料
1 1 1 //食物每种分别为1
1 1 1 //饮料每种数目分别为1
YYN //第一个人对第1,2,3种食物的态度为接受,接受和拒绝
NYY
YNY
YNY
YNY //第一个人对第1,2,3种饮料的态度为接受,拒绝和接受
YYN
YYN
NNY
3
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=;
const int MAXN = ;//点数的最大值
const int MAXM = ;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
void init()
{
tol = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w,int rw = )
{
edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = ;
edge[tol].next = head[u]; head[u] = tol++;
edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = ;
edge[tol].next = head[v]; head[v] = tol++;
}
int Q[MAXN];
void BFS(int start,int end)
{
memset(dep,-,sizeof(dep));
memset(gap,,sizeof(gap));
gap[] = ;
int front = , rear = ;
dep[end] = ;
Q[rear++] = end;
while(front != rear)
{
int u = Q[front++];
for(int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if(dep[v] != -)continue;
Q[rear++] = v;
dep[v] = dep[u] + ;
gap[dep[v]]++;
}
}
}
int S[MAXN];
int sap(int start,int end,int N)
{
BFS(start,end);
memcpy(cur,head,sizeof(head));
int top = ;
int u = start;
int ans = ;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
int inser;
for(int i = ;i < top;i++)
if(Min > edge[S[i]].cap - edge[S[i]].flow)
{
Min = edge[S[i]].cap - edge[S[i]].flow;
inser = i;
}
for(int i = ;i < top;i++)
{
edge[S[i]].flow += Min;
edge[S[i]^].flow -= Min;
}
ans += Min;
top = inser;
u = edge[S[top]^].to;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -; i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v]+ == dep[u])
{
flag = true;
cur[u] = i;
break;
}
}
if(flag)
{
S[top++] = cur[u];
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -; i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(!gap[dep[u]])return ans;
dep[u] = Min + ;
gap[dep[u]]++;
if(u != start)u = edge[S[--top]^].to;
}
return ans;
}
int g[][];
int main()
{
int i,j,k;
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
#endif
int n,d,f;
while(scanf("%d%d%d",&n,&f,&d)!=EOF)
{
init();
memset(g,,sizeof(g));
int start=;
int end=f+d+*n+;
for(i=;i<=f;i++)
{
scanf("%d",&g[][i]);
addedge(,i,g[][i]);
}
for(i=f+*n+;i<=f+*n+d;i++)
{
scanf("%d",&g[i][]);
addedge(i,end,g[i][]);
}
for(i=;i<=n;i++)
{
addedge(f+i*-,f+i*,);
}
char s[];
for(i=;i<=n;i++)
{
scanf("%s",s);
for(int j=;j<f;j++)
{
if(s[j]=='Y')
{
addedge(j+,f+*i-,);
}
}
}
for(i=;i<=n;i++)
{
scanf("%s",s);
for(int j=;j<d;j++)
{
if(s[j]=='Y')
{
addedge(f+*i,f+*n+j+,);
}
}
}
printf("%d\n",sap(start,end,d+f+*n+));
}
return ;
}