P3254 圆桌问题 网络流
#include <bits/stdc++.h>
using namespace std;
const int maxn = , inf = 0x3f3f3f;
struct Edge {
int from, to, cap, flow;
}; struct Dinic {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn]; void AddEdge(int from, int to, int cap) {
edges.push_back((Edge){from, to, cap, });
edges.push_back((Edge){to, from, , });
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool bfs() {
memset(vis, , sizeof(vis));
queue<int> que;
que.push(s);
d[s] = ;
vis[s] = true;
while (!que.empty()) {
int x = que.front(); que.pop();
for (int i = ; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = true;
d[e.to] = d[x] + ;
que.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a) {
if (x == t || a == ) return a;
int flow = , f;
for (int& i = cur[x]; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (d[x] + == d[e.to] && (f = dfs(e.to, min(a, e.cap-e.flow))) > ) {
e.flow += f;
edges[G[x][i]^].flow -= f;
flow += f;
a -= f;
if (a == ) break;
}
}
return flow;
}
int maxflow(int s, int t) {
this->s = s; this->t = t;
int flow = ;
while (bfs()) {
memset(cur,,sizeof(cur));
flow += dfs(s,inf);
}
return flow;
}
}dinic; int r[maxn], c[maxn];
int main() {
int m, n; scanf("%d%d",&m,&n);
int s = m+n+, t = m+n+, sum = ;
for (int i = ; i <= m; ++i) {
scanf("%d",&r[i]);
sum += r[i];
}
for (int i = ; i <= n; ++i) scanf("%d",&c[i]); for (int i = ; i <= m; ++i) {
dinic.AddEdge(s,i,r[i]);
}
for (int i = ; i <= n; ++i) {
dinic.AddEdge(i+m,t,c[i]);
}
for (int i = ; i <= m; ++i) {
for (int j = ; j <= n; ++j) {
dinic.AddEdge(i,j+m,);
}
}
int ans = dinic.maxflow(s,t);
if (ans != sum) puts("");
else {
puts("");
for (int i = ; i <= m; ++i) {
for (int j = ; j < dinic.edges.size(); ++j) {
if (dinic.edges[j].from == i && dinic.edges[j].flow == ) {
printf("%d ",dinic.edges[j].to-m);
}
}
putchar('\n');
}
}
return ;
}
P3254 圆桌问题 网络流的更多相关文章
- 洛谷P3254 圆桌问题 网络流_二分图
Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> ...
- 网络流之P3254 圆桌问题
题目描述 假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri (i =1,2,……,m). 会议餐厅共有n 张餐桌,每张餐桌可容纳ci (i =1,2,……,n)个代表就餐. ...
- Luogu P3254 圆桌问题(最大流)
P3254 圆桌问题 题面 题目描述 假设有来自 \(m\) 个不同单位的代表参加一次国际会议.每个单位的代表数分别为 \(r_i (i =1,2,--,m)\) . 会议餐厅共有 \(n\) 张餐桌 ...
- LibreOJ 6004. 「网络流 24 题」圆桌聚餐 网络流版子题
#6004. 「网络流 24 题」圆桌聚餐 内存限制:256 MiB时间限制:5000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数 ...
- P3254 圆桌问题
题目链接 非常简单的一道网络流题 我们发现每个单位的人要坐到不同餐桌上,那也就是说每张餐桌上不能有同一单位的人.这样的话,我们对于每个单位向每张餐桌连一条边权为1的边,表示同一餐桌不得有相同单位的人. ...
- [cogs729] [网络流24题#5] 圆桌聚餐 [网络流,最大流,多重二分图匹配]
建图:从源点向单位连边,边权为单位人数,从单位向圆桌连边,边权为1,从圆桌向汇点连边,边权为圆桌容量. #include <iostream> #include <algorithm ...
- 洛谷 [P3254] 圆桌问题
简单最大流建图 #include <iostream> #include <cstdio> #include <cstring> #include <cmat ...
- Luogu P3254 圆桌问题
题目链接 \(Click\) \(Here\) 水题.记得记一下边的流量有没有跑完. #include <bits/stdc++.h> using namespace std; const ...
- 洛谷P3254 圆桌问题(最大流)
传送门 一道良心啊……没那么多麻烦了…… 从$S$向所有单位连边,容量为单位人数,从所有桌子向$T$连边,容量为桌子能坐的人数,从每一个单位向所有桌子连边,容量为$1$,然后跑一个最大流,看一看$S$ ...
随机推荐
- List of common SCSI KCQs
Category Key ASC ASCQ Error Condition No Sense 0 00 00 No error 0 5D 00 No sense - PFA threshold rea ...
- Synchronous and Asynchronous I/O [Windows]
There are two types of input/output (I/O) synchronization: synchronous I/O and asynchronous I/O. Asy ...
- Libra教程之:Libra testnet使用指南
文章目录 Libra testnet网络 下载和安装Libra 编译Libra client并连接到Testnet网络 创建两个A和B的两个账号 检查libra cli Client是否运行 创建A的 ...
- Python3的日期和时间
2019独角兽企业重金招聘Python工程师标准>>> python 中处理日期时间数据通常使用datetime和time库 因为这两个库中的一些功能有些重复,所以,首先我们来比较一 ...
- mybatis源码学习(二):SQL的执行过程
从上一篇文章中,我们了解到MapperMethod将SQL的执行交给了sqlsession处理.今天我们继续往下看处理的过程. SqlSession接口除了提供获取Configuration,Mapp ...
- 解决Visual Studio (VS) 插件下载缓慢
1.关闭IPV6协议 因为如果都支持IPV6协议,会自动使用IPV6下载扩展. 因为IPV6还没有建立完善,所以可能会比较慢. 百度经验:怎样关闭IPV6协议 2.给IPV6添加DNS 百度: 240 ...
- 通过例题进一步学习DP
1.以上篇文章数塔为例 https://blog.csdn.net/weixin_43627118/article/details/88701586 上一章用的是递归的做法,这次我们采用递推的做法. ...
- Word转pdf,再转图片插入PDF
WORD转PDF所需jar包: https://yangtaotao.lanzous.com/ice1jlc PDF转图片所需jar包: https://yangtaotao.lanzous.com/ ...
- Elasticsearch系列---Term Vector工具探查数据
概要 本篇主要介绍一个Term Vector的概念和基本使用方法. term vector是什么? 每次有document数据插入时,elasticsearch除了对document进行正排.倒排索引 ...
- 远程登陆与数据CP
1 SSH登陆原理 SSH,Secure Shell,是一套协议标准,可以用来实现两台机器之间的安全登陆以及安全的数据传送,其保证数据安全的原理是非对称加密. 传统的对称加密方式使用的是一套密 ...