2176. 太空飞行计划问题

题目链接

2176. 太空飞行计划问题

\(W\) 教授正在为国家航天中心计划一系列的太空飞行。

每次太空飞行可进行一系列商业性实验而获取利润。

现已确定了一个可供选择的实验集合 \(E=\{E_1,E_2,\dots,E_m\}\) 和进行这些实验需要使用的全部仪器的集合 \(I=\{I_1,I_2,\dots,I_n\}\)

实验 \(E_j\) 需要用到的仪器是 \(I\) 的子集 \(R_j \subseteq I\)

配置仪器 \(I_k\) 的费用为 \(c_k\) 美元。

实验 \(E_j\) 的赞助商已同意为该实验结果支付 \(p_j\) 美元。

\(W\) 教授的任务是找出一个有效算法,确定在一次太空飞行中要进行哪些实验并因此而配置哪些仪器才能使太空飞行的净收益最大。

这里净收益是指进行实验所获得的全部收入与配置仪器的全部费用的差额。

对于给定的实验和仪器配置情况,编程找出净收益最大的试验计划。

输入格式

\(1\) 行有 \(2\) 个正整数 \(m\)\(n\)\(m\) 是实验数,\(n\) 是仪器数。

接下来的 \(m\) 行,每行是一个实验的有关数据。

第一个数赞助商同意支付该实验的费用;接着是该实验需要用到的若干仪器的编号。

最后一行的 \(n\) 个数是配置每个仪器的费用。

实验和仪器的编号都是从 \(1\) 开始。

输出格式

将最佳实验方案输出。

\(1\) 行是实验编号;

\(2\) 行是仪器编号;

最后一行是净收益。

如果最佳方案不唯一,则输出任意一种均可。

数据范围

\(1 \le m,n \le 50\),
所有仪器费用以及赞助费用均不超过 \(100\)

输入样例:

2 3
10 1 2
25 2 3
5 6 7

输出样例:

1 2
1 2 3
17

解题思路

最小割

一个点向其他点连边,如果该点要选的话则其连边的也必须要选,即最大权闭合图模板题,具体见 961. 最大获利
另外,还需要输出方案,\(S-{s}\) 即为闭合子图,dfs\(S\) 部分即可

  • 时间复杂度:\(O((n+m)^2\times nm)\)

代码

// Problem: 太空飞行计划问题
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/2178/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=105,M=(N+2505)*2,inf=1e9;
int m,n,S,T;
int h[N],f[M],ne[M],e[M],idx;
int d[N],hh,tt,q[N],cur[N];
bool v[N];
void add(int a,int b,int c)
{
	e[idx]=b,f[idx]=c,ne[idx]=h[a],h[a]=idx++;
	e[idx]=a,f[idx]=0,ne[idx]=h[b],h[b]=idx++;
}
bool bfs()
{
	memset(d,-1,sizeof d);
	d[S]=hh=tt=0;
	q[0]=S;
	cur[S]=h[S];
	while(hh<=tt)
	{
		int x=q[hh++];
		for(int i=h[x];~i;i=ne[i])
		{
			int y=e[i];
			if(d[y]==-1&&f[i])
			{
				d[y]=d[x]+1;
				cur[y]=h[y];
				if(y==T)return true;
				q[++tt]=y;
			}
		}
	}
	return false;
}
int dfs(int x,int limit)
{
	if(x==T)return limit;
	int flow=0;
	for(int i=cur[x];~i&&flow<limit;i=ne[i])
	{
		cur[x]=i;
		int y=e[i];
		if(d[y]==d[x]+1&&f[i])
		{
			int t=dfs(y,min(f[i],limit-flow));
			if(!t)d[y]=-1;
			f[i]-=t,f[i^1]+=t,flow+=t;
		}

	}
	return flow;
}
int dinic()
{
	int res=0,flow;
	while(bfs())while(flow=dfs(S,inf))res+=flow;
	return res;
}
void dfs(int x)
{
	v[x]=true;
	for(int i=h[x];~i;i=ne[i])
		if(f[i]&&!v[e[i]])dfs(e[i]);
}
int main()
{
	memset(h,-1,sizeof h);
    scanf("%d%d",&m,&n);
    getchar();
    S=0,T=n+m+1;
    string s;
    int x,tot=0;
    for(int i=1;i<=m;i++)
    {
    	getline(cin,s);
    	stringstream sin(s);
    	sin>>x;
    	add(S,i,x);
    	tot+=x;
    	while(sin>>x)add(i,x+m,inf);
    }
    for(int i=1;i<=n;i++)
    {
    	scanf("%d",&x);
    	add(i+m,T,x);
    }
    int res=dinic();
    dfs(S);
    for(int i=1;i<=m;i++)
    	if(v[i])printf("%d ",i);
    puts("");
    for(int i=1;i<=n;i++)
    	if(v[i+m])printf("%d ",i);
    puts("");
    printf("%d",tot-res);
    return 0;
}
posted @ 2022-12-01 11:00  zyy2001  阅读(70)  评论(0编辑  收藏  举报