洛谷P3254 圆桌问题 网络流_二分图
Code:
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=1004;
const int INF=10000000;
# define pb push_back
int s,t;
struct Edge{
int from,to,cap;
Edge(int u,int v,int c):from(u),to(v),cap(c) {}
};
vector<Edge>edges;
vector<int>G[maxn];
struct Dicnic{
int d[maxn],vis[maxn],cur[maxn];
queue<int>Q;
void addedge(int u,int v,int c){
edges.pb(Edge(u,v,c)); //正向弧
edges.pb(Edge(v,u,0)); //反向弧
int m=edges.size();
G[u].pb(m-2);
G[v].pb(m-1);
}
int BFS()
{
memset(vis,0,sizeof(vis));
d[s]=0,vis[s]=1;Q.push(s);
while(!Q.empty()){
int u=Q.front();Q.pop();
int sz=G[u].size();
for(int i=0;i<sz;++i){
Edge e=edges[G[u][i]];
if(!vis[e.to]&&e.cap>0){
d[e.to]=d[u]+1,vis[e.to]=1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a){
if(x==t)return a;
int sz=G[x].size();
int f,flow=0;
for(int i=0;i<sz;++i){
Edge e=edges[G[x][i]];
cur[x]=i;
if(d[e.to]==d[x]+1&&e.cap>0){
f=dfs(e.to,min(a,e.cap));
if(f)
{
int u=G[x][i];
a-=f;
edges[u].cap-=f;
edges[u^1].cap+=f;
flow+=f;
if(a==0)break;
}
}
}
return flow;
}
int maxflow(){
int ans=0;
while(BFS()){
memset(cur,0,sizeof(cur));
ans+=dfs(s,INF);
}
return ans;
}
}op;
int main()
{
int m,n,sum=0;
scanf("%d%d",&m,&n);
s=0,t=m+n+1;
for(int i=1;i<=m;++i)
{
int a;scanf("%d",&a);
op.addedge(s,i,a);
sum+=a;
for(int j=m+1;j<=m+n;++j)op.addedge(i,j,1);
}
for(int i=1;i<=n;++i)
{
int a;scanf("%d",&a);
op.addedge(i+m,t,a);
}
int F=op.maxflow();
if(F!=sum){printf("0\n");return 0;}
printf("1\n");
for(int u=1;u<=m;++u)
{
int sz=G[u].size();
for(int i=0;i<sz;++i)
{
int e=G[u][i];
if(e%2==0&&edges[e].cap==0)printf("%d ",edges[e].to-m);
}
printf("\n");
}
return 0;
}
洛谷P3254 圆桌问题 网络流_二分图的更多相关文章
- 洛谷 [P3254] 圆桌问题
简单最大流建图 #include <iostream> #include <cstdio> #include <cstring> #include <cmat ...
- 洛谷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 圆桌问题 网络流
P3254 圆桌问题 #include <bits/stdc++.h> using namespace std; , inf = 0x3f3f3f; struct Edge { int f ...
- [洛谷P3254] [网络流24题] 圆桌游戏
Description 假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri (i =1,2,--,m). 会议餐厅共有n 张餐桌,每张餐桌可容纳ci (i =1,2,--,n) ...
- 洛谷P2756 飞行员配对方案问题 网络流_二分图
Code: #include<cstdio> #include<queue> #include<vector> #include<cstring> #i ...
- 洛谷P2055 [ZJOI2009]假期的宿舍 [二分图最大匹配]
题目描述 学校放假了 · · · · · · 有些同学回家了,而有些同学则有以前的好朋友来探访,那么住宿就是一个问题.比如 A 和 B 都是学校的学生,A 要回家,而 C 来看B,C 与 A 不认识. ...
随机推荐
- android UI卡顿问题学习
转自https://blog.csdn.net/joye123/article/details/79425398 https://blog.csdn.net/zhenjie_chang/article ...
- LeetCode(96)Unique Binary Search Trees
题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...
- Unity坐标系 左手坐标系 图
x轴:从左指向右 y轴:从下指向上 z轴:指向屏幕里的是左手坐标系,指向屏幕外的是右手坐标系 记忆小技巧:都是X轴朝右,Y轴向上,跟平时画坐标一模一样,区别只是Z的朝向.你用手试一下就知道了,当大拇指 ...
- Guitar Pro 的双十一特惠活动,正在如火如荼进行中...
11月11日这个令人兴奋的日子又来了.没错,“双十一”所有网购达人狂欢的日子.同时期待已久的Guitar Pro 也将在“双十一”当天,把福利分享与你我.11月11日Guitar Pro 将在麦软商城 ...
- Spark作业执行
Spark中一个action触发一个job的执行,在job提交过程中主要涉及Driver和Executor两个节点. Driver主要解决 1. RDD 依赖性分析,生成DAG. 2. 根据RDD D ...
- 【Jim】I am back (ง •_•)ง
其实上周就来考过一次试了,真是啥都忘了 (´ー∀ー`) 下午在写[树网的核],写了一半去吃饭,回来时发现高二机房的门被锁上了,于是他们都被堵在门口. 我就回到我的地方接着写码. 听到外面有个高二的妹子 ...
- 使用yum方式安装mysql5.6
1.新开的云服务器,需要检测系统是否自带安装mysql # yum list installed | grep mysql 2.如果发现有系统自带mysql,果断这么干 # yum -y remove ...
- Python及相应软件安装
Python安装 这是下载地址:Linux下载链接,windows下载链接 1.下载压缩包 wget https://www.python.org/ftp/python/3.7.1/Python-3. ...
- Python学习笔记(7)字典
2019-03-07 字典(dict): (1)字典用大括号({})定义,字典由多个键及其对应的值组合而成,每一对键值组合称为项. (2)字典的键唯一,但是值可以是任何(不可变的)数据类型(整型,字符 ...
- 洛谷 P1313 计算系数 (二项式定理)
这道题正好复习了二项式定理 所以答案就是a^n * b^m * c(n, k) 然后注意一些细节 我一开始写组合数只写一行的组合数 即c[0] = 1; c[i] = c[i-1] * (n - i ...