UVA 1025_A Spy in the Metro
【题意】(小紫书)一个人从站台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的更多相关文章
- 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 ...
- UVA A Spy in the Metro
点击打开题目 题目大意: 在一个有n个站台的地铁线路里,给你列车通向每相邻两个车站所花费的时间,从0时刻开始,从1号站出发,要在T这个时间点上,到达n号站,给你m1辆从1开到n的列车及其出发时间,和m ...
- 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 ...
- uva 1025 A Spy in the Metro 解题报告
A Spy in the Metro Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug Secr ...
- UVA 1025 -- A Spy in the Metro (DP)
UVA 1025 -- A Spy in the Metro 题意: 一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...
- 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)
洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...
- UVA1025-A Spy in the Metro(动态规划)
Problem UVA1025-A Spy in the Metro Accept: 713 Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...
- UVa 1025 A Spy in the Metro(动态规划)
传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous ...
- 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 ...
随机推荐
- fullpagejs实现的拥有header和foooter的全屏滚动demo/fullpage footer
fullpagejs实现的拥有header和foooter的全屏滚动, 技术要点:给section元素加fp-auto-height类, <!DOCTYPE html> <html ...
- http的请求与响应-----content-type
content-type 指请求消息头的中请求消息数据的格式 有三种用法 第一种:设置在request header的参数中 js中可以在发送请求前在请求消息头中设置content-typevar x ...
- Hadoop YARN学习之组件功能简述(3)
Hadoop YARN学习之组件功能简述(3) 1. YARN的三大组件功能简述: ResourceManager(RM)是集群的资源的仲裁者, 它有两部分:一个可插拔的调度器和一个Applicati ...
- Jauery 中Ajax的几种异步请求
以下介绍Jquery中 Post Get Ajax几种异步请求的使用方法 <%@ Page Language="C#" AutoEventWireup=&q ...
- 掌握Spark机器学习库-07.14-保序回归算法实现房价预测
数据集 house.csv 数据集概览 代码 package org.apache.spark.examples.examplesforml import org.apache.spark.ml.cl ...
- 用16G内存在Java
用16G内存在Java Map中处理30亿对象 在一个下雨的夜晚,我在思考Java中内存管理的问题,以及Java集合对内存使用的效率情况.我做了一个简单的实验,测试在16G内存条件下,Java的Map ...
- java实现的单点登录
摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统当中.本文从业务的角度分析了单点登录的需求和应用领域:从技术本身的角度分析了单点登录技术的内部机制和实现手段,并且给出Web-SSO ...
- 13C++异常处理
异常处理 14.1.1 异常处理的任务 程序编制者不仅要考虑程序没有错误的理想情况,更要考虑程序存在错误时的情况,应该能够尽快地发现错误,消除错误. 程序中常见的错误有两大类: 语法错误和运行错误.在 ...
- BZOJ 3595: [Scoi2014]方伯伯的Oj Splay + 动态裂点 + 卡常
Description 方伯伯正在做他的Oj.现在他在处理Oj上的用户排名问题. Oj上注册了n个用户,编号为1-”,一开始他们按照编号排名.方伯伯会按照心情对这些用户做以下四种操作,修改用户的排名和 ...
- jquery ajax 同步异步的执行
jquery ajax 同步异步的执行 大家先看一段简单的jquery ajax 返回值的js 代码 function getReturnAjax{ $.ajax({ type:" ...