POJ1946 Cow Cycling

dp题,用dp[i][j][k]表示 剩下i头牛 头牛的体力为j 已经跑了k圈 所用的最短时间

尽量把头牛的体力用完后dropout再换牛,否则会多消耗总体力;头牛没有体力之后就可以直接dropout换牛,接上来的牛的体力等于e-k

#include <cstdio>

int Min(int a,int b){return a<b?a:b;}

const int INF = 0x3f3f3f3f;
int n,e,d,ans;
int dp[21][101][101];

int main()
{
	while (scanf("%d%d%d",&n,&e,&d)==3)
	{
		ans=INF;
		for (int i=0;i<=n;i++)
			for (int j=0;j<=e;j++)
				for (int k=0;k<=d;k++)
					dp[i][j][k]=INF;
		dp[n][e][0]=0;
		for (int k=0;k<=d;k++)
			for (int i=n;i>=1;i--)
				for (int j=e;j>=0;j--)
				{
					if (dp[i][j][k]==INF) continue;
					if (k==d) ans=Min(ans,dp[i][j][k]);
					for (int x=1;x+k<=d;x++)
					{
						if (j>=x*x)
							dp[i][j-x*x][k+x]=Min(dp[i][j-x*x][k+x],dp[i][j][k]+1);
						if (i>=2 && e-k-x*x>=0)
							dp[i-1][e-k-x*x][k+x]=Min(dp[i-1][e-k-x*x][k+x],dp[i][j][k]+1);
					}
				}
		if (ans==INF) puts("0");
		else printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(POJ1946 Cow Cycling)