【题意】(小紫书)一个人从站台1出发,乘车要在时刻T到达站台n,为使在站台等车时间最短,她可以选择乘坐两个方向的列车,并在客车停靠站的时候换车。

【分析】每次停站下车时,她都有三种选择,1.原地不动 2.搭乘向右的车 3.搭乘向左的车。d[i][j]表示在站台i,时刻j的最小等待时间。

状态转移方程:

等待:dp[i][j]=dp[i][j+1]+1;

如果有向右的车:  dp[i][j]=min(dp[i][j],dp[i+1][j+t[i]]);

如果有向左的车: dp[i][j]=min(dp[i][j],dp[i-1][j+t[i-1]]);

注意:

1.某时刻某车站是否有车,可以使用一个三维数组has_train来记录。

2.边界条件  dp[N][T]=0

【代码】

#include<cstdio>
#include<cstring>
#define min(a,b) (a)<(b)?(a):(b)
const int maxn=250;
const int INF=0xfffffff;
int has_train[55][250][2];
int t[250];
int d[55];
int e[55];
int dp[50][200];
int main (void)
{
int N,T,total,M1,M2,c=0;
while(scanf("%d",&N)==1&&N)
{
memset(has_train,0,sizeof(has_train));
scanf("%d",&T);
for(int i=1;i<=N-1;i++) scanf("%d",&t[i]);
scanf("%d",&M1);
for(int i=1;i<=M1;i++)
{
scanf("%d",&d[i]);
total=0;
has_train[1][d[i]][0]=1;
for(int j=1;j<=N-1;j++)
{
total+=t[j];
if(d[i]+total<=T)
has_train[j+1][d[i]+total][0]=1;
else break;
}
}
scanf("%d",&M2);
for(int i=1;i<=M2;i++)
{
scanf("%d",&e[i]);
total=0;
has_train[N][e[i]][1]=1;
for(int j=N-1;j>=1;j--)
{
total+=t[j];
if(e[i]+total<=T)
has_train[j][e[i]+total][1]=1;
else break;
}
}
for(int i=1;i<=N;i++)
for(int j=0;j<=T;j++)
dp[i][j]=INF;
dp[N][T]=0;
for(int j=T-1;j>=0;j--)
{
for(int i=1;i<=N;i++)//i车站j时刻
{
dp[i][j]=dp[i][j+1]+1;
if(j+t[i]<=T&&has_train[i][j][0]&&i<N)
dp[i][j]=min(dp[i][j],dp[i+1][j+t[i]]);
if(j+t[i-1]<=T&&has_train[i][j][1]&&i>1)
dp[i][j]=min(dp[i][j],dp[i-1][j+t[i-1]]);
}
}
if(dp[1][0]>=INF)
printf("Case Number %d: impossible\n",++c);
else
printf("Case Number %d: %d\n",++c,dp[1][0]);
}
return 0; }



UVA 1025_A Spy in the Metro的更多相关文章

  1. uva A Spy in the Metro(洛谷 P2583 地铁间谍)

    A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especially dangero ...

  2. UVA A Spy in the Metro

    点击打开题目 题目大意: 在一个有n个站台的地铁线路里,给你列车通向每相邻两个车站所花费的时间,从0时刻开始,从1号站出发,要在T这个时间点上,到达n号站,给你m1辆从1开到n的列车及其出发时间,和m ...

  3. 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 especia ...

  4. uva 1025 A Spy in the Metro 解题报告

    A Spy in the Metro Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug Secr ...

  5. UVA 1025 -- A Spy in the Metro (DP)

     UVA 1025 -- A Spy in the Metro  题意:  一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...

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

    洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...

  7. UVA1025-A Spy in the Metro(动态规划)

    Problem UVA1025-A Spy in the Metro Accept: 713  Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...

  8. UVa 1025 A Spy in the Metro(动态规划)

    传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous ...

  9. UVA 1025 A Spy in the Metro 【DAG上DP/逆推/三维标记数组+二维状态数组】

    Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After s ...

随机推荐

  1. e.Row.RowType == DataControlRowType.DataRow详解(转)

    代码语句如下: protected void OnRowCreate(object sender, GridViewRowEventArgs e)    {        if (e.Row.RowT ...

  2. java debug源码完整版

    第一步:现在myeclipse或者eclipse中下载jad插件,将class文件翻译成java文件 点击下载安装 第二步:创建一个java工程,导出成jar包.jdk自带的jar包不包含debug ...

  3. provider模式

    最近看代码有所感想吧.当底层API,发生变化时,可以使用Provider模式.既然是模式就一定的股则. 1.该模式对原有接口的封装. 2.该模式实现对API的封装,不显示细节,从而取消依赖关系. 3. ...

  4. 字符串(String)几个常用方法的详解

    String:(字符串) indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. stringObject.indexOf(searchvalue,fromindex) searc ...

  5. Swift 中的值类型与引用类型

    顶级修饰 次级修饰 赋值类型 存储类型 值类型 值类型   深拷贝 栈 值类型 引用类型 浅拷贝 堆 引用类型 值类型 浅拷贝 堆 引用类型 引用类型 浅拷贝 堆 复合引用类型会改变内部值类型的存储行 ...

  6. 前复权是从今天的价格倒推 后复权是从上市价格前推 不复权就是原始K线。

    前复权是从今天的价格倒推 后复权是从上市价格前推 不复权就是原始K线.

  7. 一起来学SpringBoot(十七)优雅的参数校验

    参数校验在开发中经常需要写一些字段校验的代码,比如字段非空,字段长度限制,邮箱格式验证等等,写这些与业务逻辑关系不大的代码个人感觉有两个麻烦: 验证代码繁琐,重复劳动方法内代码显得冗长每次要看哪些参数 ...

  8. viewer && ImageFlow 图片滚动组件 图片点击放大 可以滚轮放大缩小 viewer

    ImageFlow https://finnrudolph.com/products/imageflow https://github.com/countzero/ImageFlow http://w ...

  9. python3+beautifulSoup4.6抓取某网站小说(二)基础功能设计

    本章学习内容:1.网页编码还原读取2.功能设计 stuep1:网页编码还原读取 本次抓取对象: http://www.cuiweijuxs.com/jingpinxiaoshuo/ 按照第一篇的代码来 ...

  10. react-native 手势操作和 react-naviagation 组件的手势返回功能的冲突解决

    上篇我们说到过在react-native触摸及手势事件 那么我在项目中遇到的问题是在react-navigation中的子页面,希望保留在ios中的效果:从左侧往右侧滑动为退出该页面. 但是希望我在滑 ...