UVA A Spy in the Metro
题目大意:
在一个有n个站台的地铁线路里,给你列车通向每相邻两个车站所花费的时间,从0时刻开始,从1号站出发,要在T这个时间点上,到达n号站,给你m1辆从1开到n的列车及其出发时间,和m2辆从n开到1的列车及其出发时间,求在车站停留的最短时间(注意,如果你在T之前的T’时到达n号站,那你也要花费T-T’的时间停留),如果无论如何也到不了n号站,输出“Impossible”
设f[i][j]表示在第i时刻到达j号车站,将每辆列车到每个站的时间预处理一下,则状态转移方程为:
f[i][j]=min(f[i][j],min(f[train1[o][j-1]][j-1]+i-train1[o][j],f[train2[o][j+1]][j+1]+i-train2[o][j]));
代码如下:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int f[201][51],train1[51][51],train2[51][51],t[51];
int T,n,m1,m2,ans;
int getint()
{
int num=0,flag=1;char c;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;
while(c>='0'&&c<='9')num=num*10+c-48,c=getchar();
return num*flag;
}
int main()
{
int i,j,o,k=0;
while((n=getint())!=0)
{
ans=1<<30;
memset(f,0x3f,sizeof f);
T=getint();
for(i=0;i<=T;i++)f[i][1]=i;
for(i=1;i<n;i++)t[i]=getint();
m1=getint();
for(i=1;i<=m1;i++)
{
train1[i][1]=getint();
for(j=1;j<n;j++)train1[i][j+1]=train1[i][j]+t[j];
}
m2=getint();
for(i=1;i<=m2;i++)
{
train2[i][n]=getint();
for(j=n;j>1;j--)train2[i][j-1]=train2[i][j]+t[j-1];
}
for(i=1;i<=T;i++)
for(j=1;j<=n;j++)
{
if(j>1)
{
for(o=1;o<=m1;o++)if(train1[o][j]<=i)
f[i][j]=min(f[train1[o][j-1]][j-1]+i-train1[o][j],f[i][j]);
}
if(j<n)
{
for(o=1;o<=m2;o++)if(train2[o][j]<=i)
f[i][j]=min(f[train2[o][j+1]][j+1]+i-train2[o][j],f[i][j]);
}
}
for(i=0;i<=T;i++)ans=min(f[i][n]+T-i,ans);
printf("Case Number %d: ",++k);
if(ans==0x3f3f3f3f)printf("impossible\n");
else printf("%d\n",ans);
}
}
UVA 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 1025_A Spy in the Metro
[题意](小紫书)一个人从站台1出发,乘车要在时刻T到达站台n,为使在站台等车时间最短,她可以选择乘坐两个方向的列车,并在客车停靠站的时候换车. [分析]每次停站下车时,她都有三种选择,1.原地不动 ...
- 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 ...
随机推荐
- vue-learning:27 - component - 组件三大API之二:event
组件三大API之二: event 在上一节中讲到prop单向下行数据绑定的特征,父组件向子组件传值通过prop实现,那如果有子组件需要向父组件传值或其它通信请求,可以通过vue的事件监听系统(触发事件 ...
- C/C++ 获取键盘事件
Windows 系统下的 vs 中可以使用 _kbhit() 函数来获取键盘事件,使用时需要加入 conio.h 头文件 #include <conio.h> #include <i ...
- Scala中的函数表达式
最近看Spark的东西,由于之前没有接触过lambda函数表达式,所以搜了点资料,特地纪录在此 Scala中的Lambda表达式 在函数式编程中,函数是基本的构造块.Scala融合了java中的面向对 ...
- HDU - 3530 Subsequence (单调队列)
Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- javascript 闭包的理解(一)
过很多谈如何理解闭包的方法,但大多数文章,都是照抄或者解释<Javascript高级程序设计(第三版)>对于闭包的讲解,甚至例程都不约而同的引用高程三181页‘闭包与变量’一节的那个“返回 ...
- 14.python函数
原文:https://www.cnblogs.com/linhaifeng/articles/6113086.html 1.python中函数定义:函数是逻辑结构化和过程化的一种编程方法. pytho ...
- mysql授权用户权限
mysql 用户管理和权限设置 用户管理 mysql>use mysql; 查看 mysql> select host,user,password from user ; 创建 mys ...
- JavaScript中浅拷贝和深拷贝的区别
JavaScript数据类型 基础数据类型:保存在栈内存中的简单数据段 ,有undefined,boolean,number,string,null 引用数据类型:Array,object,Funct ...
- Spring 配置内容外部化
- 「CH2501」 矩阵距离 解题报告
CH2501 矩阵距离 描述 给定一个N行M列的01矩阵 A,\(A[i][j]\) 与 \(A[k][l]\) 之间的曼哈顿距离定义为: \(dist(A[i][j],A[k][l])=|i-k|+ ...