UVa1025 A Spy in the Metro

本文介绍了一道动态规划题目,通过顺推和逆推两种方法求解地铁出行的最佳方案,以减少等待时间。文章提供了详细的代码实现及时间复杂度分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一道DP题….(以前做dp都是看题解,,,)
第一次独立做很不熟练,很多细节错误。


题目描述
Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After
several thrilling events we nd her in the rst station of Algorithms City Metro, examining the time
table. The Algorithms City Metro consists of a single line with trains running both ways, so its time
table is not complicated.
Maria has an appointment with a local spy at the last station of Algorithms City Metro. Maria
knows that a powerful organization is after her. She also knows that while waiting at a station, she is at great risk of being caught. To hide in a running train is much safer, so she decides to stay in running trains as much as possible, even if this means traveling backward and forward. Maria needs to know a schedule with minimal waiting time at the stations that gets her to the last station in time for her appointment. You must write a program that nds the total waiting time in a best schedule for Maria. The Algorithms City Metro system has N stations, consecutively numbered from 1 to N. Trains move in both directions: from the rst station to the last station and from the last station back to the rst station. The time required for a train to travel between two consecutive stations is xed since all trains move at the same speed. Trains make a very short stop at each station, which you can ignorefor simplicity. Since she is a very fast agent, Maria can always change trains at a station even if the trains involved stop in that station at the same time.


我先想到的是顺推,做完后看了网上的题解大多数是逆推,我怎么没想到。
很容易发现有如下决策。
1. 等一分钟
2. 坐向左的车(如果有)
3. 坐向右的车(如果有)


顺推做法
设f[i][j]为在i时刻到达j位置需要的最少等待时间,用f[i][j]去更新三种决策能到达的其他状态.
边界是f[0][1]=0
代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<climits>
#define INF INT_MAX/2 
using namespace std;
int N,T,M1,M2,d[300],e[300],t[300],sta[300],f[2000][300];
void initf(){
    for(int i=0;i<=T;i++)
        for(int j=0;j<=N;j++)
            f[i][j]=INF; 
}
int main(){
    int kase=0;
    while(scanf("%d",&N)==1&&N){
        int tot=0;
        scanf("%d",&T);
        memset(sta,0,sizeof(sta));
        sta[0]=1;   //用于判断某火车在某时刻是否在站台 
        for(int i=1;i<=N-1;i++){
            scanf("%d",&t[i]);
            sta[t[i]+tot]=i+1;
            tot+=t[i];
        }
        scanf("%d",&M1);
        for(int i=1;i<=M1;i++) scanf("%d",&d[i]); 
        scanf("%d",&M2);
        for(int i=1;i<=M2;i++) scanf("%d",&e[i]);
        initf();
        f[0][1]=0;
        for(int i=0;i<=T;i++)
            for(int k=1;k<=N;k++) if(f[i][k]!=-1){       //在车站才能更新 
                    for(int j=1;j<=M1;j++)                                                                                               
                        if(i>=d[j]&&sta[i-d[j]]==k) f[i+t[k]][k+1]=min(f[i+t[k]][k+1],f[i][k]);
                    for(int j=1;j<=M2;j++)
                        if(i-e[j]>=0&&tot>=i-e[j]&&sta[tot-(i-e[j])]==k) f[i+t[k-1]][k-1]=min(f[i+t[k-1]][k-1],f[i][k]);
                    f[i+1][k]=min(f[i+1][k],f[i][k]+1);
                }
        printf("Case Number %d: ",++kase);
        if(f[T][N]!=INF) printf("%d\n",f[T][N]);
        else puts("impossible");
    }
    return 0;
}

状态有NT个,决策有(M1+M2+1)个
这样做时间复杂度是O(NT(M1+M2))
然后发现某时刻某位置至多一辆向左或向右的车,决策其实只有3种,,,,然后我不想改了,反正AC了。


逆推做法
设f[i][j]为当i时刻在j位置到达T时刻N位置还要最少等待多少时间,用三种状态更新它;
边界是f[T][N]=0
代码

//参考的是lrj的书里的
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<climits>
#define INF INT_MAX/2 
using namespace std;
int N,T,M1,M2,d[300],e[300],t[300],f[2000][300];
bool has_train[2000][300][2];
void initf(){
    for(int i=1;i<=N-1;i++) f[T][i]=INF;
}
int main(){
    int kase=0;
    while(scanf("%d",&N)==1&&N){
        int tot=0;
        scanf("%d",&T);
        memset(has_train,0,sizeof(has_train));
        for(int i=1;i<=N-1;i++){
            scanf("%d",&t[i]);
            tot+=t[i];
        }
        scanf("%d",&M1);
        for(int i=1;i<=M1;i++){
            scanf("%d",&d[i]);
            int tm=d[i],k=0;
            for(int j=1;j<=N;j++,tm+=t[j-1]) has_train[tm][++k][1]=1;
        }
        scanf("%d",&M2);
        for(int i=1;i<=M2;i++) {
            scanf("%d",&e[i]);
            int tm=e[i],k=N;
            for(int j=N;j>=1;j--,tm+=t[j]) has_train[tm][k--][0]=1;
        }
        initf();
        f[T][N]=0;
        for(int i=T-1;i>=0;i--)
            for(int j=1;j<=N;j++){
                f[i][j]=f[i+1][j]+1;
                if(j>1&&has_train[i][j][0]&&i+t[j-1]<=T) f[i][j]=min(f[i][j],f[i+t[j-1]][j-1]);
                if(j<N&&has_train[i][j][1]&&i+t[j]<=T) f[i][j]=min(f[i][j],f[i+t[j]][j+1]);
            }
        printf("Case Number %d: ",++kase);
        if(!(f[0][1]>=INF)) printf("%d\n",f[0][1]);
        else puts("impossible");
    }
    return 0;
}

时间复杂度O(NT)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值