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 圆桌问题 网络流的更多相关文章

  1. 洛谷P3254 圆桌问题 网络流_二分图

    Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> ...

  2. 网络流之P3254 圆桌问题

    题目描述 假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri (i =1,2,……,m). 会议餐厅共有n 张餐桌,每张餐桌可容纳ci (i =1,2,……,n)个代表就餐. ...

  3. Luogu P3254 圆桌问题(最大流)

    P3254 圆桌问题 题面 题目描述 假设有来自 \(m\) 个不同单位的代表参加一次国际会议.每个单位的代表数分别为 \(r_i (i =1,2,--,m)\) . 会议餐厅共有 \(n\) 张餐桌 ...

  4. LibreOJ 6004. 「网络流 24 题」圆桌聚餐 网络流版子题

    #6004. 「网络流 24 题」圆桌聚餐 内存限制:256 MiB时间限制:5000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数 ...

  5. P3254 圆桌问题

    题目链接 非常简单的一道网络流题 我们发现每个单位的人要坐到不同餐桌上,那也就是说每张餐桌上不能有同一单位的人.这样的话,我们对于每个单位向每张餐桌连一条边权为1的边,表示同一餐桌不得有相同单位的人. ...

  6. [cogs729] [网络流24题#5] 圆桌聚餐 [网络流,最大流,多重二分图匹配]

    建图:从源点向单位连边,边权为单位人数,从单位向圆桌连边,边权为1,从圆桌向汇点连边,边权为圆桌容量. #include <iostream> #include <algorithm ...

  7. 洛谷 [P3254] 圆桌问题

    简单最大流建图 #include <iostream> #include <cstdio> #include <cstring> #include <cmat ...

  8. Luogu P3254 圆桌问题

    题目链接 \(Click\) \(Here\) 水题.记得记一下边的流量有没有跑完. #include <bits/stdc++.h> using namespace std; const ...

  9. 洛谷P3254 圆桌问题(最大流)

    传送门 一道良心啊……没那么多麻烦了…… 从$S$向所有单位连边,容量为单位人数,从所有桌子向$T$连边,容量为桌子能坐的人数,从每一个单位向所有桌子连边,容量为$1$,然后跑一个最大流,看一看$S$ ...

随机推荐

  1. 作业九——DFA最小化,语法分析初步

  2. (第七篇)系统编码、自启动配置、HOSTNAME、系统启动、定时任务、进程管理、硬盘及其分区

    linux查看系统编码和修改系统编码的方法 查看支持的字符编码 使用locale命令, 如: root@ubuntu:/etc# locale 然后修改/etc/locale.conf,如改成中文编码 ...

  3. 怎么在java中创建一个自定义的collector

    目录 简介 Collector介绍 自定义Collector 总结 怎么在java中创建一个自定义的collector 简介 在之前的java collectors文章里面,我们讲到了stream的c ...

  4. 【Linux网络基础】TCP/IP协议簇的详细介绍(三次握手四次断开,11种状态)

    一.TCP/IP协议簇(DoD参考模型) 用于简化OSI层次,以及相关的标准. 传输控制协议(tcp/ip)簇是相关国防部DoD所创建的,主要用来确保数据的完整性以及在毁灭性战争中维持通信 是由一组不 ...

  5. B/S和C/S架构的区别

    一.B/S架构 什么是B/S模式 B/S模式,即浏览器/服务器模式,是一种从传统的二层CS模式发展起来的新的网络结构模式,其本质是三层结构C/S模式.B/S网络结构模式是基于Intranet的需求而出 ...

  6. CSS样式3

    1.positon:fixed 可以实现网页浏览器上的返回顶部的功能. positon:fixed 表示将当前div块固定在页面的某一个位置(默认为左上角). <!DOCTYPE html> ...

  7. Ubuntu 14.04 配置samba

    Ubuntu 14.04 配置samba: 安装略 # vi /etc/samba/smb.conf security = user  (在[global]下任意添加) [share] path = ...

  8. javaweb系统调优方案

    1. java代码优化 java代码优化6大原则 : https://blog.csdn.net/bunny1024/article/details/72803708 java代码优化: https: ...

  9. 软件——IDEA主题美化

    前言 IntelliJ IDEA主要用于支持 Java.Scala.Groovy 等语言的开发工具,同时具备支持目前主流的技术和框架,擅长于企业应用.移动应用和 Web 应用的开发. IntelliJ ...

  10. 选择结构(if、switch)

    3.2  用if语句实现选择结构 什么是选择结构 单分支if语句 双分支if语句 多分支if语句 1.什么是选择结构? 选择结构又称为分支结构,是根据给定的条件是否成立来决定程序的执行流程. 用if语 ...