博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3281 Dining (网络流)
阅读量:5346 次
发布时间:2019-06-15

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

POJ 3281 Dining (网络流)

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

Http

POJ:

Source

网络流

题目大意

有n只牛,F种食物,D种饮料,每一只牛喜欢若干食物和饮料。现在要给每一只牛分配食物和饮料,每一种食物或饮料只够一只牛吃。若一只牛吃到了它喜欢的食物和饮料(注意是两个都要符合),它就会很开心。现在求最多能使多少只牛开心

解决思路

这道题与Luogu1402有些类似,但不知道为什么我用二分图的方法会WrongAnswer。所以我们用网络流最大流来解决。

对于每一只牛i我们把其拆成两个点i和i+n,在i与i+n中连一条流量为1的边。这样是为了保证一只牛只吃到一种食物和一种饮料。再在对应的食物与牛i连容量为1的边,在对应的牛与饮料连容量为1的边。最后再连上超级源点和超级汇点就可以了。
虽然笔者是使用EK算法实现的网络流最大流,但是更推荐效率更优的Dinic算法,具体请移步我的

代码

EK算法

#include
#include
#include
#include
#include
#include
using namespace std;const int maxN=510;const int maxM=2147483647;const int inf=2147483647;int n,F,D;int G[maxN][maxN];int Flow[maxN];int Path[maxN];bool bfs();int main(){ memset(G,0,sizeof(G)); scanf("%d%d%d",&n,&F,&D); for (int i=1;i<=n;i++)//先在拆出来的牛点之间连边 G[i][i+n]=1; for (int i=1;i<=n;i++) { int n1,n2; scanf("%d%d",&n1,&n2); for (int j=1;j<=n1;j++) { int v; scanf("%d",&v); G[2*n+v][i]=1;//连牛与食物 } for (int j=1;j<=n2;j++) { int v; scanf("%d",&v); G[i+n][2*n+F+v]=1;//连牛与饮料 } } for (int i=1;i<=F;i++) G[0][n*2+i]=1;//连源点 for (int i=1;i<=D;i++) G[n*2+F+i][n*2+F+D+1]=1;//连汇点 int Ans=0; while (bfs())//EK算法 { int di=Flow[n*2+F+D+1]; int now=n*2+F+D+1; int last=Path[now]; while (now!=0) { G[last][now]-=di; G[now][last]+=di; now=last; last=Path[now]; } Ans++; } cout<
<
Q; while (!Q.empty()) Q.pop(); Q.push(0); do { int u=Q.front(); Q.pop(); for (int i=0;i<=2*n+F+D+1;i++) { if ((Path[i]==-1)&&(G[u][i]>0)) { Path[i]=u; Q.push(i); Flow[i]=min(Flow[u],G[u][i]); } } } while (!Q.empty()); if (Flow[n*2+F+D+1]==0) return 0; return 1;}

转载于:https://www.cnblogs.com/SYCstudio/p/7256116.html

你可能感兴趣的文章
response和request
查看>>
【转】在Eclipse中安装和使用TFS插件
查看>>
回到顶部浮窗设计
查看>>
C#中Monitor和Lock以及区别
查看>>
【NOIP2017】奶酪
查看>>
$ 一步一步学Matlab(3)——Matlab中的数据类型
查看>>
5.6.3.7 localeCompare() 方法
查看>>
Linux下好用的简单实用命令
查看>>
常用web字体的使用指南
查看>>
描绘应用程序级的信息
查看>>
poj2406-Power Strings
查看>>
2018/12/18 JS会像Linux一样改变编程
查看>>
php环境搭建脚本
查看>>
FTP主动模式与被动模式说明
查看>>
php 编译常见错误
查看>>
MES架构
查看>>
【Python3 爬虫】15_Fiddler抓包分析
查看>>
高性能JavaScript-JS脚本加载与执行对性能的影响
查看>>
关于标签之间因为换行等问题造成的空白间距问题处理
查看>>
hdu 2767(tarjan)
查看>>