POJ
来源:互联网 发布:软件行业就业形势 编辑:程序博客网 时间:2024/06/07 08:34
Dining
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
Line 1: Three space-separated integers: N, F, and D
Lines 2.. N+1: Each line i starts with a two integers Fi and Di, the number of dishes that cow i likes and the number of drinks that cow i likes. The next Fi integers denote the dishes that cow i will eat, and the Di integers following that denote the drinks that cow i will drink.
Output
Line 1: A single integer that is the maximum number of cows that can be fed both food and drink that conform to their wishes
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
题意:有N头牛,F种食物,D种饮料,每一头牛都有自己喜欢的食物和饮料,且每一种食物和饮料都只有一份,让你分配这些食物和饮料,问能使多少头牛同时获得自己喜欢的食物和饮料。
思路:只考虑一种食物的时候就非常容易,直接二分图最大匹配。而除了匈牙利算法外,网络流中的最大流算法也可以解决二分图匹配问题(通过添加s和t),所以我们可以尝试类似的方法建图求最大流。如果单单添加s和t一头羊可能会对答案贡献超过一(cow可能有多个喜欢的食物和饮料),我们可以在cow自己之间建一条容量为1的边,然后就可以套模板了。
#include <iostream>#include <fstream>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#include <cmath>#include <algorithm>#include <functional>#define inf 0X3f3f3f3fusing namespace std;typedef long long ll;const int MAXN=1e5+10;const int MAX=500+10;const double eps=1e-6;int F,D,N;struct EDGE{ int v,c,rev;};vector<EDGE>G[MAX];bool vis[MAX];void init(){ for(int i=1;i<=MAX;i++) G[i].clear();} void addedge(int u,int v,int c){ G[u].push_back((EDGE){v,c,G[v].size()}); G[v].push_back((EDGE){u,0,G[u].size()-1});}int dfs(int v,int t,int f){ if(v==t) return f; vis[v]=1; for(int i=0;i<G[v].size();i++){ EDGE &e=G[v][i]; if(!vis[e.v]&&e.c>0){ int d=dfs(e.v,t,min(f,e.c)); if(d>0){ e.c-=d; G[e.v][e.rev].c+=d; return d; } } } return 0;}int max_flow(int s,int t){ int flow=0; while(1){ memset(vis,0,sizeof(vis)); int f=dfs(s,t,inf); if(!f) return flow; flow+=f; }}int main(){ #ifdef ONLINE_JUDGE #else freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); #endif while(cin>>N>>F>>D){ init(); int s=N*2+F+D+1; int t=s+1; for(int i=1;i<=F;i++) addedge(s,2*N+i,1); for(int i=1;i<=D;i++) addedge(2*N+F+i,t,1); for(int i=1;i<=N;i++) addedge(i,N+i,1); int x,y,T; for(int i=1;i<=N;i++){ cin>>x>>y; for(int j=1;j<=x;j++){ scanf("%d",&T); addedge(2*N+T,i,1); } for(int j=1;j<=y;j++){ scanf("%d",&T); addedge(N+i,2*N+F+T,1); } } cout<<max_flow(s,t)<<endl; } return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- HDU 6127 Hard challenge【极角排序】
- 数据结构当中的图怎么都弄不懂怎么办?
- Power of Matrix UVA
- 【LeetCode】Contains Duplicate II 解题报告
- 2017.08.18【NOIP提高组】模拟赛B组总结
- POJ
- 归并排序
- 这是我刚接触博客的第一个测试博客
- 链表、头指针、头结点
- 【jzoj5306】【NOIP2017提高A组模拟8.18】【棋盘游戏】
- hdu 6143-动态规划
- [jzoj5307]【NOIP2017提高A组模拟8.18】偷窃
- Android模块化开发
- 保持对象成员私有