挖地雷2【DP】

> Description
  在一个地图上有N个地窖(N<=20),每个地窖中埋有一定数量的地雷。同时,给出地窖之间的连接路径。
  例如:
在这里插入图片描述


> Input
在这里插入图片描述


> Output
K1 K2,……,KV (挖地雷的顺序)
MAX (挖地雷的数量)


> Sample Input
在这里插入图片描述
5
10 8 4 7 6
1 1 1 0
0 0 0
1 1
1


> Sample Output
1 3 4 5
27


> 解题思路
这一道题更上一道挖地雷挖地雷1是十分十分一样的,只用把输入那里换一个方式,然后再把输出那里的"-" 换成 " "就行了。所以我就不做解释了。


> 代码

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=201;
int b[maxn][maxn],a[maxn],f[maxn],f1[maxn];
int t,n,ans=0;

void aka(int s)
{
	if(s==0) return;
	aka(f1[s]);
	printf("%d ",s); 
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		f[i]=a[i]; f1[i]=0;
	}
	for(int i=1;i<=n-1;i++)
	 for(int j=i+1;j<=n;j++)
	 {
 		 scanf("%d",&t);
 		 if(t==1) 
		 {
		 	b[i][0]++;
		 	b[i][b[i][0]]=j;
		 }
	 }
	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=b[i][0];j++)
		 if(f[i]+a[b[i][j]]>f[b[i][j]])
		 {
			 f[b[i][j]]=f[i]+a[b[i][j]];
			 f1[b[i][j]]=i;
		 }
		if(f[i]>f[ans]) ans=i;
	}
    aka(f1[ans]);
	printf("%d",ans);
	printf("\n");
	printf("%d",f[ans]);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43010386/article/details/82810311
今日推荐