• Ural_1031.Railway Ticket (DP)


      /*思路:定义dp[i]表示到达第i站的最小花费。所以 dp[i] = min(dp[i-1] + Cx, dp[i-1] + Cx, dp[i-3] + Cx...(直到i 和 (i-x) 两站的距离大于L3);*/

    /*My Code:*/

    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    const int N = 10007;
    const int inf = 0x7fffffff;

    int dis[N];
    int dp[N];

    int main() {
    //freopen("data.in", "r", stdin);

    int n, S, E, min;
    int l1, l2, l3;
    int c1, c2, c3;
    int i, j, tmp;

    memset(dp, 0, sizeof(dp));
    memset(dis, 0, sizeof(dis));

    scanf("%d%d%d%d%d%d", &l1, &l2, &l3, &c1, &c2, &c3);
    scanf("%d", &n);
    scanf("%d%d", &S, &E);

    if(S > E) {tmp = S; S = E; E = tmp;}
    for(i = 2; i <= n; i++)
    scanf("%d", &dis[i]);

    for(i = S+1; i <= E; i++) {
    for(min = inf, j = i-1; j >= S; j--) {
    tmp = dis[i] - dis[j];
    if(tmp <= l1) tmp = dp[j] + c1;
    else if(tmp <= l2 && tmp > l1) tmp = dp[j] + c2;
    else if(tmp > l2 && tmp <= l3) tmp = dp[j] + c3;
    else break;
    if(min > tmp) min = tmp;
    }
    dp[i] = min;
    }
    printf("%d\n", dp[E]);
    return 0;
    }



  • 相关阅读:
    第四周作业
    第四周上机作业
    java第十周上机练习
    java第九周上机练习
    第八周作业
    java第八周上机练习
    java第七周作业
    java第七周上机练习
    java第六周作业
    java上机练习 4.9
  • 原文地址:https://www.cnblogs.com/vongang/p/2234830.html
Copyright © 2020-2023  润新知