动态规划——地铁里的间谍

城市间谍(A Spy in the Metro UVa 1025)最详细题解 题的核心是逆推的思想这道题一看要问最少需要等待多少时间,自然会想到用dp,那么怎么来处理这个问题呢?我们可以用dp[i][j]来表示时刻i,你现在身处第j个站,最少还需要等待多长时间,我们所知的是,在T时刻,你人一定需要在第n个车站去完成间谍任务,所以dp[T][n]=0;可以由这个位置来进行反推,比如我在时刻T-1可以仍然位于车站n或者说如果有往左开的车我就有选择往左坐车的可能; 那么我们对于一 阅读详情

Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first 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 finds 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 first station to the last station and from the last station back to the first station. The time required for a train to travel between two consecutive stations is fixed since all trains move at the same speed. Trains make a very short stop at each station, which you can ignore for 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.

 

Input

The input file contains several test cases. Each test case consists of seven lines with information as follows. Line 1. The integer N (2 ≤ N ≤ 50), which is the number of stations. Line 2. The integer T (0 ≤ T ≤ 200), which is the time of the appointment. Line 3. N − 1 integers: t1, t2, . . . , tN−1 (1 ≤ ti ≤ 20), representing the travel times for the trains between two consecutive stations: t1 represents the travel time between the first two stations, t2 the time between the second and the third station, and so on. Line 4. The integer M1 (1 ≤ M1 ≤ 50), representing the number of trains departing from the first station. Line 5. M1 integers: d1, d2, . . . , dM1 (0 ≤ di ≤ 250 and di < di+1), representing the times at which trains depart from the first station. Line 6. The integer M2 (1 ≤ M2 ≤ 50), representing the number of trains departing from the N-th station. Line 7. M2 integers: e1, e2, . . . , eM2 (0 ≤ ei ≤ 250 and ei < ei+1) representing the times at which trains depart from the N-th station. The last case is followed by a line containing a single zero.

Output

For each test case, print a line containing the case number (starting with 1) and an integer representing the total waiting time in the stations for a best schedule, or the word ‘impossible’ in case Maria is unable to make the appointment. Use the format of the sample output.

Sample Input

4 
55
5 10 15
4
0 5 10 20
4
0 5 10 15
4
18
1 2 3
5
0 3 6 10 12
6
0 3 5 7 12 15
2
30
20
1
20
7
1 3 5 7 11 13 17
0

Sample Output

Case Number 1: 5

Case Number 2: 0

Case Number 3: impossible



题意:

共有n个车站,现在你要在t时刻到n站和一个间谍见面,告诉你每两个相邻车站之间的行驶时间(无论是从左往右还是从右往左,时间不变),再告诉有m1辆车会从第1站出发向第n站行驶,并给出这m1辆车的出发时间。然后再告诉有m2辆车会从第n站出发向第1站行驶,并给出这m2辆车的出发时间,现在要求出你所要等待的最小时间。在一辆行驶的车上所花的时间不算在等待时间里面,忽略上下车和列车停靠的时间。

思路:

由于影响决策的只有当前的时刻和当前所处的站,于是用dp[i][j]表示在i时刻时我在j站所需等待的最小时间。

那么有三种决策

1.在原地等1分钟

2.如果此时有向右开的车,就上车

3.如果此时有向左开的车,就上车

事先将dp数组置为INF,之后判断dp[0][1]的值的情况看是否有解即可


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn=55;
const int maxt=220;
int n ,t;
bool train[maxt][maxn][2];
int timetable[maxn];
int dp[maxt][maxn];
void solve()
{
    for(int i=1; i<n; 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;   //决策1
            if((i+timetable[j]<=t)&&j<n&&train[i][j][0])   //决策2
                dp[i][j]=min(dp[i][j], dp[i+timetable[j]][j+1]);
            if((i+timetable[j-1])<=t&&j>1&&train[i][j][1])  //决策3
                dp[i][j]=min(dp[i][j], dp[i+timetable[j-1]][j-1]);
        }
    }
}
int main()
{
    int cas=0;
    while(~scanf("%d", &n) &&n)
    {
        scanf("%d", &t);
        memset(timetable, 0, sizeof(timetable));
        memset(train, 0, sizeof(train));
        for(int i=1; i<n; i++)
            scanf("%d", &timetable[i]);   //存放相邻两个站之间的行驶时间
        int m1, m2;
        scanf("%d", &m1);
        for(int i=0; i<m1; i++)   //计算每个站有车的时刻
        {
            int x;
            scanf("%d", &x);
            train[x][1][0]=true;
            for(int j=1; j<n; j++)
                if(x+timetable[j]<=t)    //不加会RE
                    train[x+=timetable[j]][j+1][0]=true;
        }
        scanf("%d", &m2);
        for(int i=0; i<m2; i++)   //同上
        {
            int x;
            scanf("%d", &x);
            train[x][n][1]=true;
            for(int j=n-1; j>=1; j--)
                if(x+timetable[j]<=t)
                    train[x+=timetable[j]][j][1]=true;
        }
        solve();
        if(dp[0][1]>=INF)
            printf("Case Number %d: impossible\n", ++cas);
        else
            printf("Case Number %d: %d\n", ++cas, dp[0][1]);
    }
    return 0;
}


