洛谷 [P3254] 圆桌问题
简单最大流建图
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN=600,MAXM=2000005;
int head[MAXN],cur[MAXN],n,m,s,t,nume,num1[MAXN],num2[MAXN],maxflow,dep[MAXN];
queue<int> q;
struct edge{
int to,nxt,flow,cap;
}e[MAXM];
void adde(int from,int to,int cap){
e[++nume].to=to;
e[nume].nxt=head[from];
head[from]=nume;
e[nume].cap=cap;
}
bool bfs(){
memset(dep,0,sizeof(dep));
q.push(s);dep[s]=1;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(!dep[v]&&e[i].flow<e[i].cap){
dep[v]=dep[u]+1;
q.push(v);
}
}
}
return dep[t];
}
int dfs(int u,int flow){
if(u==t) return flow;
int tot=0;
for(int &i=cur[u];i&&tot<flow;i=e[i].nxt){
int v=e[i].to;
if(dep[v]==dep[u]+1&&e[i].flow<e[i].cap){
if(int t=dfs(v,min(flow-tot,e[i].cap-e[i].flow))){
e[i].flow+=t;
e[((i-1)^1)+1].flow-=t;
tot+=t;
}
}
}
return tot;
}
void dinic(){
while(bfs()){
for(int i=s;i<=t;i++) cur[i]=head[i];
maxflow+=dfs(s,0x3f3f3f3f);
}
}
int main(){
cin>>m>>n;
int tot=0;
for(int i=1;i<=m;i++){
cin>>num1[i];
if(num1[i]>n){
cout<<0<<endl;
return 0;
}
tot+=num1[i];
}
for(int i=1;i<=n;i++) cin>>num2[i];
s=0;t=m+n+1;
for(int i=1;i<=m;i++) adde(s,i,num1[i]),adde(i,s,0);
for(int i=1;i<=n;i++) adde(i+m,t,num2[i]),adde(t,i+m,0);
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++) adde(i,j+m,1),adde(j+m,i,0);
}
dinic();
if(maxflow!=tot) cout<<0<<endl;
else{
cout<<1<<endl;
for(int i=1;i<=m;i++){
for(int j=head[i];j;j=e[j].nxt){
if(e[j].flow&&e[j].to) printf("%d ",e[j].to-m);
}
printf("\n");
}
}
return 0;
}
洛谷 [P3254] 圆桌问题的更多相关文章
- 洛谷P3254 圆桌问题(最大流)
传送门 一道良心啊……没那么多麻烦了…… 从$S$向所有单位连边,容量为单位人数,从所有桌子向$T$连边,容量为桌子能坐的人数,从每一个单位向所有桌子连边,容量为$1$,然后跑一个最大流,看一看$S$ ...
- [洛谷P3254]圆桌问题
题目大意:有$m$个单位,每个单位有$r_i$个代表,有$n$张餐桌,每张餐桌可容纳$c_i$个代表.要求同一个单位的代表不在同一个餐桌就餐.若可以,输出$1$以及其中一种方案,否则输出$0$ 题解: ...
- 洛谷P3254 圆桌问题(最大流)
题意 $m$个不同单位代表参加会议,第$i$个单位有$r_i$个人 $n$张餐桌,第$i$张可容纳$c_i$个代表就餐 同一个单位的代表需要在不同的餐桌就餐 问是否可行,要求输出方案 Sol 比较zz ...
- 洛谷 P3254 圆桌问题【最大流】
s向所有单位连流量为人数的边,所有饭桌向t连流量为饭桌容量的边,每个单位向每个饭桌连容量为1的边表示这个饭桌只能坐这个单位的一个人.跑dinic如果小于总人数则无解,否则对于每个单位for与它相连.满 ...
- 洛谷P3254 圆桌问题 网络流_二分图
Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> ...
- [洛谷P3254] [网络流24题] 圆桌游戏
Description 假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri (i =1,2,--,m). 会议餐厅共有n 张餐桌,每张餐桌可容纳ci (i =1,2,--,n) ...
- 洛谷.3254.圆桌问题(最大流ISAP)
题目链接 日常水题 还是忍不住吐槽这题奇怪的评价 #include <cstdio> #include <cctype> #include <algorithm> ...
- 洛谷1640 bzoj1854游戏 匈牙利就是又短又快
bzoj炸了,靠离线版题目做了两道(过过样例什么的还是轻松的)但是交不了,正巧洛谷有个"大牛分站",就转回洛谷做题了 水题先行,一道傻逼匈牙利 其实本来的思路是搜索然后发现写出来类 ...
- 洛谷P1352 codevs1380 没有上司的舞会——S.B.S.
没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description Ural大学有N个职员,编号为1~N.他们有 ...
随机推荐
- sscanf()用法
http://blog.chinaunix.net/uid-26284412-id-3189214.html #include<cstdio> #include<cstring> ...
- JAVA多线程之CountDownLatch
前序: 上周测试给开发的同事所开发的模块提出了一个bug,并且还是偶现. 经过仔细查看代码,发现是在业务中启用了多线程,2个线程同时跑,但是新启动的2个线程必须保证一个完成之后另一个再继续运行,才能消 ...
- Cookie实现--用户上次访问时间
用户上次访问时间
- TPshop学习(8)微信支付
http://blog.csdn.net/phper8/article/details/76383415 学习内容: https://www.kancloud.cn/tpshop/thinkphp5/ ...
- php短信接口代码
这篇文章主要为大家详细介绍了php短信接口代码,php短信发送.php批量发送.php获取余额等代码,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了几个常用的php短信接口代码,供大家参考,具体内 ...
- PHP error_reporting() 错误控制函数功能详解
定义和用法: error_reporting() 设置 PHP 的报错级别并返回当前级别. 函数语法: error_reporting(report_level) 如果参数 level 未指定 ...
- 怎么去掉织梦网站首页带的index.html/index.php
方法1. 1)在空间面板里面找到默认首页设置: 我们是需要去掉index.html,这时我们只需要把index.html这个把它移到最顶级去就行,然后点击确定,在打开网站刷新下,就基本可以解决了! 其 ...
- dede织梦栏目页和文章页中获取当前栏目名称方法
一般情况下,在dede织梦系统中列表页.栏目页和文章页中获取当前所在栏目名称只需要代码:{dede:type}[field:typename]{/dede:type}即可,不需要定义ID,默认的就是当 ...
- DALI调色温
DALI调色温模块使用手册 公 司: 深圳市万秀电子有限公司 网 站: http://www.wanxiucx.com 总 机: 0755-23215689 联系人: 张先生 手 机: 139 ...
- Python解析JSON详解
JSON 函数 使用 JSON 函数需要导入 json 库:import json. 函数 描述 json.dumps 将 Python 对象编码成 JSON 字符串 json.loads 将已 ...