【题意】(小紫书)一个人从站台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. Android基础TOP3:线性布局的特点,常用属性,及权重值

    线性布局是一种让视图水平或者垂直布排列的布局: 常用属性: androuid:orientation :表示布局方向 取值vertical表示垂直布局 取值horizontal表示水平布局 andro ...

  2. 4 Visual Effects 视觉效果 读书笔记 第四章

    4   Visual Effects    视觉效果        读书笔记 第四章 Well, circles and ovals are good, but how about drawing r ...

  3. mongodb用户权限管理(二)

    数据库 分配用户权限 有了创建语法,和参数说明,接下来开始实践. 注意,还有一点,账号是跟着数据库绑定的,在那个库里授权,就在那个库里验证(auth) 否则会失败 创建 账号管理授权权限 的账号 &g ...

  4. 【译】x86程序员手册33-9.6中断任务和中断处理程序

    9.6 Interrupt Tasks and Interrupt Procedures 中断任务和中断处理程序 Just as a CALL instruction can call either ...

  5. codeforces_B. Forgery

    http://codeforces.com/contest/1059/problem/B 题意: For simplicity, the signature is represented as an  ...

  6. 【转】用jquery编写动态的返回顶部特效

    jquery代码: function gotoTop(min_height){ //预定义返回顶部的html代码,它的css样式默认为不显示 var gotoTop_html = '<div i ...

  7. controller,sevices层,java初步了解

    一.controller层 二.service层 1.接口 2.接口的实现 转换 ClearingAccountArgument对象

  8. 阿里云报错Redirecting to /bin/systemctl restart sshd.service

    转:http://blog.csdn.net/caijunfen/article/details/70599138 云服务器 ECS Linux CentOS 7 下重启服务不再通过 service  ...

  9. NSFileHandle类

    Objective-C使用NSFileHandle类对文件进行基本操作,IOS文件操作 NSFileHandle类中得方法可以对文件进行基本的读写,偏移量的操作.NSFileHandle基本步骤:1. ...

  10. jquery的$().each,$.each的区别02

    在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法.两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点. $().each,对于这个方法,在d ...