写出GD32L233/235在读写flash的区别(用于OTA功能) 在单片机面表示 大概是 address += sizeof(uint32_t) address += sizeof(uint64_t)1.说到OTA功能,实际上 往flash 指定区域写 数据, 拷贝数据,然后 跳转, 必要时 注意单片机的偏移量。在写本文时,L233支持单字写入,而L235只有双字写入. 一个写入地址+4 一个写入地址+8。这个是 自己写的擦除一页 数据,当然可以参考 提供的demo去实现,看自己需求。接下来是 读取 双字,也就是在L235上的读取。 阅读详情

相关推荐

《Autosar_BSW高阶配置》总目录_培训教程持续更新中...

目录 一、Autosar COM Module 二、Autosar BswM Module 三、Autosar EcuM Module 四、Autosar DCM Module 五、Autosar DEM Module 六、其它Module陆续推出 欢迎大家订阅《Autosar_BSW高阶配置》专栏(可以理解为是Autosar培训教程),该专栏每周至少更新一篇,一次订阅,不再二次收费,献上常用的案例和配置方法。下方整理了相关博文的链接(单击蓝色字体即可跳转),方便大家获取。 本专栏...

「汽车电子助手」的博客 4万+

UVA 1025 A Spy in the Metro (城市间谍(算法竞赛入门经典——例题9-1))(翻译,详解)

 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. Afterseveral thrilling events we find her in the first station of Algorithms City Metro...

RuanranW 653

PSI5通讯模块E21.41芯片手册

PSI5通讯模块E21.41芯片手册

UVA - 1025 A Spy in the Metro[DP DAG]

UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first...

weixin_34355559的博客 177

[SinGuLaRiTy] 动态规划题目复习

【SinGuLaRiTy-1026】 Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [UVA 1025] A Spy in the Metro 题目描述 特工玛利亚被送到S市执行一个特别危险的任务。她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂。 玛利亚有一个任务,现在的时间为0,她要从第一个站出发,并在最后...

weixin_33762130的博客 4687

动态规划】[UVA1025]A Spy in the Metro 城市间谍

DescriptionSecret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examini

XiaoZheng的博客 2266

算法竞赛入门经典(第二版)-刘汝佳-第九章 动态规划初步 例题(11/31)

说明本文是我对第9章31道例题的练习总结,建议配合紫书——《算法竞赛入门经典(第2版)》阅读本文。 另外为了方便做题,我在VOJ上开了一个contest,欢迎一起在上面做:第九章例题contest 如果想直接看某道题,请点开目录后点开相应的题目!!!例题例9-1 UVA 1025 地铁间谍题意思路代码例9-2 UVA 437 巴比伦塔题意思路代码例9-3 UVA 1347 旅游题意思路代码例

梁山伯的专栏 3891

动态规划——城市间谍

*描述及讲解摘自其它博客,代码自打 Description Sample Input 4  55  5 10 15  4  0 5 10 20  4  0 5 10 15  4  18  1 2 3  5  0 3 6 10 12  6  0 3 5 7 12 15  2  30  20  1  20  7  1 3 5 7 11 13 17  0 Sample Output Case ...

Renaming的博客 791

UVA1025 城市间谍 A Spy in the Metro

某城市地铁是一条直线,有n2≤n≤50)个车站,从左到右编号1n。有M1​辆列车从第1站开始往右开,还有M2​辆列车从第n站开始往左开。列车在相邻站台间所需的运行时间是固定的,因为所有列车的运行速度是相同的。在时刻0,Mario 从第1站出发,目的在时刻T0≤T≤200)会见车站n的一个间谍。在车站等车时容易被抓,所以她决定尽量躲在开动的火车上,让在车站等待的时间尽量短。

Gowi的博客 1441

地铁间谍 【UVA - 1025】【DP】

题目链接 题目大意 某城市的地铁是线性的,有n(2≤n≤50)个车站,从左到右编号为1~n。有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开。在时刻0,Mario从第1站出发,目的是在时刻T(0≤T≤200)会见车站n的一个间谍。在车站等车时容易被抓,所以她决定尽量躲在开动的火车上,让在车站等待的总时间尽量短。列车靠站停车时间忽略不计,且Mario身手敏捷,即使两辆方向不同的列车在...

qq_41925919的博客 215

洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)

洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址: http://www.luogu.org/problem/show?pid=2583 题目描述   特工玛利亚被送到S市执行一个特别危险的任务。她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂。   玛利亚有一个任务,现在的时间为0,她要从第一个站出发,并在最后一站的间谍碰头。玛利

