UVA 1025 A Spy in the Metro DP水题

简单DAG,每个状态有三种决策,原地傻等,上往左开的车,上往右开的车。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <cstdlib>
#include <climits>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <stack>
#include <sstream>
#include <numeric>
#include <fstream>
#include <functional>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int,int> pii;
const int INF = INT_MAX / 3;
const double eps = 1e-8;
const LL LINF = 1e17;
const double DINF = 1e60;
const int maxn = 55;
const int maxt = 400;
int f[maxt][maxn], N, T;
int cost[maxn], M1, M2, d1[maxn], d2[maxn];
bool left_bus[maxn][maxt], right_bus[maxn][maxt];

int main() {
    int kase = 1;
    while(scanf("%d",&N), N) {
        memset(f,0x3f,sizeof(f));
        memset(cost,0,sizeof(cost));
        memset(left_bus,0,sizeof(left_bus));
        memset(right_bus,0,sizeof(right_bus));
        int inf = f[0][0];
        scanf("%d",&T);
        for(int i = 1;i < N;i++) scanf("%d",&cost[i]);
        scanf("%d",&M1);
        for(int i = 1;i <= M1;i++) scanf("%d",&d1[i]);
        scanf("%d",&M2);
        for(int i = 1;i <= M2;i++) scanf("%d",&d2[i]);
        for(int i = 1;i <= M1;i++) {
            int nowtime = d1[i];
            for(int j = 1;j <= N;j++) {
                left_bus[j][nowtime] = true;
                nowtime += cost[j];
            }
        }
        for(int i = 1;i <= M2;i++) {
            int nowtime = d2[i];
            for(int j = N;j >= 1;j--) {
                right_bus[j][nowtime] = true;
                nowtime += cost[j - 1];
            }
        }
        f[0][1] = 0;
        for(int i = 0;i <= T;i++) {
            for(int j = 1;j <= N;j++) {
                //wait
                f[i + 1][j] = min(f[i + 1][j],f[i][j] + 1);
                //go left_bus
                if(right_bus[j][i] && j > 1 && i + cost[j - 1] <= T) {
                    f[i + cost[j - 1]][j - 1] = min(f[i + cost[j - 1]][j - 1],
                            f[i][j]);
                }
                //go right_bus
                if(left_bus[j][i] && j < N && i + cost[j] <= T) {
                    f[i + cost[j]][j + 1] = min(f[i + cost[j]][j + 1],
                            f[i][j]);
                }
            }
        }
        printf("Case Number %d: ",kase++);
        if(f[T][N] == inf) puts("impossible");
        else printf("%d\n",f[T][N]);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/rolight/p/3966017.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值