POJ 3281(Dining-网络流拆点)[Template:网络流dinic]
Language:
Default
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 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 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. Source |
首先s向食物连边。饮料向t连边。容量=1(每份食物仅仅有一份)
然后相应的食物向牛。再向相应的饮料连边,容量=1,表示1种取法
可是一仅仅牛仅仅能取一份,所以牛代表的点本身容量=1。故拆点。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXn (100+10)
#define MAXf (100+10)
#define MAXd (100+10)
#define MAXN (1000+10)
#define MAXM ((30300)*2+100)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
class Max_flow //dinic+当前弧优化
{
public:
int n,s,t;
int q[10000];
int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;
void addedge(int u,int v,int w)
{
edge[++size]=v;
weight[size]=w;
next[size]=pre[u];
pre[u]=size;
}
void addedge2(int u,int v,int w){addedge(u,v,w),addedge(v,u,0);}
bool b[MAXN];
int d[MAXN];
bool SPFA(int s,int t)
{
For(i,n) d[i]=INF;
MEM(b)
d[q[1]=s]=0;b[s]=1;
int head=1,tail=1;
while (head<=tail)
{
int now=q[head++];
Forp(now)
{
int &v=edge[p];
if (weight[p]&&!b[v])
{
d[v]=d[now]+1;
b[v]=1,q[++tail]=v;
}
}
}
return b[t];
}
int iter[MAXN];
int dfs(int x,int f)
{
if (x==t) return f;
Forpiter(x)
{
int v=edge[p];
if (weight[p]&&d[x]<d[v])
{
int nowflow=dfs(v,min(weight[p],f));
if (nowflow)
{
weight[p]-=nowflow;
weight[p^1]+=nowflow;
return nowflow;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int flow=0;
while(SPFA(s,t))
{
For(i,n) iter[i]=pre[i];
int f;
while (f=dfs(s,INF))
flow+=f;
}
return flow;
}
void mem(int n,int s,int t)
{
(*this).n=n;
(*this).t=t;
(*this).s=s; size=1;
MEM(pre)
}
}S; int n,f,d;
int main()
{
// freopen("poj3281.in","r",stdin);
// freopen(".out","w",stdout);
cin>>n>>f>>d;
int s=1,t=2+2*n+f+d;
S.mem(t,1,t); For(i,f)
S.addedge2(s,1+i,1); For(i,d)
S.addedge2(1+f+2*n+i,t,1); For(i,n)
{
S.addedge2(1+f+i,1+f+n+i,1);
int fi,di,p;
scanf("%d%d",&fi,&di);
For(j,fi)
{
scanf("%d",&p);
S.addedge2(1+p,1+f+i,1);
}
For(j,di)
{
scanf("%d",&p);
S.addedge2(1+f+n+i,1+f+2*n+p,1);
} } cout<<S.max_flow(s,t)<<endl; return 0;
}
POJ 3281(Dining-网络流拆点)[Template:网络流dinic]的更多相关文章
- POJ - 3281 Dining(拆点+最大网络流)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18230 Accepted: 8132 Descripti ...
- poj 3281 Dining【拆点网络流】
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11828 Accepted: 5437 Descripti ...
- 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(网络流+拆点)
Dining Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20052 Accepted: 8915 Descripti ...
- POJ 3281 Dining(网络流拆点)
[题目链接] http://poj.org/problem?id=3281 [题目大意] 给出一些食物,一些饮料,每头牛只喜欢一些种类的食物和饮料, 但是每头牛最多只能得到一种饮料和食物,问可以最多满 ...
- 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)
Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...
- POJ 3281 Dining(网络流-拆点)
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...
随机推荐
- go之数据类型转换和类型断言
一.类型转换 1.1 简单类型转换 格式 valueOfTypeB = typeB(valueOfTypeA) int 转 float64 package main import "fmt& ...
- A - Team
Problem description One day three best friends Petya, Vasya and Tonya decided to form a team and tak ...
- Excel 批量出来数据
try { string sheetname = TextBox1.Text.Trim(); HttpPostedFile upLoadPostFile = FileUpload1.PostedFil ...
- 图像局部显著性—点特征(SURF)
1999年的SIFT(ICCV 1999,并改进发表于IJCV 2004,本文描述):参考描述:图像特征点描述. 参考原文:SURF特征提取分析 本文有大量删除,如有疑义,请参考原文. SURF对SI ...
- Functor and Monad in Swift
I have been trying to teach myself Functional Programming since late 2013. Many of the concepts are ...
- efcore 控制台迁移架构
添加 nuget 包: Microsoft.EntityFrameworkCore.Design Microsoft.EntityFrameworkCore.SqlServer Microsoft.E ...
- mysql与oracle 表字段定义比较
链接: https://blog.csdn.net/yzsind/article/details/7948226
- BZOJ 1060: [ZJOI2007]时态同步 树上问题 + 贪心
Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数 字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路 ...
- Log4net日志发布到服务器上日志无法写入
log4net在本地执行时候,日志正常写入,但是发布到服务器上的时候,日志就无法正常写入 解决方案: 1.文件权限 在发布到服务器上的时候,可能文件没有写入权限,导致日志无法正常写入 打开IIS 找到 ...
- PAT_A1013#Battle Over Cities
Source: PAT A1013 Battle Over Cities (25 分) Description: It is vitally important to have all the cit ...