题意

$m$个不同单位代表参加会议,第$i$个单位有$r_i$个人

$n$张餐桌,第$i$张可容纳$c_i$个代表就餐

同一个单位的代表需要在不同的餐桌就餐

问是否可行,要求输出方案

Sol

比较zz的最大流

从$S$向$1-m$连流量为$r_i$的边

从$m + 1$向$m + n$连流量为$c_i$的边

从$1-m$向$m + 1$到$m + n$中的每个点连流量为$1$的边

跑最大流即可

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int MAXN = 1e5 + , INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int M, N, S, T;
int r[MAXN], c[MAXN];
struct Edge {
int u, v, f, nxt;
}E[MAXN];
int head[MAXN], cur[MAXN], num;
inline void add_edge(int x, int y, int f) {
E[num] = (Edge){x, y, f, head[x]};
head[x] = num++;
}
inline void AddEdge(int x, int y, int z) {
add_edge(x, y, z);
add_edge(y, x, );
}
int sum = , deep[MAXN];
bool BFS() {
queue<int> q; q.push(S);
memset(deep, , sizeof(deep)); deep[S] = ;
while(!q.empty()) {
int p = q.front(); q.pop();
for(int i = head[p]; i != -; i = E[i].nxt) {
int to = E[i].v;
if(!deep[to] && E[i].f) {
deep[to] = deep[p] + ;
q.push(to);
}
}
}
return deep[T] > ;
}
int DFS(int x, int flow) {
if(x == T) return flow;
int ansflow = ;
for(int &i = cur[x]; i != -; i = E[i].nxt) {
int to = E[i].v;
if(deep[to] == deep[x] + && E[i].f) {
int nowflow = DFS(to, min(flow, E[i].f));
E[i].f -= nowflow; E[i ^ ].f += nowflow;
ansflow += nowflow; flow -= nowflow;
if(flow <= ) break;
}
}
return ansflow;
}
int Dinic() {
int ans = ;
while(BFS()) {
memcpy(cur, head, sizeof(head));
ans += DFS(S, INF);
}
return ans;
}
int main() {
memset(head, -, sizeof(head));
M = read(); N = read(); S = ; T = M + N + ;
for(int i = ; i <= M; i++) r[i] = read(), AddEdge(S, i, r[i]), sum += r[i];
for(int i = ; i <= N; i++) c[i] = read(), AddEdge(i + M, T, c[i]);
for(int i = ; i <= M; i++)
for(int j = ; j <= N; j++)
AddEdge(i, j + M, );
if(Dinic() >= sum) printf("1\n");
else {printf(""); return ;}
for(int x = ; x <= M; x++) {
for(int i = head[x]; i != -; i = E[i].nxt)
if(E[i].f == )
printf("%d ", E[i].v - M);
puts("");
}
return ;
}

洛谷P3254 圆桌问题(最大流)的更多相关文章

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

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

  2. 洛谷 P3254 圆桌问题【最大流】

    s向所有单位连流量为人数的边,所有饭桌向t连流量为饭桌容量的边,每个单位向每个饭桌连容量为1的边表示这个饭桌只能坐这个单位的一个人.跑dinic如果小于总人数则无解,否则对于每个单位for与它相连.满 ...

  3. 洛谷 [P3254] 圆桌问题

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

  4. 洛谷.3254.圆桌问题(最大流ISAP)

    题目链接 日常水题 还是忍不住吐槽这题奇怪的评价 #include <cstdio> #include <cctype> #include <algorithm> ...

  5. [洛谷P3254]圆桌问题

    题目大意:有$m$个单位,每个单位有$r_i$个代表,有$n$张餐桌,每张餐桌可容纳$c_i$个代表.要求同一个单位的代表不在同一个餐桌就餐.若可以,输出$1$以及其中一种方案,否则输出$0$ 题解: ...

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

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

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

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

  8. 洛谷.4015.运输问题(SPFA费用流)

    题目链接 嗯..水题 洛谷这网络流二十四题的难度评价真神奇.. #include <queue> #include <cstdio> #include <cctype&g ...

  9. [洛谷P3254] [网络流24题] 圆桌游戏

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

随机推荐

  1. ES6的相关新属性

    ES6  引入了类这个概念. 1.class……extends es6中的class与es5 中的function差不多: class Student extends People , student ...

  2. IntelliJ IDEA 注册码 有效期截止于2018/10/14

    来源: http://idea.lanyus.com/ IntelliJ IDEA 注册码: EB101IWSWD-eyJsaWNlbnNlSWQiOiJFQjEwMUlXU1dEIiwibGljZW ...

  3. Python项目使用memcached缓存

    前言许多Web应用都将数据保存到MySQL这样的关系型数据库管理系统中,应用服务器从中读取数据并在浏览器中显示. 但随着数据量的增大.访问的集中,就会出现数据库的负担加重.数据库响应恶化. 网站显示延 ...

  4. (转)Excel自定义格式详解

    ”G/通用格式”:以常规的数字显示,相当于”分类”列表中的”常规”选项.例:代码:”G/通用格式”.10显示为10:10.1显示为10.1. 2. “#”:数字占位符.只显有意义的零而不显示无意义的零 ...

  5. Naïve Bayes Models

    贝叶斯模型假设: 为防止概率为零的情况,做拉普拉斯平滑得: 下面介绍一下朴素贝叶斯模型与多变量伯努利模型的区别: 朴素贝叶斯: 多变量伯努利: 即: 多变量伯努利模型不考虑样本出现的次数,每个特征的取 ...

  6. 从零开始构建一个Reactor模式的网络库(二)线程类Thread

    线程类Thread是对POSIX线程的封装类,因为要构建的是一个Linux环境下的多线程网络库,对线程的封装是很必要的. 首先是CurrentThread命名空间,主要是获取以及缓存线程id: #if ...

  7. E - Alice and Bob

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  8. 安装 statconn 使R与c#交互

    很久以前完成过,但是最近重新折腾发现全忘了,所以记下来. 1.安装 R 2.安装 R studio 3.去 http://rcom.univie.ac.at/download.html 下载 stat ...

  9. bzoj4145 [AMPPZ2014]The Prices(状压dp)

    Description 你要购买m种物品各一件,一共有n家商店,你到第i家商店的路费为d[i],在第i家商店购买第j种物品的费用为c[i][j], 求最小总费用. Input 第一行包含两个正整数n, ...

  10. 洛谷P4199 万径人踪灭(manacher+FFT)

    传送门 题目所求为所有的不连续回文子序列个数,可以转化为回文子序列数-回文子串数 回文子串manacher跑一跑就行了,考虑怎么求回文子序列数 我们考虑,如果$S_i$是回文子序列的对称中心,那么只要 ...