NOIP1996找地雷

简单DP

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define down(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
#define N 205
int a[N][N],w[N],pre[N];
int f[N];
int n,m,ans=0,anss;

void ini()
{
	scanf("%d",&n);
	fo(i,1,n)scanf("%d",&w[i]);
	fo(i,1,n-1)
	{
		fo(j,i+1,n)
		{
			int x;
			scanf("%d",&x);
			a[i][j]=x;
			a[j][i]=x;
		}
	}
}

void DP()
{
	fo(i,1,n)
	{
		if(w[i]>f[i])
		{
			f[i]=w[i];
			pre[i]=i;
		}
		fo(j,1,i-1)
		if(a[i][j]&&f[j]+w[i]>f[i])
		{
			f[i]=f[j]+w[i];
			pre[i]=j;
		}
		if(f[i]>ans)
		{
			ans=f[i];
			anss=i;
		}
	}
}

void OUT()
{
	int out[N]={0};
	while(pre[anss]!=anss)
	{
		out[++out[0]]=anss;
		anss=pre[anss];
	}
	cout<<anss<<' ';
	down(i,out[0],1)cout<<out[i]<<' ';
	cout<<endl<<ans<<endl;
}

int main()
{
	ini();
	DP();
	OUT();
	return 0;
}


你可能感兴趣的:(dp)