P2762 太空飞行计划问题 最大权闭合子图
link:https://www.luogu.org/problemnew/show/P2762
题意
承担实验赚钱,但是要花去对应仪器的费用,仪器可能共用。
求最大的收益和对应的选择方案。
思路
这道题读入有点技巧,就是要自己判断换行
这道题和费用流关系不大,是最大权闭合子图,源点连接实验,容量为收益,实验向对应的仪器连接容量为inf的边,仪器向汇点连接容量为费用的边。
跑出最小割s,即最大流,然后用实验总收益 - s即可。
至于如何输出方案,即输出最大权闭合子图中的点。就是跑最后一次bfs的时候,有dis的点就是选中的点。
结合图形我们可以这么理解,从源点流到实验的边一定不能流满,要是满了,就说明这个做这个实验没有任何意义。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
\\ Λ_Λ 来了老弟
\('ㅅ')
> ⌒ヽ
/ へ\
/ / \\
レ ノ ヽ_つ
/ /
/ /|
( (ヽ
| |、\
| 丿 \ ⌒)
| | ) /
'ノ ) Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 1e4+;
struct E{
int u,v,w;
int nxt;
}edge[maxn];
int gtot = ,head[maxn];
void addedge(int u,int v,int w){
edge[gtot].u = u;
edge[gtot].v = v;
edge[gtot].w = w;
edge[gtot].nxt = head[u];
head[u] = gtot++; edge[gtot].u = v;
edge[gtot].v = u;
edge[gtot].w = ;
edge[gtot].nxt = head[v];
head[v] = gtot++;
}
int dis[maxn],cur[maxn];
bool bfs(int s,int t){
rep(i, s, t) cur[i] = head[i];
memset(dis, inf, sizeof(dis));
dis[s] = ;
queue<int>que; que.push(s);
while(!que.empty()){
int u = que.front(); que.pop();
for(int i = head[u]; ~i; i = edge[i].nxt){
int v = edge[i].v, w = edge[i].w;
if(w > && dis[v] > dis[u] + ){
dis[v] = dis[u] + ;
que.push(v);
}
} }
return dis[t] < inf;
} int dfs(int u,int t,int maxflow){
if(u == t || maxflow == ) return maxflow; for(int i=cur[u]; ~i; i = edge[i].nxt){
cur[u] = i;
int v = edge[i].v, w = edge[i].w;
if(w > && dis[v] == dis[u] + ) {
int f = dfs(v, t, min(w, maxflow));
if(f > ) {
edge[i].w -= f;
edge[i^].w += f;
return f;
}
}
}
return ;
} int dinic(int s,int t){
int flow = ;
while(bfs(s, t)){
while(int f = dfs(s, t, inf)) flow += f;
}
return flow;
}
char tools[];
int init[maxn];
int main(){ memset(head,-, sizeof(head));
int n,m;
scanf("%d%d", &n, &m);
int s = , t = n+m+;
int sum = ;
for(int i=; i<=n; i++) {
int x; scanf("%d", &x);
addedge(s, i, x);
init[i] = x;
sum += x; memset(tools,,sizeof tools);
cin.getline(tools,);
int ulen=,tool;
while (sscanf(tools+ulen,"%d",&tool)==)//之前已经用scanf读完了赞助商同意支付该实验的费用
{//tool是该实验所需仪器的其中一个
//这一行,你可以将读进来的编号进行储存、处理,如连边。 addedge(i, n+tool,inf);
// cout<<tool<<endl;
if (tool==)
ulen++;
else {
while (tool) {
tool/=;
ulen++;
}
}
ulen++;
}
}
for(int i=; i<=m; i++){
int x; scanf("%d", &x);
addedge(i+n, t, x);
init[i+n] = x;
}
int ans = sum - dinic(s, t); for(int i=; i <= n; i++) {
if(dis[i] < inf) printf("%d ", i);
}
puts(""); for(int i=; i<=m; i++){
if(dis[i+n] < inf) printf("%d ", i);
}
puts("");
printf("%d\n", ans);
return ;
}
P2762 太空飞行计划问题 最大权闭合子图的更多相关文章
- 洛谷 P4174 [NOI2006]最大获利 && 洛谷 P2762 太空飞行计划问题 (最大权闭合子图 && 最小割输出任意一组方案)
https://www.luogu.org/problemnew/show/P4174 最大权闭合子图的模板 每个通讯站建一个点,点权为-Pi:每个用户建一个点,点权为Ci,分别向Ai和Bi对应的点连 ...
- 【Luogu】P2762太空飞行计划(最大权闭合图)
题目链接 woc这题目的输入格式和输出格式真的恶心 首先我们就着样例讲一下闭合图 如图所示,第一层是两个实验节点,带来正收益:第二层是三个仪器节点,带来负收益:问讲道理到终点可以获得多大收益. 闭合图 ...
- 洛谷P2762 太空飞行计划问题(最大权闭合图)
题意 有$m$个实验,$n$中器材,每个实验需要使用一些器材 每个实验有收入,每个器材有花费 最大化收入 - 花费 Sol 最大权闭合图的经典应用 从$S$向每个实验连流量为该实验收入的边 从每个器材 ...
- LuoguP2762 太空飞行计划问题(最大权闭合子图,最小割)
题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,Em},和进行这些实验需要使用的全部仪器的 ...
- 洛谷 P2762 太空飞行计划问题 P3410 拍照【最大权闭合子图】题解+代码
洛谷 P2762 太空飞行计划问题 P3410 拍照[最大权闭合子图]题解+代码 最大权闭合子图 定义: 如果对于一个点集合,其中任何一个点都不能到达此集合以外的点,这就叫做闭合子图.每个点都有一个权 ...
- 网络流24题:P2762 太空飞行计划问题
P2762 太空飞行计划问题 题目背景 题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,E ...
- 洛谷 P2762 太空飞行计划问题 【最大权闭合子图+最小割】
--一道难在读入的题. 最后解决方案直接getline一行然后是把读优拆掉放进函数,虽然很丑但是过了. 然后就是裸的最大权闭合子图了,把仪器当成负权点向t连流量为其价格的边,s向实验连流量为实验报酬的 ...
- 【网络流24题】 No.2 太空飞行计划问题 (最大闭合权图 最大流 )
原题: W教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合 E={E1,E2,...,Em},和进行这些实验需 ...
- 洛谷 [P2762] 太空飞行计划问题
最大权闭合子图 胡伯涛论文真是个好东西.jpg 求一个有向图的最大权闭合子图,常应用于有先决条件的最优化问题中 将所有正权点与源点相连,容量为点权; 将所有负权点与汇点相连,容量为点权的相反数; 将原 ...
随机推荐
- Linux下安装配置Jmeter5.1,并执行jmx文件
Windows下的jmeter是GUI模式,可查看操作,但是GUI对性能的干扰比较大,所有一般压测会在Linux上运行. 下面是Linux下安装配置Jmeter5.1,并执行jmx文件的步骤, 一.安 ...
- 【eclipse】No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A
用 eclipse 写 Java 代码时出现了这个问题,详细如下: No enclosing instance of type TestParsingLinkedList is accessible. ...
- How to extract WeChat chat messages from a smartphone running Android 7.x or above
A friend of mine she was frustarted in extracting WeChat chat messages from suspect's smartphone run ...
- S2:ArrayList
1.ArrayList ArrayList非常类似于数组,也有人称它为数组列表,ArrayList可以动态维护. 因为数组的长度是固定的,而SArrayList的容量可以根据需要自动扩充. Arr ...
- Unity基础之:UnityAPI的学习
版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...
- jQuery插件之路(二)——轮播
还记得以前刚接触前端的时候,浏览各大网站,很多都少不了的有个轮播的效果,那个时候自己是一个彻彻底底的小白,想着这些图片滚动起来还真是有意思,是什么让这些图片在一个方向上连续的滚动呢.后来慢慢的接触多了 ...
- linux下安装开发环境
jdk 下载jdk安装包,解压到/usr/java/jdk 配置环境变量: #vi /etc/profile 在该profile文件中最下面添加: JAVA_HOME=/usr/java/jdk1.7 ...
- axios配置请求头content-type
现在前端开发中需要通过Ajax发送请求获取后端数据是很普遍的一件事情了,鉴于我平时在撸码中用的是vue技术栈,今天这里来谈谈我们常用的发Ajax请求的一个插件—axios.> 现在网上可能发送A ...
- 实现API优先设计的重要性和实现方式
应用API优先的方法意味着设计API时,使其具有一致性和适应性,无论应用于哪些开发项目.对API使用API描述语言(如OpenAPI)是关键,因为它有助于建立API与其他程序通信的枢纽,即使这些系 ...
- Linux安装nfs共享文件
简介nfs nfs网络文件系统常用于共享音视频,图片等静态资源.将需要共享的资源放到NFS里的共享目录,通过服务器挂载实现访问. 服务端安装: yum install -y nfs-utils rpc ...