太空飞行计划问题
Description
W
教授正在为国家航天中心计划一系列的太空飞行。
每次太空飞行可进行一系列商业性实验而获取利润。
现已确定了一个可供选择的实验集合
和进行这些实验需要使用的全部仪器的集合
I={I1,I2,…,In}
。
实验
Ej
需要用到的仪器是
I
的子集
实验
Ej
的赞助商已同意为该实验结果支付
pj
美元。
W
教授的任务是找出一个有效算法,确定在一次太空飞行中要进行哪些实验并因此而配置哪些仪器才能使太空飞行的净收益最大。
这里净收益是指进行实验所获得的全部收入与配置仪器的全部费用的差额。
对于给定的实验和仪器配置情况,编程找出净收益最大的试验计划。
Input
第
接下来的
第一个数赞助商同意支付该实验的费用;
接着是该实验需要用到的若干仪器的编号。
最后一行的
n
个数是配置每个仪器的费用。
Output
第
第
2
<script type="math/tex" id="MathJax-Element-19">2</script> 行是仪器编号;
最后一行是净收益。
Sample Input
2 3
10 1 2
25 2 3
5 6 7
Sample Output
1 2
1 2 3
17
HINT
数据范围很小啦
Solution
因为是网络流里面的题,所以肯定要用网络流来做了啊。
弄个源点和一个汇点。
将源点连接每个实验,流量为实验的收益。
将每个仪器连接汇点,流量为装置的花费。
∵ 净收益=选择的实验的收益-选择的装置的花费。
∴ 净收益=(所有实验的收益-未选择的实验的收益)-选择的装置的花费。
即净收益=所有实验的收益-(未选择的实验的收益+选择的装置的花费(②))。
∵ 所有实验的收益是以给固定的值,所以我们只要使 ② 最小即可。
对应到网络中,我们发现 ② 正好是网络的一个割。
所以只要求最小割就行了。
Code
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <queue>
- #define Min(x,y) ((x)<(y)?(x):(y))
- using namespace std;
- const int INF=0x3f3f3f3f;
- int m,n,l,s,t,cnt;
- int low[1000],head[100],nxt[1000],data[1000];
- int w[100];
- int c[100];
- int to[100][100];
- int dis[100];
- char str[10000];
- queue<int>q;
- void add(int x,int y,int z){
- nxt[cnt]=head[x];data[cnt]=y;low[cnt]=z;head[x]=cnt++;
- nxt[cnt]=head[y];data[cnt]=x;low[cnt]=0;head[y]=cnt++;
- }
- bool BFS(){
- memset(dis,-1,sizeof dis);
- q.push(s);dis[s]=0;
- while(!q.empty()){
- int now=q.front();q.pop();
- for(int i=head[now];i!=-1;i=nxt[i])
- if(low[i]&&dis[data[i]]<0){dis[data[i]]=dis[now]+1;q.push(data[i]);}
- }
- return dis[t]>0;
- }
- int dfs(int now,int flow){
- if(now==t)return flow;
- int Flow;
- for(int i=head[now];i!=-1;i=nxt[i]){
- if(low[i]&&dis[data[i]]==dis[now]+1){
- if(Flow=dfs(data[i],Min(flow,low[i]))){
- low[i]-=Flow;
- low[i^1]+=Flow;
- return Flow;
- }
- }
- }
- return 0;
- }
- int main(){
- int weight=0;
- scanf(”%d%d”,&m,&n);
- memset(head,-1,sizeof head);
- s=n+m+1,t=n+m+2;
- for(int i=1;i<=m;i++){
- scanf(”%d”,&w[i]);
- weight+=w[i];
- gets(str);
- l=strlen(str);
- int pos=0;
- while(pos<l){
- if(str[pos]<‘0’||str[pos]>‘9’){
- while(str[pos]<‘0’||str[pos]>‘9’&&pos<l){pos++;}
- if(pos==l)break;
- to[i][0]++;
- }
- to[i][to[i][0]]=to[i][to[i][0]]*10+str[pos]-’0’;
- pos++;
- }
- }
- for(int i=1;i<=m;i++){
- add(s,i,w[i]);
- for(int j=1;j<=to[i][0];j++)add(i,to[i][j]+m,INF);
- }
- for(int i=1;i<=n;i++){
- scanf(”%d”,&c[i]);
- add(i+m,t,c[i]);
- }
- while(BFS()){
- int flag;
- while(flag=dfs(s,INF)){
- weight-=flag;
- }
- }
- BFS();
- for(int i=1;i<=m;i++)if(dis[i]>0)printf(“%d ”,i);
- printf(”\n”);
- for(int j=m+1;j<=m+n;j++)if(dis[j]>0)printf(“%d ”,j-m);
- printf(”\n%d\n”,weight);
- return 0;
- }