标签:编号 long number lin open enc 代码 main 技术
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18479 | Accepted: 8243 |
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<cmath> #include<cstring> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> #include<set> using namespace std; #define PI acos(-1.0) typedef long long ll; typedef pair<int,int> P; const int maxn=1e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7; const ll INF=1e13+7; priority_queue<P,vector<P>,greater<P> >q; struct edge { int from,to; int cap; int rev;///方向边的编号 }; int n,f,d; vector<edge>G[maxn]; int used[maxn]; void addedge(int u,int v,int c) { edge e; e.from=u,e.to=v,e.cap=c,e.rev=G[v].size(); G[u].push_back(e); e.from=v,e.to=u,e.cap=0,e.rev=G[u].size()-1; G[v].push_back(e); } int dfs(int u,int t,int f) { if(u==t) return f; used[u]=true; for(int i=0; i<G[u].size(); i++) { edge e=G[u][i]; int v=e.to,c=e.cap; if(!used[v]&&c>0) { int d=dfs(v,t,min(f,c)); if(d>0) { G[u][i].cap-=d; G[v][e.rev].cap+=d; return d; } } } return 0; } int max_flow(int s,int t) { int flow=0; while(true) { memset(used,0,sizeof(used)); int f=dfs(s,t,inf); if(f==0) return flow; flow+=f; } } int main() { scanf("%d%d%d",&n,&f,&d); for(int i=0; i<=2*n+f+d+1; i++) G[i].clear(); for(int i=1; i<=n; i++) { int x,fi,di; scanf("%d%d",&fi,&di); for(int j=1; j<=fi; j++) { scanf("%d",&x); addedge(2*n+x,i,1); } for(int j=1; j<=di; j++) { scanf("%d",&x); addedge(n+i,2*n+f+x,1); } } for(int i=1; i<=n; i++) addedge(i,n+i,1); for(int i=1; i<=f; i++) addedge(0,2*n+i,1); for(int i=1; i<=d; i++) addedge(2*n+f+i,2*n+f+d+1,1); cout<<max_flow(0,2*n+f+d+1)<<endl; return 0; }
标签:编号 long number lin open enc 代码 main 技术
原文地址:http://www.cnblogs.com/GeekZRF/p/7246477.html