标签:32位 hdu2059 速度 gb2312 加油站 ane target 长度 起点到终点
尽管乌龟深知获胜希望不大,只是迫于舆论压力。仅仅能接受挑战。
比赛是设在一条笔直的道路上。长度为L米,规则非常easy。谁先到达终点谁就算获胜。
无奈乌龟自从上次获胜以后,成了名龟。被一些八卦杂志称为“动物界的刘翔”。广告不断,手头也有了不少积蓄。为了可以再赢兔子,乌龟不惜花下血本买了最先进的武器——“"小飞鸽"牌电动车。这辆车在有电的情况下可以以VT1 m/s的速度“飞驰”。可惜电池容量有限,每次充满电最多仅仅能行驶C米的距离。以后就仅仅能用脚来蹬了。乌龟用脚蹬时的速度为VT2 m/s。更过分的是,乌龟居然在跑道上修建了非常多非常多(N个)的供电站,供自己给电动车充电。当中,每次充电须要花费T秒钟的时间。当然,乌龟经过一个充电站的时候可以选择去或不去充电。
比赛立即開始了,兔子和带着充满电的电动车的乌龟并列站在起跑线上。你的任务就是写个程序。推断乌龟用最佳的方案进军时。能不能赢了一直以恒定速度奔跑的兔子。
否则输出一行"Good job,rabbit!";
题目数据保证不会出现乌龟和兔子同一时候到达的情况。
100 3 20 5 5 8 2 10 40 60 100 3 60 5 5 8 2 10 40 60
Good job,rabbit! What a pity rabbit!
然后换了下思路。让dp[i]表示以第i个加油站作为终于加油站时乌龟到达终点的用时,在内部加入一个gotime作为出发时间点。toend表示起点到终点的用时,这样一来,到达第i个充电站的最短用时就行求出来了。其到终点的用时也是固定的,终于得出答案。
另外一种思路优于第一种思路的地方是到达第i个充电站的用时的得出考虑到前面全部的充电站,而不不过跟其相邻的前一个充电站。这样得出的解才是真正的最优解。
#include <stdio.h> #include <string.h> #define maxn 105 #define inf 0x3f3f3f3f struct Node { double goTime, toEnd; } dp[maxn]; // dp[i]表示以第i个加油站作为终于加油站的用时 int arr[maxn], L, n, c, t, vr, vt1, vt2; double min(double a, double b) { return a < b ?a : b; } double calTime(int dist) { if(dist <= c) return (double)dist / vt1; return (double)(dist-c) / vt2 + (double)c / vt1; } int main() { // freopen("stdin.txt", "r", stdin); double ans; while(~scanf("%d", &L)) { scanf("%d%d%d", &n, &c, &t); scanf("%d%d%d", &vr, &vt1, &vt2); for(int i = 1; i <= n; ++i) scanf("%d", &arr[i]); arr[0] = 0; arr[n+1] = L; dp[0].goTime = 0.0; ans = dp[0].toEnd = dp[0].goTime + calTime(arr[n+1] - arr[0]); for(int i = 1; i <= n; ++i) { dp[i].goTime = inf; for(int j = 0; j < i; ++j) { dp[i].goTime = min(dp[i].goTime, dp[j].goTime + calTime(arr[i] - arr[j])); } dp[i].toEnd = (dp[i].goTime += t) + calTime(arr[n+1] - arr[i]); ans = min(ans, dp[i].toEnd); } if(ans < (double)L / vr) printf("What a pity rabbit!\n"); else printf("Good job,rabbit!\n"); } return 0; }
2015.1.27更新
#include <stdio.h> #include <string.h> #define maxn 102 double dp[maxn]; // arrive time double L, C, T, VR, VT1, VT2; double pos[maxn]; int N; double min(double a, double b) { return a < b ?a : b; } int main() { // freopen("stdin.txt", "r", stdin); int i, j; double tmp; while (scanf("%lf", &L) == 1) { scanf("%d%lf%lf", &N, &C, &T); scanf("%lf%lf%lf", &VR, &VT1, &VT2); for (i = 0; i < N; ++i) scanf("%lf", &pos[i]); pos[N] = L; for (i = 0; i <= N; ++i) { // use none pos if (pos[i] <= C) dp[i] = pos[i] / VT1; else dp[i] = C / VT1 + (pos[i] - C) / VT2; } for (i = 0; i < N; ++i) { // let it be the last pos for (j = i + 1; j <= N; ++j) { if (pos[j] - pos[i] <= C) tmp = (pos[j] - pos[i]) / VT1; else tmp = C / VT1 + (pos[j] - pos[i] - C) / VT2; dp[j] = min(dp[j], dp[i] + T + tmp); } } if (dp[N] < L / VR) printf("What a pity rabbit!\n"); else puts("Good job,rabbit!"); } return 0; }
标签:32位 hdu2059 速度 gb2312 加油站 ane target 长度 起点到终点
原文地址:http://www.cnblogs.com/wzzkaifa/p/6789650.html