LibreOJ 6004. 「网络流 24 题」圆桌聚餐 网络流版子题
#6004. 「网络流 24 题」圆桌聚餐
题目描述
假设有来自 n nn 个不同单位的代表参加一次国际会议。每个单位的代表数分别为 ri r_iri。会议餐厅共有 m mm 张餐桌,每张餐桌可容纳 ci c_ici 个代表就餐。
为了使代表们充分交流,希望从同一个单位来的代表不在同一个餐桌就餐。
试设计一个算法,给出满足要求的代表就餐方案。
输入格式
文件第 1 11 行有 2 22 个正整数 m mm 和 n nn,m mm 表示单位数,n nn 表示餐桌数。
文件第 2 22 行有 m mm 个正整数,分别表示每个单位的代表数。
文件第 3 33 行有 n nn 个正整数,分别表示每个餐桌的容量。
输出格式
如果问题有解,在文件第 1 11 行输出 1 11,否则输出 0 00。
接下来的 m mm 行给出每个单位代表的就餐桌号。如果有多个满足要求的方案,只要输出一个方案。
样例
样例输入
4 5
4 5 3 5
3 5 2 6 4
样例输出
1
1 2 4 5
1 2 3 4 5
2 4 5
1 2 3 4 5
数据范围与提示
1≤m≤150,1≤n≤270 1 \leq m \leq 150, 1 \leq n \leq 2701≤m≤150,1≤n≤270
题目链接:https://loj.ac/problem/6004
思路:最大流板子题
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define PI acos(-1.0)
const int maxn=,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e18+;
struct edge
{
int from,to,cap,flow;
};
vector<edge>es;
vector<int>G[maxn];
bool vis[maxn];
int dist[maxn];
int iter[maxn];
void init(int n)
{
for(int i=; i<=n+; i++) G[i].clear();
es.clear();
}
void addedge(int from,int to,int cap)
{
es.push_back((edge)
{
from,to,cap,
});
es.push_back((edge)
{
to,from,,
});
int x=es.size();
G[from].push_back(x-);
G[to].push_back(x-);
}
bool BFS(int s,int t)
{
memset(vis,,sizeof(vis));
queue <int> Q;
vis[s]=;
dist[s]=;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for (int i=; i<G[u].size(); i++)
{
edge &e=es[G[u][i]];
if (!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
dist[e.to]=dist[u]+;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int u,int t,int f)
{
if(u==t||f==) return f;
int flow=,d;
for(int &i=iter[u]; i<G[u].size(); i++)
{
edge &e=es[G[u][i]];
if(dist[u]+==dist[e.to]&&(d=DFS(e.to,t,min(f,e.cap-e.flow)))>)
{
e.flow+=d;
es[G[u][i]^].flow-=d;
flow+=d;
f-=d;
if (f==) break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
int flow=;
while(BFS(s,t))
{
memset(iter,,sizeof(iter));
int d=;
while(d=DFS(s,t,inf)) flow+=d;
}
return flow;
}
int a[maxn],b[maxn];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int s=,t=n+m+;
init(n+m+);
int sum=;
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
addedge(i,j+n,);
}
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
sum+=a[i];
addedge(s,i,a[i]);
}
for(int i=; i<=m; i++)
{
scanf("%d",&b[i]);
addedge(i+n,t,b[i]);
}
if(Maxflow(s,t)<sum) printf("0\n");
else
{
printf("1\n");
for(int i=; i<n*m*; i+=)
{
if(es[i].flow>) printf("%d ",es[i].to-n);
if(es[i].to-n==m) printf("\n");
}
}
return ;
}
最大流版子题
LibreOJ 6004. 「网络流 24 题」圆桌聚餐 网络流版子题的更多相关文章
- Libre 6004 「网络流 24 题」圆桌聚餐(网络流,最大流)
Libre 6004 「网络流 24 题」圆桌聚餐(网络流,最大流) Description 假设有来自n个不同单位的代表参加一次国际会议.每个单位的代表数分别为 ri.会议餐厅共有m张餐桌,每张餐桌 ...
- 【刷题】LOJ 6004 「网络流 24 题」圆桌聚餐
题目描述 假设有来自 \(n\) 个不同单位的代表参加一次国际会议.每个单位的代表数分别为 \(r_i\) .会议餐厅共有 \(m\) 张餐桌,每张餐桌可容纳 \(c_i\) 个代表就餐. 为了使 ...
- [cogs729] [网络流24题#5] 圆桌聚餐 [网络流,最大流,多重二分图匹配]
建图:从源点向单位连边,边权为单位人数,从单位向圆桌连边,边权为1,从圆桌向汇点连边,边权为圆桌容量. #include <iostream> #include <algorithm ...
- LibreOJ #6212. 「美团 CodeM 决赛」melon
二次联通门 : LibreOJ #6212. 「美团 CodeM 决赛」melon /* LibreOJ #6212. 「美团 CodeM 决赛」melon MDZZ 这是决赛题?? */ #incl ...
- Libre 6012 「网络流 24 题」分配问题 (网络流,费用流)
Libre 6012 「网络流 24 题」分配问题 (网络流,费用流) Description 有n件工作要分配给n个人做.第i个人做第j件工作产生的效益为\(c_{ij}\).试设计一个将n件工作分 ...
- Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流)
Libre 6011 「网络流 24 题」运输问题 (网络流,最小费用最大流) Description W 公司有m个仓库和n个零售商店.第i个仓库有\(a_i\)个单位的货物:第j个零售商店需要\( ...
- LibreOJ #6191. 「美团 CodeM 复赛」配对游戏
二次联通门 : LibreOJ #6191. 「美团 CodeM 复赛」配对游戏 /* LibreOJ #6191. 「美团 CodeM 复赛」配对游戏 概率dp */ #include <cs ...
- liberOJ#6006. 「网络流 24 题」试题库 网络流, 输出方案
#6006. 「网络流 24 题」试题库 题目描述 假设一个试题库中有 n nn 道试题.每道试题都标明了所属类别.同一道题可能有多个类别属性.现要从题库中抽取 m mm 道题组成试卷.并要求 ...
- LibreOJ #6192. 「美团 CodeM 复赛」城市网络
#6192. 「美团 CodeM 复赛」城市网络 内存限制:64 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: sqc 提交提交记录统计讨论测试数据 题目描 ...
随机推荐
- web service,soap ,http,tcp,udp
webservice and soap HTTP只负责把数据传送过去,不会管这个数据是XML.HTML.图片.文本文件或者别的什么.而SOAP协议则定义了怎么把一个对象变成XML文本,在远程如何调用 ...
- Push API
[Push API] The Push API gives web applications the ability to receive messages pushed to them from a ...
- django搭建的站点,通过localhost能访问,但是通过ip不能访问
问题:使用ip访问不了django站点,只能用127.0.0.1访问 解决方法:启动服务时ip使用0.0.0.0 使用gunicorn启动 gunicorn -w4 -b0.0.0.0:8 ...
- java多线程与并发笔记
0.多线程,主要用来提高程序效率,处理耗时的操作. 多个线程写在同一个类里调用,并不是说写在前面的线程就会先运行.各个线程会进行争抢,能抢到系统资源的才会先运行. 因此,同一个程序,多个线程运行,可能 ...
- as3.0控制声音大小
//加载声音 var sound:Sound=new Sound(); sound.load(new URLRequest("1.mp3")): //声明声道 var soundC ...
- codeforces 722D Generating Sets 【优先队列】
You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive ...
- js 小结
<script type="text/javascript"> var hotalAddJs = { makeSubmitDataHandler: function ( ...
- vue 父子组件相互传参
转自https://blog.csdn.net/u011175079/article/details/79161029 子组件: <template> <div> <di ...
- 【python】初识python
[命名规范] 模块名:小写字母,单词之间用_分割:例如:ad_stats.py 包名:和模块名一样 类名:单词首字母大写:例如:ConfigUtil 全局变量名:大写字母,单词之间用_分割:例如:NU ...
- mysql 索引 create_time 加explain关键字是否走索引
SELECT * FROM t_user WHERE email='217@xxg.com'; --1.725 --加email索引之后 0.003 SELECT * FROM t_user WHE ...