洛谷P3254 圆桌问题(最大流)
一道良心啊……没那么多麻烦了……
从$S$向所有单位连边,容量为单位人数,从所有桌子向$T$连边,容量为桌子能坐的人数,从每一个单位向所有桌子连边,容量为$1$,然后跑一个最大流,看一看$S$到单位这一边流满了没,如果没有就无解。方案的话,就看单位到哪一个桌子有流就行
因为手写队列然后两个$t$弄混了结果死都不知道哪里错……
//minamoto
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
char sr[<<],z[];int C=-,Z;
inline void Ot(){fwrite(sr,,C+,stdout),C=-;}
inline void print(int x){
if(C><<)Ot();
while(z[++Z]=x%+,x/=);
while(sr[++C]=z[Z],--Z);
}
const int N=,M=;
int head[N],Next[M],ver[M],edge[M],cur[N],tot=;
int dep[N],n,m,u,v,e,vis[N],s,t,sum=;
queue<int> q;
inline void add(int u,int v,int e){
ver[++tot]=v,Next[tot]=head[u],head[u]=tot,edge[tot]=e;
ver[++tot]=u,Next[tot]=head[v],head[v]=tot,edge[tot]=;
}
bool bfs(){
memset(dep,-,sizeof(dep));
while(!q.empty()) q.pop();
for(int i=;i<=n+m+;++i) cur[i]=head[i];
q.push(s),dep[s]=;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=Next[i]){
int v=ver[i];
if(dep[v]<&&edge[i])
dep[v]=dep[u]+,q.push(v);
}
}
return ~dep[t];
}
int dfs(int u,int limit){
if(!limit||u==t) return limit;
int flow=,f;
for(int i=cur[u];i;i=Next[i]){
int v=ver[i];cur[u]=i;
if(dep[v]==dep[u]+&&(f=dfs(v,min(limit,edge[i])))){
flow+=f,limit-=f;
edge[i]-=f,edge[i^]+=f;
if(!limit) break;
}
}
return flow;
}
int dinic(){
int flow=;
while(bfs()) flow+=dfs(s,inf);
return flow;
}
int main(){
m=read(),n=read();
s=,t=n+m+;
for(int i=;i<=m;++i){
e=read(),add(s,i,e),sum+=e;
for(int j=;j<=n;++j)
add(i,j+m,);
}
for(int i=;i<=n;++i){
e=read();add(i+m,t,e);
}
if(dinic()!=sum) return puts(""),;
print(),sr[++C]='\n';
for(int i=;i<=m;++i){
for(int j=head[i];j;j=Next[j]){
if(!edge[j]&&ver[j]>m&&ver[j]<=n+m)
print(ver[j]-m),sr[++C]=' ';
}
sr[++C]='\n';
}
Ot();
return ;
}
洛谷P3254 圆桌问题(最大流)的更多相关文章
- 洛谷P3254 圆桌问题(最大流)
题意 $m$个不同单位代表参加会议,第$i$个单位有$r_i$个人 $n$张餐桌,第$i$张可容纳$c_i$个代表就餐 同一个单位的代表需要在不同的餐桌就餐 问是否可行,要求输出方案 Sol 比较zz ...
- 洛谷 P3254 圆桌问题【最大流】
s向所有单位连流量为人数的边,所有饭桌向t连流量为饭桌容量的边,每个单位向每个饭桌连容量为1的边表示这个饭桌只能坐这个单位的一个人.跑dinic如果小于总人数则无解,否则对于每个单位for与它相连.满 ...
- 洛谷 [P3254] 圆桌问题
简单最大流建图 #include <iostream> #include <cstdio> #include <cstring> #include <cmat ...
- 洛谷.3254.圆桌问题(最大流ISAP)
题目链接 日常水题 还是忍不住吐槽这题奇怪的评价 #include <cstdio> #include <cctype> #include <algorithm> ...
- [洛谷P3254]圆桌问题
题目大意:有$m$个单位,每个单位有$r_i$个代表,有$n$张餐桌,每张餐桌可容纳$c_i$个代表.要求同一个单位的代表不在同一个餐桌就餐.若可以,输出$1$以及其中一种方案,否则输出$0$ 题解: ...
- 洛谷P3254 圆桌问题 网络流_二分图
Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> ...
- Luogu P3254 圆桌问题(最大流)
P3254 圆桌问题 题面 题目描述 假设有来自 \(m\) 个不同单位的代表参加一次国际会议.每个单位的代表数分别为 \(r_i (i =1,2,--,m)\) . 会议餐厅共有 \(n\) 张餐桌 ...
- 洛谷.4015.运输问题(SPFA费用流)
题目链接 嗯..水题 洛谷这网络流二十四题的难度评价真神奇.. #include <queue> #include <cstdio> #include <cctype&g ...
- [洛谷P3254] [网络流24题] 圆桌游戏
Description 假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri (i =1,2,--,m). 会议餐厅共有n 张餐桌,每张餐桌可容纳ci (i =1,2,--,n) ...
随机推荐
- Python Twisted系列教程14:Deferred用于同步环境
作者:dave@http://krondo.com/when-a-deferred-isnt/ 译者:杨晓伟(采用意译) 你可以从这里从头开始阅读这个系列. 介绍 这部分我们要介绍Deferred的 ...
- Python Twisted系列教程12:改进诗歌下载服务器
作者:dave@http://krondo.com/a-poetry-transformation-server/ 译者:杨晓伟(采用意译) 你可以从这里从头阅读这个系列. 新的服务器实现 这里我们 ...
- Python Twisted网络编程框架与异步编程入门教程
原作出处:twisted-intro-cn 作者:Dave 译者:杨晓伟 luocheng likebeta 转载声明:版权归原作出处所有,转载只为让更多人看到这部优秀作品合集,如果侵权,请留言告知 ...
- Android 4 学习(14):Internet Resources
参考<Professional Android 4 Development> 使用Internet资源 打开URI String myFeed = getString(R.string.m ...
- 手机自带输入法emoji表情的输入,提交及显示——前端解决方案
体验更优排版请移步原文:http://blog.kwin.wang/programming/emoji-transform-commit.html 之前就遇到过需要前端支持用户输入并提交emoji表情 ...
- 监控Mongo慢查询
监控Mongo慢查询 1. 使用mongostat监控MongoDB全局情况 mongostat是mongdb自带的状态检测工具,在命令行下使用.它会间隔固定时间获取MongoDB的当前运行状态,并输 ...
- spring cloud微服务搭建(一)
martin fowler大神提出微服务的概念后,各种微服务的技术满天飞,现在用的比较多的是spring cloud和阿里的dubbo,由于dubbo 在16年10月份就停止更新了,不过好像前些天又更 ...
- git rm简介
本文翻译整理自:http://web.mit.edu/~mkgray/project/silk/root/afs/sipb/project/git/git-doc/git-rm.html 在git中我 ...
- 分布式缓存产品Redis和memcached比较区别(图)
- WebFlux03 SpringBoot WebFlux实现CRUD
1 准备 基于SpringBoot2.0搭建WebFlux项目,详情请参见三少另外一篇博文 2 工程结构 <?xml version="1.0" encoding=" ...