UVA 1025 A Spy in the Metro DP

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

using namespace std;

const int INF = 0x3f3f3f3f;
int n, t, times[100], m1, m2;
int has_train[1000][1000][2];
int dp[1000][1000];

int main()
{
    int kase = 0;
    while (scanf("%d", &n) == 1 && n) {
        memset(times, 0, sizeof(times));
        memset(has_train, 0, sizeof(has_train));
        memset(dp, 0, sizeof(dp));
        scanf("%d", &t);
        for (int i = 1; i < n; i++) scanf("%d", ×[i]);
        scanf("%d", &m1);
        for (int i = 1; i <= m1; i++) {
            int temp; scanf("%d", &temp);
            for (int j = 1; j <= n; j++) {
                has_train[temp][j][0] = 1;
                temp += times[j];
            }
        }
        scanf("%d", &m2);
        for (int i = 1; i <= m2; i++) {
            int temp; scanf("%d", &temp);
            for (int j = n; j  >= 1; j--) {
                has_train[temp][j][1] = 1;
                temp += times[j-1];
            }
        }
        for (int i = 1; i <= n - 1; i++) dp[t][i] = INF;
        dp[t][n] = 0;

        for (int i = t - 1; i >= 0; i--) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = dp[i+1][j] + 1;
                if (j < n && has_train[i][j][0] && i + times[j] <= t) {
                    dp[i][j] = min(dp[i][j], dp[i + times[j]][j + 1]);
                }
                if (j > 1 && has_train[i][j][1] && i + times[j-1] <= t) {
                    dp[i][j] = min(dp[i][j], dp[i + times[j-1]][j-1]);
                }
            }
        }
        cout << "Case Number " << ++kase << ": ";
        if (dp[0][1] >= INF) cout << "impossible\n";
        else cout << dp[0][1] << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值