UVa 1025 城市里的间谍
https://vjudge.net/problem/UVA-1025
题意:一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短,输出最短等车时间。
思路:先用一个has_train[t][i][0]来表示在t时刻,在车站i,是否有往右开的车。同理,has_train[t][i][1]用来保存是否有往左开的车。
用d(i,j)表示时刻i,你在车站j,最少还需要等待多长时间。边界条件是d(T,n)=0,其他d(T,i)为正无穷。
每次有三种决策:
①:等一分钟。
②:搭成往右开的车(如果有)。
③:搭成往左开的车(如果有)。
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std; const int INF = 0x3f3f3f3f; int T, n, dp[][], m1, m2, has_train[][][],t[]; int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int kase = ;
while (cin >> n && n)
{
cin >> T;
for (int i = ; i < n; i++)
{
cin >> t[i];
}
memset(has_train, , sizeof(has_train));
int x;
cin >> m1;
for (int i = ; i < m1; i++)
{
cin >> x;
for (int j = ; x<=T && j <= n; j++)
{
has_train[x][j][] = ;
x += t[j];
}
} cin >> m2;
for (int i = ; i < m2; i++)
{
cin >> x;
for (int j = n; x <=T && j >; j--)
{
has_train[x][j][] = ;
x += t[j-];
}
} for (int i = ; i <= n - ; i++) dp[T][i] = INF;
dp[T][n] = ;
for (int i = T - ; i >= ; i--)
{
for (int j = ; j <= n; j++)
{
dp[i][j] = dp[i + ][j] + ; //等待一个单位
if (j < n && has_train[i][j][] && i + t[j] <= T)
dp[i][j] = min(dp[i][j], dp[i + t[j]][j + ]); //往右
if (j> && has_train[i][j][] && i + t[j - ] <= T)
dp[i][j] = min(dp[i][j], dp[i + t[j - ]][j - ]); //往左
}
}
cout << "Case Number " << ++kase << ": ";
if (dp[][] >= INF) cout << "impossible" << endl;
else cout << dp[][] << endl;
}
return ;
}
UVa 1025 城市里的间谍的更多相关文章
- UVa-1025城市里的间谍 A Spy in the Metro
原题 城市里的间谍 分析 动态规划,dp[i][j]表示你在时刻i,车站j,最少还要等待的时间. 边界条件d[T][n]=0 已经到达,其他d[T][i]=inf不可达. 在一个站点时,有以下三种决策 ...
- 城市里的间谍B901
城市里的间谍 城市里的间谍 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 某城市的地铁是线性的,有 n(2 <= n ...
- 9-1 A Spy in the Metro uva1025 城市里的间谍 (DP)
非常有价值的dp题目 也是我做的第一题dp 真的效率好高 题意:某城市的地铁是线性的 有n个车站 从左到右编号为1-n 有m1辆列车从第一站开始往右开 还有m2辆列车从第n站开始往左开 在 ...
- UVA1025 城市里的间谍
#include<iostream> #include<cstdio> #include<memory.h> using namespace std; #defin ...
- 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍
参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...
- UVA 1025 -- A Spy in the Metro (DP)
UVA 1025 -- A Spy in the Metro 题意: 一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...
- 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,城市的间谍
题目链接:https://uva.onlinejudge.org/external/10/1025.pdf 题意: 地铁是线性的,有n个站,编号(1~n),M1辆从左至右的车,和M2辆从右至左的车,发 ...
随机推荐
- U盘安装win10操作系统
https://www.zhihu.com/question/39207359 1:进入微软官方网站,点击立即下载工具,下载完成mediacreationtool,双击打开,接受协议 https ...
- Thread类的常见问题
void waitForSignal() { Object obj = new Object(); synchronized(Thread.currentThread()) { obj.wait(); ...
- [LeetCode] 394. Decode String_Medium tag: stack 666
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- Java中将xml文件转化为json的两种方式
原文地址https://blog.csdn.net/a532672728/article/details/76312475 最近有个需求,要将xml转json之后存储在redis中,找来找去发现整体来 ...
- 如何实现parseFloat保留小数点后2位
Number.toPrecision(2);function toPrecision ( [precision : Number] ) : String参数 precision 可选项.有效位数.值必 ...
- C++矩阵库 Eigen 快速入门
最近需要用 C++ 做一些数值计算,之前一直采用Matlab 混合编程的方式处理矩阵运算,非常麻烦,直到发现了 Eigen 库,简直相见恨晚,好用哭了. Eigen 是一个基于C++模板的线性代数库, ...
- HTTP 超文本协议
转载 :http://mp.weixin.qq.com/s/3d3zhksViX2NTuIssiYGJg
- python 打印输出至文件中, 'wt'读写文件方式,会把原文件内容清空
会把原文件内容清空,写最新的 文件必须以文本模式打开,如果文件是二进制模式的话,打印会出错
- Python tricks(7) -- new-style class的__slots__属性
__slots__是在python 2.2开始引入的一个新特性, 我们来看一下官方给出的解释. This class variable can be assigned a string, iterab ...
- MongoDB ----基于分布式文件存储的数据库
参考: http://www.cnblogs.com/huangxincheng/category/355399.html http://www.cnblogs.com/daizhj/category ...