【题意】(小紫书)一个人从站台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. MySQL+PHP配置 Windows系统IIS版

    MySQL+PHP配置 Windows系统IIS版 1.下载 MySQL下载地址:http://dev.mysql.com/downloads/mysql/5.1.html->Windows ( ...

  2. C# 代码笔记_tuple元组

    tuple元组: 赋值 List<Tuple<string, int>> cc = new List<Tuple<string, int>>() { n ...

  3. HTML5 File API的应用

    HTML5 File API简介 HTML5File API协议族 Directories and System   文件系统和目录读取 FileWriter   写入文件 FileReader   ...

  4. php中读取以及写入文件的方法总结

    ==>读取文件内容(方法一) $fileData = fread($fileStream,filesize($filePath)); 注意: 文本文件读取到网页上显示时,由于换行符不被解释,文本 ...

  5. 绿化VSCode

    通过啃源码, 终于找到了解决办法, 设置环境变量: VSCODE_APPDATA=C:\Program Files\VSCode\UserData VSCODE_EXTENSIONS=%VSCODE_ ...

  6. sql中的日期时间处理

    每个数据库,不同的日期格式化: 1.mysql 2.sqlserver 使用Convert()函数: select convert(char(10),GetDate(),120) as Date 第3 ...

  7. 手动配置webpack

    //注:“__dirname”是node.js中的一个全局变量,它指向当前执行脚本所在的目录.const path = require('path');const webpack = require( ...

  8. oracle数据库使用hint来让模糊查询走索引

    在没有创建数据直方图之前,查询优化器是cbo,可能不会选择代价最低(效率最高)的方式查询. 先创建表 --日语假名表 CREATE TABLE JAPANESE_SOUNDMARK ( ID INTE ...

  9. 原生 js 上传图片

    js <!doctype html> <html> <head> <meta charset="utf-8"> <title& ...

  10. js+flash(as3)实现复制文字内容到剪切板实例

    /* SWFObject v2.2 swfobject.js */ var swfobject=function(){var D="undefined",r="objec ...