ural 1031. Railway Tickets | poj 2355

类型:DP

题目:http://acm.timus.ru/problem.aspx?space=1&num=1031

思路:用f(i)表示到第i站时的最小费用,考虑第i站有两种选择,单独买一张票,或者和前面的几个站共同买一张票

!!!可能总共用一张票就够了

// ural 1031. Railway Tickets
// wa wa wa wa ac 0.406s
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define FOR(i,a,b) for(i = (a); i < (b); ++i)
#define FORE(i,a,b) for(i = (a); i <= (b); ++i)
#define FORDE(i,a,b) for(i = (a); i >= (b); --i)

int n, m;
int l1, l2, l3, c1, c2, c3;
int st, ed;
long long dp[10010], l[10010], val[10010];

void input() {
    int i;

    scanf("%d %d %d %d %d %d", &l1, &l2, &l3, &c1, &c2, &c3);
    scanf("%d", &n);
    scanf("%d %d", &st, &ed);
    if(st > ed)
        swap(st, ed);
    FORE(i, 1, n - 1)
        scanf("%lld", &l[i]);
    FORDE(i, n - 1, 1)
        l[i] = l[i] - l[i - 1];
}

long long get_val(long long x) {
    if(x <= l1)
        return c1;
    if(x <= l2)
        return c2;
    return c3;
}

void solve() {
    int i, j;

    FORE(i, 1, n - 1) {
        if(l[i] <= l1)
            val[i] = c1;
        else if(l[i] <= l2)
            val[i] = c2;
        else
            val[i] = c3;
    }
    FORE(i, st, ed - 1) {
        dp[i] = dp[i - 1] + val[i];
        j = i - 1;
        long long sum_val = l[i];
        // !!!
        while(j >= st - 1 && sum_val <= l3) {
            dp[i] = min(dp[i], dp[j] + get_val(sum_val));
            sum_val += l[j];
            --j;
        }
    }
    printf("%lld\n", dp[ed - 1]);
}

int main() {
    input();
    solve();
    return 0;
}


阅读更多

没有更多推荐了,返回首页