UVA1025 - A Spy in the Metro

用d[i][j]表示在时刻i,车站j的最少等待时间
有三种决策:
1.等一分钟
2.乘往左开的车
3.乘往右开的车
这三种情况下算处的d[i][j]取最小值

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
using namespace std;

#define INF 1 << 30
int has_train[255][55][2];
int dp[255][55];
int tim[55];
int main()
{
   // freopen("in.txt", "r", stdin);
    int n, t, cas = 0;

    while(cin >> n && n)
    {
        memset(has_train, 0, sizeof(has_train));
        cin >> t;
        for(int i = 1; i <= n-1; i++)
            cin >> tim[i];
        int k;
        cin >> k;
        for(int i = 0; i < k; i++)
        {
            int a, s;
            cin >> a;
            s = a;
            has_train[a][1][0] = 1;
            for(int j = 1; j <= n-1; j++)
            {
                if(tim[j]+s > t)
                    break;
                has_train[tim[j]+s][j+1][0] = 1;
                s += tim[j];
            }
        }
        cin >> k;
        for(int i = 0; i < k; i++)
        {
            int a, s;
            cin >> a;
            s = a;
            has_train[a][n][1] = 1;
            for(int j = n-1; j >= 1; j--)
            {
                if(tim[j] + s > t)
                    break;
                has_train[tim[j]+s][j][1] = 1;
                s += tim[j];
            }
        }
        dp[t][n] = 0;
        for(int i = 1; i < n; i++)
            dp[t][i] = INF;
        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 + tim[j] <= t)
                dp[i][j] = min(dp[i][j], dp[i+tim[j]][j+1]);
            if(j > 1 && has_train[i][j][1] && i + tim[j-1] <= t)
                dp[i][j] = min(dp[i][j], dp[i+tim[j-1]][j-1]);
        }
        printf("Case Number %d: ", ++cas);
        if(dp[0][1] >= INF)
            cout << "impossible" << endl;
        else
            cout << dp[0][1] << endl;
    }
    return 0;
}

你可能感兴趣的:(UVA1025 - A Spy in the Metro)