标签:eal sample mem code 分享 org ios start decide
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16352 | Accepted: 7307 |
Description
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others.
Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not be able to stuff everybody, he wants to give a complete meal of both food and drink to as many cows as possible.
Farmer John has cooked F (1 ≤ F ≤ 100) types of foods and prepared D (1 ≤ D ≤ 100) types of drinks. Each of his N (1 ≤ N ≤ 100) cows has decided whether she is willing to eat a particular food or drink a particular drink. Farmer John must assign a food type and a drink type to each cow to maximize the number of cows who get both.
Each dish or drink can only be consumed by one cow (i.e., once food type 2 is assigned to a cow, no other cow can be assigned food type 2).
Input
Output
Sample Input
4 3 3 2 2 1 2 3 1 2 2 2 3 1 2 2 2 1 3 1 2 2 1 1 3 3
Sample Output
3
Hint
Source
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int N=105,INF=1e9; int read(){ char c=getchar();int x=0,f=1; while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1; c=getchar();} while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘; c=getchar();} return x*f; } int n,F,D,s,t,fi,di,v; struct edge{ int v,ne,c,f; }e[N*N*3]; int h[N*4],cnt=0; inline void ins(int u,int v,int c){ cnt++; e[cnt].v=v;e[cnt].c=c;e[cnt].f=0;e[cnt].ne=h[u];h[u]=cnt; cnt++; e[cnt].v=u;e[cnt].c=0;e[cnt].f=0;e[cnt].ne=h[v];h[v]=cnt; } int cur[N*4]; int vis[N*4],d[N*4],q[N*4],head,tail; bool bfs(){ memset(vis,0,sizeof(vis)); memset(d,0,sizeof(d)); head=tail=1; q[tail++]=s;d[s]=0;vis[s]=1; while(head!=tail){ int u=q[head++]; for(int i=h[u];i;i=e[i].ne){ int v=e[i].v; if(!vis[v]&&e[i].f<e[i].c){ q[tail++]=v;vis[v]=1; d[v]=d[u]+1; if(v==t) return 1; } } } return 0; } int dfs(int u,int a){ if(u==t||a==0) return a; int flow=0,f; for(int &i=cur[u];i;i=e[i].ne){ int v=e[i].v; if(d[v]==d[u]+1&&(f=dfs(v,min(a,e[i].c-e[i].f)))>0){ flow+=f; e[i].f+=f; e[((i-1)^1)+1].f-=f; a-=f; if(a==0) break; } } return flow; } int dinic(){ int flow=0; while(bfs()){ for(int i=s;i<=t;i++) cur[i]=h[i]; flow+=dfs(s,INF); } return flow; } int main(){ n=read();F=read();D=read(); s=0;t=F+n+n+D+1; for(int i=1;i<=F;i++) ins(s,i,1); for(int i=1;i<=D;i++) ins(F+n+n+i,t,1); for(int i=1;i<=n;i++){ ins(F+i,F+n+i,1); fi=read();di=read(); while(fi--){v=read();ins(v,F+i,1);} while(di--){v=read();ins(F+n+i,F+n+n+v,1);} } printf("%d",dinic()); }
标签:eal sample mem code 分享 org ios start decide
原文地址:http://www.cnblogs.com/candy99/p/6102879.html