洛谷 P2891 [USACO07OPEN]吃饭Dining
裸的最大流。
#include <cstdio>
#include <cstring>
#include <queue>
const int MAXN = 4e3 + 19, MAXM = 4e6 + 19;
struct Edge{
int to, next, c;
}edge[MAXM];
int cnt = -1, head[MAXN];
inline void add(int from, int to, int c){
edge[++cnt].to = to;
edge[cnt].c = c;
edge[cnt].next = head[from];
head[from] = cnt;
}
int n, f, d;
int dep[MAXN];
int bfs(void){
std::queue<int>q; q.push(0);
std::memset(dep, 0, sizeof dep); dep[0] = 1;
while(!q.empty()){
int node = q.front(); q.pop();
for(int i = head[node]; i != -1; i = edge[i].next)
if(!dep[edge[i].to] && edge[i].c)
dep[edge[i].to] = dep[node] + 1, q.push(edge[i].to);
}
return dep[f + n + n + d + 1];
}
inline int min(const int& a, const int& b){
return a < b ? a : b;
}
int dfs(int node, int flow){
if(node == f + n + n + d + 1 || !flow)
return flow;
int stream = 0, f;
for(int i = head[node]; i != -1; i = edge[i].next)
if(dep[edge[i].to] == dep[node] + 1 && (f = dfs(edge[i].to, min(flow, edge[i].c)))){
flow -= f, stream += f;
edge[i].c -= f, edge[i ^ 1].c += f;
if(!flow)
break;
}
return stream;
}
int dinic(void){
int flow = 0;
while(bfs())
flow += dfs(0, 0x3f3f3f3f);
return flow;
}
int main(){
std::memset(head, -1, sizeof head);
std::scanf("%d%d%d", &n, &f, &d);
for(int i = 1; i <= f; ++i)
add(0, i, 1), add(i, 0, 0);
for(int i = f + 1; i <= f + n; ++i)
add(i, i + n, 1), add(i + n, i, 0);
for(int i = f + n + n + 1; i <= f + n + n + d; ++i)
add(i, f + n + n + d + 1, 1), add(f + n + n + d + 1, i, 0);
for(int a, b, i = 1; i <= n; ++i){
int l;
std::scanf("%d%d", &a, &b);
while(a--){
std::scanf("%d", &l);
add(l, f + i, 1), add(f + i, l, 0);
}
while(b--){
std::scanf("%d", &l);
add(f + n + i, f + n + n + l, 1), add(f + n + n + l, f + n + i, 0);
}
}
std::printf("%d\n", dinic());
return 0;
}
洛谷 P2891 [USACO07OPEN]吃饭Dining的更多相关文章
- 洛谷P2891 [USACO07OPEN]吃饭Dining
题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...
- P2891 [USACO07OPEN]吃饭Dining(最大流+拆点)
题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...
- P2891 [USACO07OPEN]吃饭Dining
漂亮小姐姐点击就送:https://www.luogu.org/problemnew/show/P2891 题目描述 Cows are such finicky eaters. Each cow ha ...
- P2891 [USACO07OPEN]吃饭Dining 最大流
\(\color{#0066ff}{ 题目描述 }\) 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类 ...
- 「洛谷P2891」[USACO07OPEN]吃饭Dining 解题报告
P2891 [USACO07OPEN]吃饭Dining 题目描述 Cows are such finicky eaters. Each cow has a preference for certain ...
- [Luogu P2891/POJ 3281/USACO07OPEN ]吃饭Dining
传送门:https://www.luogu.org/problemnew/show/P2891 题面 \ Solution 网络流 先引用一句真理:网络流最重要的就是建模 今天这道题让我深有体会 首先 ...
- 洛谷P2891 Dining P1402 酒店之王【类二分图匹配】题解+代码
洛谷P2891 Dining P1402 酒店之王[类二分图匹配]题解+代码 酒店之王 题目描述 XX酒店的老板想成为酒店之王,本着这种希望,第一步要将酒店变得人性化.由于很多来住店的旅客有自己喜好的 ...
- 2018.06.29 洛谷P2890 [USACO07OPEN]便宜的回文(简单dp)
P2890 [USACO07OPEN]便宜的回文Cheapest Palindrome 时空限制 1000ms / 128MB 题目描述 Keeping track of all the cows c ...
- [USACO07OPEN]吃饭Dining
嘟嘟嘟 这应该是网络流入门题之一了,跟教辅的组成这道题很像. 把每一只牛看成书,然后对牛拆点,因为每一只牛只要一份,食物和饮料分别看成练习册和答案. #include<cstdio> #i ...
随机推荐
- finally语句块一定会被执行吗
finally语句块一定会被执行吗? 答案: 不一定 1. 发生异常的代码必须在try 代码块中,才有可能被执行 public class MyTest { public static void ma ...
- Shiro入门学习之shi.ini实现授权(三)
一.Shiro授权 前提:需要认证通过才会有授权一说 1.授权过程 2.相关方法说明 ①subject.hasRole("role1"):判断是否有该角色 ②subject.has ...
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- 解决安装 fireworks、photoshop 时卡在输入账号、手机号处等问题
一定要断网! 一定要断网! 一定要断网! 重要的事情说三遍! 我安装的是破解版CS6系列: 安装步骤如下: 1.当进行安装到此步时,(前面的步骤为解压文件等便不解释),选择“试用”,此时应该是 断网 ...
- 三分钟让你秒懂.Net生态系统
提到.Net的时候,大多数人的第一反应可能就是.Net Framework和Visual Studio..Net Framework的第一个版本发布与2002年2月13日,这对于科技发展日新月异的时代 ...
- POJ 1064 Cable master(二分答案)
嗯... 题目链接:http://poj.org/problem?id=1064 其实这是一道很好想的二分答案的一道题... 二分的区间就是1~max_l,从1开始是因为所有小于1的都需要按0计算,没 ...
- python浅析格式化输出和深浅copy
一,格式化输出 今天主要想记录一下关于格式化输出的例子,然后结合了自己的理解,分析如下: 格式是 :百分号+占位符 主要有三种使用形式:%s (其中s表示string)表示字符串 %d (其中d表 ...
- NET站点升级后,新特新无法编译通过
NET3.5 webconfig中有自动配置如下代码,用于指示编译器. <system.codedom> <compilers> <compiler language=& ...
- CSS Sprite精灵图如何缩放大小
transform:scale( x ): 语法为:transform: scale(x,y). 同时有scaleX, scaleY专门的x, y方向的控制. 例如:transform: scale( ...
- Coursera 国内无法登陆问题
参考: 1.Coursera登不上去 | 知乎 2.Win10如何修改hosts | 百度经验 浏览器在访问https://www.coursera.org时,出现正在等待 d3njjcbhbojbo ...