World Finals 2003 UVA - 1025 A Spy in the Metro(动态规划)
分析:时间是一个天然的序,这个题目中应该决策的只有时间和车站,使用dp[i][j]表示到达i时间,j车站在地上已经等待的最小时间,决策方式有三种,第一种:等待一秒钟转移到dp[i+1][j]的状态,代价为1。第二种:如果可以则向右上车,转移到dp[i+t][j+1],无代价,t为列车行驶时间。第三种与第二种相同。初始状态为dp[0][1] = 0,其他为INF。答案为dp[T][n]。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define N 55
#define INF 1e9
bool gol[N][N*],gor[N][N*];
int dp[N*][N];
int main()
{
int n,T,t[N],m1,m2,d,e,sum,ans,ca=;
while(cin>>n && n)
{
cin>>T;
for(int i = ; i <= n; i++)
{
cin>>t[i];
}
memset(gol,false,sizeof(gol));
memset(gor,false,sizeof(gor));
cin>>m1;
for(int i = ; i < m1; i++)
{
cin>>d;
gor[][d] = true;
sum = d;
for(int j = ; j <= n; j++)
{
sum += t[j];
gor[j][sum] = true;
}
}
cin>>m2;
for(int i = ; i < m2; i++)
{
cin>>d;
gol[n][d] = true;
sum = d;
for(int j = n-; j >= ; j--)
{
sum += t[j+];
gol[j][sum] = true;
}
}
for(int i = ; i <= T; i++)
{
for(int j = ; j <= n; j++)
{
dp[i][j] = INF;
}
}
dp[][] = ;
for(int i = ; i <= T; i++)
{
for(int j = ; j <= n; j++)
{
if(i+ <= T) dp[i+][j] = min(dp[i][j]+,dp[i+][j]);
if(j+<=n&&gor[j][i]&&i+t[j+]<=T)dp[i+t[j+]][j+]=min(dp[i+t[j+]][j+],dp[i][j]);
if(j->=&&gol[j][i]&&i+t[j]<=T)dp[i+t[j]][j-]=min(dp[i+t[j]][j-],dp[i][j]);
}
}
printf("Case Number %d: ",++ca);
if(dp[T][n] >= INF) ans = -;
else ans = dp[T][n];
if(ans == -) printf("impossible\n");
else printf("%d\n",ans);
}
return ;
}
World Finals 2003 UVA - 1025 A Spy in the Metro(动态规划)的更多相关文章
- 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 (DP)
UVA 1025 -- A Spy in the Metro 题意: 一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...
- 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(动态规划)
传送门 Description Secret agent Maria was sent to Algorithms City to carry out an especially dangerous ...
- uva 1025 A Spy int the Metro
https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...
- 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 ...
- UVa 1025 A Spy in the Metro
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913 预处理出每个时间.每个车站是否有火车 为了方便判断是否可行,倒推处理 ...
- DAG的动态规划 (UVA 1025 A Spy in the Metro)
第一遍,刘汝佳提示+题解:回头再看!!! POINT: dp[time][sta]; 在time时刻在车站sta还需要最少等待多长时间: 终点的状态很确定必然是的 dp[T][N] = 0 ---即在 ...
- UVa 1025 A Spy in the Metro (DP动态规划)
题意:一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, 也就是尽量多坐车,最后输出最少等待时间. 析:这个挺复杂,首先时间是 ...
随机推荐
- 【转载】将python脚本打包成exe文件
exe文件也就是可以直接执行的文件.通常我们编好的带py后缀的脚本文件都是需要在有python的环境下执 行,每次通过Win + R打开运行窗口再输入powershell打开控制台,再千辛万苦地cd ...
- XueTr 0.45 (手工杀毒辅助工具) 绿色版
软件名称: XueTr 0.45 (手工杀毒辅助工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 3.3MB图片预览: ...
- 解决 .NET Core 中 GetHostAddressesAsync 引起的 EnyimMemcached 死锁问题
在我们将站点从 ASP.NET + Windows 迁移至 ASP.NET Core + Linux 的过程中,目前遇到的最大障碍就是 —— 没有可用的支持 .NET Core 的 memcached ...
- 2. Shell 传递参数
1. 概要 我们可以在执行 Shell脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推-- #!/bin/bas ...
- smartGit注意点
1.xcode工程默认会创建一个.git文件,去掉勾选 2.文件夹不显示,点击smartGit导航栏上的view-->Files from Subdirectories
- php 提交表单
滴答…滴答…的雨,欢迎大家光临我的博客. 学习是快乐的,教育是枯燥的. 博客园 首页 博问 闪存 联系 订阅 管理 随笔-58 评论-2017 文章-5 trackba ...
- .NET技术+25台服务器怎样支撑世界第54大网站
摘要:同时使用Linux和Windows平台产品,大量使用静态的方法和类,Stack Overflow是个重度性能控.同时,取代横向扩展,他们坚持着纵向扩展思路,因为“硬件永远比程序员便宜”. Sta ...
- tmux commands
最近在学Linux,用到tmux这个命令,看到很多快捷键的介绍,个人觉得不太好用,因此把几个常用的命令记录下来,以便以后学习和使用. 常用tmux commands: tmux ls ...
- QTP如何连接/查询数据库
在日常测试工作中经常会遇到有关于数据的测试,比如数据的正确性,完整性,测试人员造的测试数据预期与实际程序运行结果是否一致,如果这个时候数据量比较多的情况下,通过人工比对无疑是一项非常耗时,而且安全系数 ...
- IoC容器Autofac正篇之类型关联(服务暴露)(八)
类型关联 类型关联就是将类挂载到接口(一个或多个)上去,以方便外部以统一的方式进行调用(看下例). 一.As关联 我们在进行手动关联时,基本都是使用As进行关联的. 1 2 3 4 5 6 7 8 ...