[USACO07OPEN]吃饭Dining
来源:互联网 发布:navicat连接rds数据库 编辑:程序博客网 时间:2024/05/02 02:26
[USACO07OPEN]吃饭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).
有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料。现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮料。(1 <= f <= 100, 1 <= d <= 100, 1 <= n <= 100)
输入格式:
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.
输出格式:
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
输入样例#1:
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
输出样例#1:
3
说明
One way to satisfy three cows is:
Cow 1: no meal
Cow 2: Food #2, Drink #2
Cow 3: Food #1, Drink #1
Cow 4: Food #3, Drink #3
The pigeon-hole principle tells us we can do no better since there are only three kinds of food or drink. Other test data sets are more challenging, of course.
题解:
为了控制两个条件都满足,将人进行拆点放中间,向两个条件连边即可。
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<queue>using namespace std;const int max_n = 3100;const int max_l = 31000;const int inf = 1e9+7;int point[max_n],nxt[max_l],v[max_l],remain[max_l];int cur[max_n],deep[max_n];int n,f,d,a,b,x,s,t,ans,tot;inline void init(){ memset(point,-1,sizeof(point)); memset(nxt,-1,sizeof(nxt)); tot=-1; t=2*n+f+d+1;}inline void addedge(int x,int y,int val){ ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=val; ++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;}inline bool bfs(int s,int t){ memset(deep,0x7f,sizeof(deep)); for(int i=s; i<=t; ++i) cur[i]=point[i]; queue<int> q; q.push(s); deep[s]=0; while(!q.empty()) { int now=q.front(); q.pop(); for(int i=point[now]; i!=-1; i=nxt[i]) if(deep[v[i]]>inf && remain[i]) { deep[v[i]]=deep[now]+1; q.push(v[i]); } } return deep[t]<inf;}inline int dfs(int now,int t,int limit){ if(now==t || !limit) return limit; int f,flow=0; for(int i=cur[now]; i!=-1; i=nxt[i]) { cur[now]=i; if(deep[v[i]]==deep[now]+1 && (f=dfs(v[i],t,min(limit,remain[i])))) { flow+=f; limit-=f; remain[i]-=f; remain[i^1]+=f; if(!limit) break; } } return flow;}inline int dinic(int s,int t){ int ans=0; while(bfs(s,t)) ans+=dfs(s,t,inf); return ans;}int main(){ scanf("%d%d%d",&n,&f,&d); init(); for(int i=1; i<=f; ++i) addedge(s,i,1); for(int i=2*n+f+1; i<=2*n+f+d; ++i) addedge(i,t,1); for(int i=1; i<=n; ++i) { addedge(f+i,f+n+i,1); scanf("%d%d",&a,&b); for(int j=1; j<=a; ++j) { scanf("%d",&x); addedge(x,f+i,1); } for(int j=1; j<=b; ++j) { scanf("%d",&x); addedge(f+n+i,2*n+f+x,1); } } printf("%d",dinic(s,t)); return 0;}
- [USACO07OPEN]吃饭Dining
- 【bzoj1711】[Usaco2007 Open]Dining吃饭
- 1711: [Usaco2007 Open]Dining吃饭
- bzoj1711 [Usaco2007 Open]Dining吃饭
- BZOJ1711: [Usaco2007 Open]Dining吃饭
- [BZOJ1711]Dining 吃饭 最大流
- 【BZOJ1711】[Usaco2007 Open]Dining吃饭【最大流】
- BZOJ1711 [Usaco2007 Open]Dining吃饭 题解&代码
- BZOJ 1711 Dining 吃饭 网络流
- bzoj1711 [Usaco2007 Open]Dining吃饭 最大流
- [bzoj1711][Usaco2007 Open]Dining吃饭 最大流
- 网络流 模板【Usaco2007 Open】bzoj1711 Dining吃饭
- [BZOJ1711][Usaco2007 Open]Dining吃饭(最大流)
- bzoj 1711: [Usaco2007 Open]Dining吃饭 (最大流)
- BZOJ1711 [ Usaco2007 Open ]Dining吃饭 拆点最大流
- BZOJ 1711: [Usaco2007 Open]Dining吃饭 最大流
- bzoj 1711: [Usaco2007 Open]Dining吃饭 网络流
- [最大流]BZOJ 1711: [Usaco2007 Open]Dining吃饭 题解
- 特征缩放
- HDU1087
- BeautifulSoup学习笔记6
- 【GDOI2018模拟7.12】C
- mysql基本sql语句总结(二)
- [USACO07OPEN]吃饭Dining
- java中的字符,字符串,数字之间的转换
- kd树建立的python实现
- 对象基础
- linux服务器开发二(系统编程)--进程相关
- 简单的RelativeLayout 相对布局 及其常用属性
- 设置Edittext 输入带有小数点类型的输入规则
- App奔溃日志收集
- ASP.NET网站管理工具的【安全】功能无法使用问题