博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POJ3281] Dining
阅读量:4686 次
发布时间:2019-06-09

本文共 3858 字,大约阅读时间需要 12 分钟。

[POJ3281] Dining

标签 :网络流 POJ


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

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
Hint
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.


题解

  • 此题很像二分图匹配的模型。但是我们发现如果将牛与食物、饮料分别连边的话肯定不行。因为只有一只牛同时有了食物和饮料,他才能对答案做出1的贡献。因此,我们考虑网络流的常用技巧——”拆点"
  • 把一头牛拆成入点和出点,再将食物向入连一条容量为1的弧,出点向饮料连一条容量为1的弧,再将源点向食物相连,饮料想汇点相连。最后跑一遍最大流就行了。

Code

#include
#include
#include
#include
#include
#include
#include
using namespace std;#define inf 1<<30#define ll long long#define REP(i,a,b) for(int i=(a),_end_=(b);i<=_end_;i++)#define DREP(i,a,b) for(int i=(a),_end_=(b);i>=_end_;i--)#define EREP(i,a) for(int i=start[(a)];i;i=e[i].next)inline int read(){ int sum=0,p=1;char ch=getchar(); while(!(('0'<=ch && ch<='9') || ch=='-'))ch=getchar(); if(ch=='-')p=-1,ch=getchar(); while('0'<=ch && ch<='9')sum=sum*10+ch-48,ch=getchar(); return sum*p;}const int maxn=410;struct node { int v,next,w;};node e[maxn*410];int cnt=1,start[maxn];int n,cn,fn,dn,S,T;void addedge(int u,int v,int w){ e[++cnt]={v,start[u],w}; start[u]=cnt;}void init(){ memset(e,0,sizeof(e)); memset(start,0,sizeof(start)); n=2*cn+fn+dn+1; S=0,T=n; cnt=1; REP(i,1,cn) { int Food_cnt=read(); int Drink_cnt=read(); REP(j,1,Food_cnt) { int Food=read(); addedge(2*i-1,2*cn+Food,0); addedge(2*cn+Food,2*i-1,1); } addedge(2*i-1,2*i,1); addedge(2*i,2*i-1,0); REP(j,1,Drink_cnt) { int Drink=read(); addedge(2*i,2*cn+fn+Drink,1); addedge(2*cn+fn+Drink,2*i,0); } } REP(i,1,fn) { addedge(0,2*cn+i,1); addedge(2*cn+i,0,0); } REP(i,1,dn) { addedge(n,2*cn+fn+i,0); addedge(2*cn+fn+i,n,1); }}int lev[maxn];inline bool bfs(){ memset(lev,0,sizeof(lev)); queue
q; lev[S]=1; q.push(S); do{ int u=q.front();q.pop(); EREP(i,u) { int v=e[i].v; if(!lev[v] && e[i].w) { lev[v]=lev[u]+1; q.push(v); if(v==T)return true; } } }while(!q.empty()); return false;}int ans=0;int cur[maxn];int dfs(int u,int flow){ if(u==T)return flow; int tag=0,c=0; for(int &i=cur[u];i;i=e[i].next) { int v=e[i].v; if(lev[v]==lev[u]+1 && e[i].w) { c=dfs(v,min(flow-tag,e[i].w)); e[i].w-=c; e[i^1].w+=c; tag+=c; if(tag==flow)return tag; } } return tag;}void doing(){ ans=0; while(bfs()) { REP(i,0,n)cur[i]=start[i]; ans+=dfs(S,inf); } cout<
<

转载于:https://www.cnblogs.com/gzy-cjoier/p/7267509.html

你可能感兴趣的文章
Jquery radio选中
查看>>
《Visual C++ 2010入门教程》系列二:安装、配置和首次使用VS2010
查看>>
P1351 联合权值[鬼畜解法]
查看>>
Best Time to Buy and Sell Stock with Cooldown_LeetCode
查看>>
nginx+tomcat反向代理
查看>>
postgressql数据库中limit offset使用
查看>>
linux下如何添加一个用户并且让用户获得root权限
查看>>
搭建Extjs框架(二)
查看>>
NSDateFormatter的说明
查看>>
测试思想-集成测试 关于接口测试 Part 2
查看>>
测试思想-测试设计 史上最详细测试用例设计实践总结 Part1
查看>>
windows下mysql密码忘了怎么办?【转】
查看>>
java文件上传和下载
查看>>
SQL联合查询(内联、左联、右联、全联)的语法(转)
查看>>
枚举和实用类
查看>>
python基础知识第二篇(字符串)
查看>>
php生成器使用总结
查看>>
android studio 导入第三方库的记录
查看>>
T-SQL中的indexof函数
查看>>
javascript基础之数组(Array)对象
查看>>