POJ - 3281 Dining(拆点+最大网络流)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18230 | Accepted: 8132 |
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
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
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
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.
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int INF=1e9;
const int N=+;
int vis[][];
int dis[N];
int head[N];
int a[N],b[N];
int n,f,d;
int tot;
struct node{
int to,next,flow,c;
}edge[N<<];
void init(){
memset(head,-,sizeof(head));
tot=;
}
void add(int u,int v,int c){
edge[tot].to=v;
edge[tot].flow=c;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].to=u;
edge[tot].flow=;
edge[tot].next=head[v];
head[v]=tot++; }
int BFS(int s,int t){
queue<int>q;
memset(dis,-,sizeof(dis));
dis[s]=;
q.push(s);
while(!q.empty()){
int x=q.front();
q.pop();
if(x==t)return ;
for(int i=head[x];i!=-;i=edge[i].next){
int v=edge[i].to;
if(edge[i].flow&&dis[v]==-){
dis[v]=dis[x]+;
q.push(v);
}
}
}
if(dis[t]==-)return ;
return ;
}
int DFS(int s,int flow){
if(s==*n+d+f+)return flow;
int ans=;
for(int i=head[s];i!=-;i=edge[i].next){
int v=edge[i].to;
if(edge[i].flow&&dis[v]==dis[s]+){
int f=DFS(v,min(flow-ans,edge[i].flow));
edge[i].flow-=f;
edge[i^].flow+=f;
ans+=f;
if(ans==flow)return ans;
}
}
return ans;
}
int Dinc(int s,int t){
int flow=;
while(BFS(s,t)){
//cout<<1<<endl;
flow+=DFS(s,INF);
//cout<<flow<<endl;
}
return flow;
}
int main(){
while(scanf("%d%d%d",&n,&f,&d)!=EOF){
init();
int s=;
for(int i=;i<=f;i++){
add(s,*n+i,);
}
for(int i=;i<=d;i++){
add(*n+f+i,*n+f+d+,);
}
for(int i=;i<=n;i++){
add(i,i+n,);
}
int D,F;
for(int i=;i<=n;i++){
scanf("%d%d",&D,&F);
for(int j=;j<=D;j++){
scanf("%d",&a[j]);
add(*n+a[j],i,);
}
for(int j=;j<=F;j++){
scanf("%d",&b[j]);
add(n+i,*n+f+b[j],);
}
}
cout<<Dinc(s,*n+f+d+)<<endl;
} }
POJ - 3281 Dining(拆点+最大网络流)的更多相关文章
- POJ 3281 Dining (拆点)【最大流】
<题目链接> 题目大意: 有N头牛,F种食物,D种饮料,每一头牛都有自己喜欢的食物和饮料,且每一种食物和饮料都只有一份,让你分配这些食物和饮料,问最多能使多少头牛同时获得自己喜欢的食物和饮 ...
- poj 3281 Dining 拆点 最大流
题目链接 题意 有\(N\)头牛,\(F\)个食物和\(D\)个饮料.每头牛都有自己偏好的食物和饮料列表. 问该如何分配食物和饮料,使得尽量多的牛能够既获得自己喜欢的食物又获得自己喜欢的饮料. 建图 ...
- POJ 3281 Dining (网络流)
POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...
- POJ 3281 Dining(最大流)
POJ 3281 Dining id=3281" target="_blank" style="">题目链接 题意:n个牛.每一个牛有一些喜欢的 ...
- poj 3281 Dining 网络流-最大流-建图的题
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...
- POJ 3281 Dining(网络流拆点)
[题目链接] http://poj.org/problem?id=3281 [题目大意] 给出一些食物,一些饮料,每头牛只喜欢一些种类的食物和饮料, 但是每头牛最多只能得到一种饮料和食物,问可以最多满 ...
- poj 3281 Dining(网络流+拆点)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20052 Accepted: 8915 Descripti ...
- 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)
Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...
- poj 3281 Dining【拆点网络流】
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11828 Accepted: 5437 Descripti ...
随机推荐
- sql server 大数据跨服务器迁移表数据——使用链接服务器
1.创建链接服务器(填写链接服务器.远程登录.使用密码) 2.188.188.1.177是远程的 select count(*) from [188.188.1.177].BigDataAnalysi ...
- jQuery——开关灯
js对象与jquery对象的相互转化: 1.$(js对象) 2.$(selector).get(索引).$(selector)[索引] <!DOCTYPE html> <html l ...
- 白盒-CNN纹理深度可视化: 使用MIT Place 场景预训练模型
MIT发文:深度视觉的量化表示................ Places2 是一个场景图像数据集,包含 1千万张 图片,400多个不同类型的场景环境,可用于以场景和环境为应用内容的视觉认知任务. ...
- ats 与 https
一些证书相关的描述: https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKe ...
- 如何在mac里面,把xcode代码同步到 tfs 的 git库(克隆git篇)
如果是新的git库,还没有任何内容可以克隆,可以参考:http://www.cnblogs.com/IWings/p/6755795.html 在mac安装visual studio code htt ...
- CAD全屏显示控件
主要用到函数说明: MxDrawXCustomFunction::Mx_FullScreen 全屏显示控件,详细说明如下: 参数 说明 int iFull = 2 0: 不完屏,1:全屏,2:自动切换 ...
- openstack——neutron网络服务
一.neutron 介绍: Neutron 概述 传统的网络管理方式很大程度上依赖于管理员手工配置和维护各种网络硬件设备:而云环境下的网络已经变得非常复杂,特别是在多租户场景里,用户随时都可能需要 ...
- sysbench使用指南
sysbench 安装.使用和测试 摘要: sysbench是一个开源的.模块化的.跨平台的多线程性能测试工具,可以用来进行CPU.内存.磁盘I/O.线程.数据库的性能测试.目前支持的数据库有MySQ ...
- 【模板】dijkstra
洛谷 4779 #include<cstdio> #include<cstring> #include<algorithm> #include<queue&g ...
- sql 区分大小写查询
sql 区分大小写查询 select * FROM [Users] where userName collate Chinese_PRC_CS_AS='ADMIN'