【题意】(小紫书)一个人从站台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. Oracle中默认创建的表

    安装Oracle数据库后,会自动创建几个表.分别是emp.dept.bonus(也有可能不一样),这些表都在scott账户中.

  2. 可变类型的安全性——更锋利的C#代码小记(2)

    ReadOnlyCollection类型是.NET系统类库提供的一个只读集合类型,它与原来的List<string>不存在任何类型转换关系,因此可以从根本上阻止外部对其的修改操作using ...

  3. AJPFX实列判断一个字符串是不是对称字符串

    import java.util.Scanner; /** *        判断一个字符串是否是对称字符串 */ public class StringDemo { public static vo ...

  4. js跨域请求的5中解决方式

    跨域请求数据解决方案主要有如下解决方法: ? 1 2 3 4 5 JSONP方式 表单POST方式 服务器代理 Html5的XDomainRequest Flash request 分开说明: 一.J ...

  5. iOS Programming Editing UITableView

    iOS Programming Editing UITableView 1.1 Editing mode  UITableView has an editing property, and when ...

  6. C语言基础-循环结构

    循环结构while while循环-图例 while循环-格式 while ( 条件 ) { 语句1; 语句2; .... } 如果条件成立,就会执行循环体中的语句(“循环体”就是while后面大括号 ...

  7. 仿陌陌的ios客户端+服务端源码

    软件功能:模仿陌陌客户端,功能很相似,注册.登陆.上传照片.浏览照片.浏览查找附近会员.关注.取消关注.聊天.语音和文字聊天,还有拼车和搭车的功能,支持微博分享和查找好友. 后台是php+mysql, ...

  8. 洛谷P2863 [USACO06JAN]牛的舞会The Cow Prom

    代码是粘的,庆幸我还能看懂. #include<iostream> #include<cstdio> #include<cmath> #include<alg ...

  9. vue之Render函数

    (1)什么是Render函数 先来看一个场景,在博客网中,一般有一级标题.二级标题.三级标题...,为了方便分享url,它们都做成了锚点,点击后,会将内容加载网址后面,以#分隔. 例如‘特性’是一个& ...

  10. mitmproxy 数据抓包

    1.安装环境: 基于python windows操作系统需要安装Microsoft Visual C++ V14.0以上 linux操作系统则直接基于python安装即可 2.安装mitmproxy ...