hahalidaxin的专栏 1万+

动态规划初步--城市间谍

一、题目 某城市的地铁是线性的,有n(2 ≤ n≤ 50)个车站,从左到右编号为1~n。有M1辆车从第一站开始往右开,还有M2辆从第n站开始往左开。在时刻0,Mario从第一站出发,目的是在T时刻会见在n站的一个间谍。要求其在车站的等待时间足够短。 二、解题思路 状态由当前时间和当前所在站决定,我们可以用dp[i][j]表示在时刻t,第i站最少还需要等待的时间。易只T时刻的情况容...

dianshu1593的博客 168

UVA 1025 城市间谍 A Spy in the Metro

UVA 1025

阿巴阿巴 201

洛谷P2583 地铁间谍

P2583 地铁间谍 题目描述 特工玛利亚被送到S市执行一个特别危险的任务。她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂。 玛利亚有一个任务,现在的时间为0,她要从第一个站出发,并在最后一站的间谍碰头。玛利亚知道有一个强大的组织正在追踪她,她知道如果一直呆在一个车站,她会有很大的被抓的风险,躲在运行的列车中是比较安全的。所以,她决定尽可...

weixin_30625691的博客 104

uva1025城市间谍

   某城市地铁是线性的,有n(2≤n≤50)个车站,从左到右编号1~n。有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开。    列车在相邻站台间所需的运行时间是固定的,因为所有列车的运行速度是相同的。    在时刻0,Mario从第1站出发,目的在时刻T(0≤T≤200)会见车站n的一个间谍。在车站等车时容易被抓,所以她决定尽量躲在开动的火车上,让在车站等待的时间尽量短...

weixin_30656145的博客 102

城市间谍(A Spy in the Metro UVa 1025)

没错,这有部分是转发的,因为其中一些注解让我理解了这题的过程。非常感激 http://blog.csdn.net/NOIAu/article/details/71517440 题意: 某城市的地铁是线性的,有n(2≤n≤50)个车站,从左到右编号为1~n。有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开。在时刻0,Mario从第1站出发,目的是在时刻T(0≤T≤200)会见

Loser 519

地铁间谍(洛谷 2583)

题目描述 特工玛利亚被送到S市执行一个特别危险的任务。她需要利用地铁完成他的任务,S市的地铁只有一条线路运行,所以并不复杂。 玛利亚有一个任务,现在的时间为0,她要从第一个站出发,并在最后一站的间谍碰头。玛利亚知道有一个强大的组织正在追踪她,她知道如果一直呆在一个车站,她会有很大的被抓的风险,躲在运行的列车中是比较安全的。所以,她决定尽可能地呆在运行的列车中,她只能往前或往后坐车。 玛利亚为...

weixin_30788619的博客 117

第九章-DAG上的动态规划

摘要: 本文讨论了两个动态规划问题。 第一个是DAG模型中的矩形嵌套问题。第二个是地铁间谍问题。

lvquan2006的博客 1356

26.城市间谍 (UVa1025)

某城市的地铁是线性的,有n(2≤n≤50)n(2≤n≤50)n(2≤n≤50)个车站,从左到右编号为1~n1~n1~n。有M1M1M1辆列车从第1站开始往右开,还有M2M_2M2​辆列车从第nnn站开始往左开。在时刻000,Mario从第1站出发,目的是在时刻T(0≤T≤200)T(0≤T≤200)T(0≤T≤200)会见车站nnn的一个间谍。在车站等车时容易被抓,所以她决定尽量躲在开动的火车上,...

W_LAILAI的博客 256

A Spy in the Metro UVA - 1025 城市间谍

 题目链接 题意: 某城市的地铁是线性的,有n(2≤n≤50)个车站,从左到右编号为1~n。有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开。在时刻0,Mario从第1站出发,目的是在时刻T(0≤T≤200)会见车站n的一个间谍。在车站等车时容易被抓,所以她决定尽量躲在开动的火车上,让在车站等待的总时间尽量短。列车靠站停车时间忽略不计,且Mario身手敏捷,即使两辆方向不同的列...

Nicolas的博客 398

『UVA 1025』城市间谍

题意:某城市地铁是线性的,有n个车站,从左到右编号1-n。有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开。在时刻0,Mario从第1站出发,目的在时刻T会见车站n的一个间谍。在车站等车时容易被抓,所以她决定尽量躲在开动的火车上,让在车站等待的时间尽量短。列车靠站停车时间忽略不计,且Mario身手敏捷,即时两辆方向不同的列车在同一时间靠站,Mario也能完成换乘个人感想:做了这么久d

Gavinjou大笨象的博客 948
上一篇: 博弈——Treblecross
下一篇: 动态规划——巴比伦塔
LSC_333
博客等级 码龄9年 14粉丝 134原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值