乌龟的用时主要与到达充电站时是否充电有关。用dp[i]表示到达第i个充电站的最少用时,同时可以将起点和终点都看作充电站,用dp[0],dp[n + 1]表示,于是得状态转移公式dp[i] = min(dp[i], dp[j] + tk),j < i,tk表示从k站充满电后到达i站的用时,特别的dp[0] = 0。dp[n + 1]即为乌龟的最优策略
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int L, N, C, T, VR, VT1, VT2, p[110];
double dp[110];
while (cin >> L)
{
cin >> N >> C >> T;
cin >> VR >> VT1 >> VT2;
p[0] = 0;
p[N + 1] = L;
for (int i = 1;i <= N;i++)
{
cin >> p[i];
}
dp[0] = 0.0;
for (int i = 1;i <= N + 1;i++)
{
dp[i] = 1000000000;
for (int j = 0;j < i;j++)
{
double len = 1.0*(p[i] - p[j]);
double tmp = (len >= C) ? ((C*1.0) / VT1 + (len - C)*1.0 / VT2) : ((len*1.0) / VT1);
if (j)
tmp += T;
dp[i] = min(dp[i], dp[j] + tmp);
}
}
double RT = (L*1.0) / VR;
if (dp[N+1] < RT)
cout << "What a pity rabbit!" << endl;
else
cout<<"Good job,rabbit!"<<endl;
}
return 0;
}