洛谷 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 ...
随机推荐
- Celery异步框架
一.什么是celery Celery是一个简单.灵活且可靠的,处理大量消息的分布式系统 专注于实时处理的异步任务队列 同时也支持任务调度 二.Celery架构 Celery的架构由三部分组成,消息中间 ...
- PHP加JS实现分片上传,断点续传
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title> ...
- MySQL8.0.11安装后,使用CMD无法启动mysql服务
首先,先把mysql的bin路径添加到系统环境变量 这样做可以,直接进入CMD后执行mysql服务,不需要进入mysql的bin文件路径去执行. 第一步:在MySQL的安装文件的bin目录(例如:C: ...
- Django框架之登录案例
内容: (1)request.GET和request.POST (2)获取get方法提交和post方法提交的数据 一.登录案例 登录逻辑代码 def login(request): if reques ...
- javascript的正则匹配
前段时间需要用到比较多的js代码,当时有点搞不清test和match方法的区别,向百度求助,找到了这边关于正则匹配的博文,感谢作者分享. 原文地址[http://blog.sina.com.cn/s/ ...
- vmware克隆机无法启动eth0联网问题
cd /etc/sysconfig/network-scrip vi ifcfg-eth0 把eth0修改成eth1,重启网卡就可以了
- 吴裕雄 python 神经网络——TensorFlow训练神经网络:卷积层、池化层样例
import numpy as np import tensorflow as tf M = np.array([ [[1],[-1],[0]], [[-1],[2],[1]], [[0],[2],[ ...
- Cisco 3702i TX功率图
有关思科TX Power选择的一些基本信息: 思科使用1到8的等级,其中1是最高功率,8是最低功率虽然比例上升到8,但不是每个band都使用(0-7)8个数据中的所有值.每个数字都有一个相应的dBm值 ...
- Java中获取MongoDB连接的方法
首先是所需jar包,Maven中的配置如下: <dependency> <groupId>org.mongodb</groupId> <artifactId& ...
- js脚本中执行java后台代码
使用场景:关闭页面弹窗时执行sql语句. 其实js里执行sql语句有多种方式. 方式一:直接在js代码里调用sql语句,原则上不能使用,因为这将sql直接暴露在客户端,安全性极差. 方式二:在js里运 ...