ZOJ 2297 Survival 状态压缩DP

做法:这种元素个数很少,而且状态和顺序有关,可以往状态压缩上考虑

#include<cstdio>
#include<cstring>
const int LMT=1<<20;
int dp[LMT],c[25],r[25];
int max(int a,int b)
{
	return a>b?a:b;
}
int min(int a,int b)
{
	return a<b?a:b;
}
void init(void)
{
	memset(dp,-1,sizeof(dp));
}
int main(void)
{
	int n,i,j,lim,boss;
	while(~scanf("%d",&n))
	{
		init();
		for(i=1;i<n;i++)
			scanf("%d%d",&c[i],&r[i]);
		scanf("%d",&boss);
		lim=1<<(n-1);
		dp[0]=100;
		for(i=0;i<lim;i++)
			if(dp[i]!=-1)
			{
				for(j=1;j<n;j++)
					if(!(i&(1<<(j-1)))&&dp[i]>=c[j])
						{
						   dp[i|(1<<(j-1))]=max(dp[i|(1<<(j-1))],dp[i]+r[j]-c[j]);
						   dp[i|(1<<(j-1))]=min(dp[i|(1<<(j-1))],100);
						}
			}
			if(dp[lim-1]<boss||dp[lim-1]==-1)printf("try again\n");
			else printf("clear!!!\n");
	}
	return 0;
}


你可能感兴趣的:(ZOJ 2297 Survival 状态压缩DP)