[USACO07OPEN]吃饭Dining
这应该是网络流入门题之一了,跟教辅的组成这道题很像。
把每一只牛看成书,然后对牛拆点,因为每一只牛只要一份,食物和饮料分别看成练习册和答案。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = 4e3 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, f, d, t; struct Edge
{
int from, to, cap, flow;
};
vector<Edge> edges;
vector<int> G[maxn];
void addEdge(int from, int to)
{
edges.push_back((Edge){from, to, , });
edges.push_back((Edge){to, from, , });
int sz = edges.size();
G[from].push_back(sz - );
G[to].push_back(sz - );
} int dis[maxn];
bool bfs()
{
Mem(dis, ); dis[] = ;
queue<int> q; q.push();
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = ; i < (int)G[now].size(); ++i)
{
Edge& e = edges[G[now][i]];
if(!dis[e.to] && e.cap > e.flow)
{
dis[e.to] = dis[now] + ;
q.push(e.to);
}
}
}
return dis[t];
}
int cur[maxn];
int dfs(int now, int res)
{
if(now == t || res == ) return res;
int flow = , f;
for(int& i = cur[now]; i < (int)G[now].size(); ++i)
{
Edge& e = edges[G[now][i]];
if(dis[e.to] == dis[now] + && (f = dfs(e.to, min(res, e.cap - e.flow))) > )
{
e.flow += f;
edges[G[now][i] ^ ].flow -= f;
flow += f;
res -= f;
if(res == ) break;
}
}
return flow;
} int maxflow()
{
int flow = ;
while(bfs())
{
Mem(cur, );
flow += dfs(, INF);
}
return flow;
} int main()
{
n = read(); f = read(); d = read();
t = (n << ) + f + d + ;
for(int i = ; i <= f; ++i) addEdge(, i);
for(int i = ; i <= d; ++i) addEdge((n << ) + f + i, t);
for(int i = ; i <= n; ++i)
{
addEdge(f + i, f + n + i);
int f1 = read(), d1 = read();
for(int j = ; j <= f1; ++j)
{
int x = read();
addEdge(x, f + i);
}
for(int j = ; j <= d1; ++j)
{
int x = read();
addEdge(f + n + i, (n << ) + f + x);
}
}
write(maxflow()); enter;
return ;
}
[USACO07OPEN]吃饭Dining的更多相关文章
- 「洛谷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 [USACO07OPEN]吃饭Dining(最大流+拆点)
题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she w ...
- P2891 [USACO07OPEN]吃饭Dining 最大流
\(\color{#0066ff}{ 题目描述 }\) 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类 ...
- 洛谷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 ...
- bzoj1711[USACO07OPEN]吃饭Dining
题意 有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享用到自己喜欢的食物和饮 ...
- 洛谷 P2891 [USACO07OPEN]吃饭Dining
裸的最大流. #include <cstdio> #include <cstring> #include <queue> const int MAXN = 4e3 ...
- BZOJ 1711 吃饭dining/Luogu P1402 酒店之王 拆点+最大流流匹配
题意: (吃饭dining)有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享 ...
随机推荐
- Shiro - 自定义filterChainDefinitions和Realm
在Spring Context中定义shiroFilter(org.apache.shiro.spring.web.ShiroFilterFactoryBean)时需要为其filterChainDef ...
- IIS网站不能访问
摘要:IIS环境下,部署的网站在服务器上可以正常访问,客户端却不能访问. 原因:防火墙入站规则万维网服务没有开启.
- 【转】Encrypt ConnectionString in Web.Config 【加密ASP.NET web.config数据库链接字串】
原文链接:https://www.codeproject.com/Tips/795135/Encrypt-ConnectionString-in-Web-Config web.config中一般会存放 ...
- URL 编码 之 我见
URL编码 编辑 url编码是一种浏览器用来打包表单输入的格式.浏览器从表单中获取所有的name和其中的值 ,将它们以name/value参数编码(移去那些不能传送的字符,将数据排行等等)作为URL的 ...
- Java基础(九)多线程
一.线程和进程 进程(Process): 1.是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础. 2.在早期面向进程设计的计算机结构中,进程是程 ...
- android 动态库死机调试方法 .
原地址:http://blog.csdn.net/andyhuabing/article/details/7074979 这两种方法都不是我发明了,都是网上一些高手公共出来的调试方法,无奈找不到出处的 ...
- Windows下etc文件夹
etc etcetera[ɛtsɛtərə]缩写 等等的意思 放置一些其他文件
- 解决Win7远程连接灰色无法选中的问题
内网开发最常用的莫过于Windows的远程连接服务了.但是有时候这个远程连接是灰色的,无法勾选.折腾了各种办法.最后在这里找到了答案.https://answers.microsoft.com/en- ...
- kafka leader平衡策略
1.1个partition的默认leader是replicas中的第一个replica 2.kafka controller会启动一个定时的check线程,kafka默认是5min周期,mafka是3 ...
- 从尾到头打印链表(C++和Python 实现)
(说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 输入一个链表的头结点, 从尾到头反过来打印出每个结点的值. ...