poj 3281 Dining(网络流+拆点)
Dining
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 20052 | Accepted: 8915 |
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
分析
拆点+网络流最大流
code
#include<cstdio>
#include<algorithm> using namespace std; const int INF = 1e9;
const int N = ; struct Edge{
int to,nxt,c;
}e[N];
int head[N],dis[N],cur[N],q[];
int tot = ,L,R,S,T; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>''; ch = nc()) if (ch=='-') f = -;
for (; ch>=''&&ch<=''; ch = nc()) x = x * + ch - '';
return x * f;
}
inline void add_edge(int u,int v,int w) {
e[++tot].to = v,e[tot].c = w,e[tot].nxt = head[u],head[u] = tot;
e[++tot].to = u,e[tot].c = ,e[tot].nxt = head[v],head[v] = tot;
}
bool bfs() {
for (int i=; i<=T; ++i) {
cur[i] = head[i];dis[i] = -;
}
L = ;R = ;
q[++R] = S;dis[S] = ;
while (L <= R) {
int u = q[L++];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to,c = e[i].c;
if (dis[v]==- && c>) {
dis[v] = dis[u] + ;
q[++R] = v;
if (v==T) return true;
}
}
}
return false;
}
int dfs(int u,int flow) {
if (u==T) return flow;
int used = ;
for (int &i=cur[u]; i; i=e[i].nxt) {
int v = e[i].to,c = e[i].c;
if (dis[v]==dis[u]+ && c>) {
int tmp = dfs(v,min(c,flow-used));
if (tmp > ) {
e[i].c -= tmp;e[i^].c += tmp;
used += tmp;
if (used==flow) break;
}
}
}
if (used!=flow) dis[u] = -;
return used;
}
inline int dinic() {
int ans = ;
while (bfs()) ans += dfs(S,INF);
return ans;
}
int main() {
int n = read(),F = read(),D = read();
S = ,T = n+n+F+D+;
for (int i=; i<=n; ++i) {
int f = read(),d = read();
for (int a,j=; j<=f; ++j)
a = read(),add_edge(a+n+n,i,);
for (int a,j=; j<=d; ++j)
a = read(),add_edge(i+n,a+n+n+F,);
}
for (int i=; i<=n; ++i) add_edge(i,i+n,);
for (int i=; i<=F; ++i) add_edge(S,i+n+n,);
for (int i=; i<=D; ++i) add_edge(i+n+n+F,T,);
printf("%d",dinic());
return ;
}
poj 3281 Dining(网络流+拆点)的更多相关文章
- POJ 3281 Dining(网络流-拆点)
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...
- poj 3281 Dining 网络流-最大流-建图的题
题意很简单:JOHN是一个农场主养了一些奶牛,神奇的是这些个奶牛有不同的品味,只喜欢吃某些食物,喝某些饮料,傻傻的John做了很多食物和饮料,但她不知道可以最多喂饱多少牛,(喂饱当然是有吃有喝才会饱) ...
- 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 网络流最大流
B - DiningTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.ac ...
- POJ 3281 Dining (网络流之最大流)
题意:农夫为他的 N (1 ≤ N ≤ 100) 牛准备了 F (1 ≤ F ≤ 100)种食物和 D (1 ≤ D ≤ 100) 种饮料.每头牛都有各自喜欢的食物和饮料, 而每种食物或饮料只能分配给 ...
- POJ 3281 Dining[网络流]
Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will c ...
- POJ 3281 Dining (网络流构图)
[题意]有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有N头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和 ...
- 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个牛.每一个牛有一些喜欢的 ...
随机推荐
- Android sdk manager 显示 “Done loading packages”,该怎么办?
试了这个方法:请用管理员的身份运行"SDK Manager.exe".不管用. 下面的方法可以: 在SDK Manager下Tools->Options打开了SDK Mana ...
- JsonModel&AFNetWorking
// HttpManager.h // JsonModel&AFNetWorking // // Created by qianfeng on 15/7/21. // Copyright (c ...
- vue2.0:(五)、路由vue-router
好的,接下来,我们来写路由.用的是vue2.0的路由. 步骤一:配置main.js import Vue from 'vue'; import App from './App'; import rou ...
- js获取当前的年月日时分秒周期
function timeNow(){ var date = new Date(); this.year = date.getFullYear(); this.month = date.getMont ...
- 数据库limit子句
limit子句:用来限定语句执行结果的偏移量,有一个或者两个参数:第一个参数表示返回结果首行偏移量,第二个参数表示最大返回行数.例如:SELECT * FROM employees ORDER BY ...
- CentOS7.2上安装Python3.6
CentOS 7下安装Python3.6 1)安装python3.6可能使用的依赖yum -y install openssl-devel bzip2-devel expat-devel gdbm-d ...
- Win10微软帐户切换不回Administrator本地帐户的解决方法【亲测】
在Win10系统中经常会用到微软帐户登录,如应用商店等地方,不过一些用户反馈原来使用Administrator帐户被绑定微软帐户后无法切换回本地帐户,连[改用本地帐户登录]按钮都没有,那么怎么解决呢? ...
- COGS 1715. [CQOI2011]动态逆序对
★★★ 输入文件:inverse.in 输出文件:inverse.out 简单对比时间限制:2 s 内存限制:128 MB [题目描述] 对于序列A,它的逆序对数定义为满足i<j ...
- ASP.NET(Web Form)绘制图表 -- Google Chart 三部曲
ASP.NET(Web Form)绘制图表 -- Google Chart 三部曲 ASP.NET(Web Form)绘制图表 -- Google Chart 三部曲 1. 网页绘制图表 Googl ...
- MovieReview—Kingsman THE SECRET SERVICE(王牌特工之特工学院)
Brain Storming Mr. Valentine's Day see excess human beings as the Earth's virus and try to e ...