Address: HDU 4292
Question.
Since each person can only have one copy, you need to split the points to limit the traffic. The method for creating a graph is to create a Source Vertex and a sink vertex, connect the food to the Source Vertex, set the weight to the amount of food, connect the beverage to the sink vertex, and set the weight to the number of drinks .. Split the person into I and I, set the corresponding I and I to 1, and connect I with the corresponding Yes food, connect ''to the yes beverage edge corresponding to it, and find the maximum stream at a time.
The Code is as follows:
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;int head[900], source, sink, nv, cnt;int cur[900], num[900], d[900], pre[900], q[900];struct node{ int u, v, cap, next;} edge[10000000];void add(int u, int v, int cap){ edge[cnt].v=v; edge[cnt].cap=cap; edge[cnt].next=head[u]; head[u]=cnt++; edge[cnt].v=u; edge[cnt].cap=0; edge[cnt].next=head[v]; head[v]=cnt++;}void bfs(){ memset(num,0,sizeof(num)); memset(d,-1,sizeof(d)); int f1=0, f2=0, i; d[sink]=0; num[0]=1; q[f1++]=sink; while(f1>=f2) { int u=q[f2++]; for(i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].v; if(d[v]==-1) { d[v]=d[u]+1; num[d[v]]++; q[f1++]=v; } } }}void isap(){ memcpy(cur,head,sizeof(cur)); bfs(); int flow=0, u=pre[source]=source, i; while(d[source]<nv) { if(u==sink) { int f=INF,pos; for(i=source; i!=sink; i=edge[cur[i]].v) { if(f>edge[cur[i]].cap) { f=edge[cur[i]].cap; pos=i; } } for(i=source; i!=sink; i=edge[cur[i]].v) { edge[cur[i]].cap-=f; edge[cur[i]^1].cap+=f; } flow+=f; u=pos; } for(i=cur[u]; i!=-1; i=edge[i].next) { if(d[edge[i].v]+1==d[u]&&edge[i].cap) { break; } } if(i!=-1) { cur[u]=i; pre[edge[i].v]=u; u=edge[i].v; } else { if(--num[d[u]]==0) break; int mind=nv; for(i=head[u]; i!=-1; i=edge[i].next) { if(mind>d[edge[i].v]&&edge[i].cap) { mind=d[edge[i].v]; cur[u]=i; } } d[u]=mind+1; num[d[u]]++; u=pre[u]; } } printf("%d\n",flow);}int main(){ int n, f, d, i, j, x; char s[300]; while(scanf("%d%d%d",&n,&f,&d)!=EOF) { memset(head,-1,sizeof(head)); source=0; cnt=0; sink=2*n+f+d+1; nv=sink+1; for(i=1; i<=f; i++) { scanf("%d",&x); add(source,i,x); } for(i=1; i<=d; i++) { scanf("%d",&x); add(i+2*n+f,sink,x); } for(i=1; i<=n; i++) { add(i+f,i+f+n,1); } for(i=1; i<=n; i++) { scanf("%s",s); for(j=0; j<f; j++) { if(s[j]=='Y') { add(j+1,f+i,INF); } } } for(i=1; i<=n; i++) { scanf("%s",s); for(j=0; j<d; j++) { if(s[j]=='Y') { add(i+n+f,j+1+2*n+f,INF); } } } isap(); } return 0;}