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,并且等车时间最短, 也就是尽量多坐车,最后输出最少等待时间. 析:这个挺复杂,首先时间是 ...
随机推荐
- C++ 类中的引用成员变量初始化
刚遇到一个问题,需要的类成员为指针的引用,而引用不能在构造函数里初始化,必须在初始化列表中进行初始化,并且需要该引用在构造函数中的形参必须为引用形式 1: class ThreadParam { 2: ...
- redis win版安装
直接来看看redis怎么安装到windows系统上,并开启他的服务. 可以在这里下载https://github.com/dmajkic/redis/downloads. 我用的是64位的,解压后的结 ...
- tableviewcell滑动显示多个按钮UITableViewRowAction(转载)
demo截图 ios8 新的属性 typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) { UITableViewRowActionStyleDe ...
- 阿里云ECOS 集群方案
转载 https://it.toggle.cn/article_detail/7e6f674b2564d6c319f807b4fda87eac.html 架构说明 前端由阿里云SLB统一分发Web请求 ...
- gridControl 中CellValueChanged,ShowingEditor,CustomDrawCell的用法
private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventA ...
- c语言-经验之谈
如果你是一个大牛,那就直接忽略这里. 如果你是一个新手,请继续向下看. 在自学计算机的路上真的很悲惨,如果你是在学校里面学习还算比较幸运. 针对编程来说,在学校里面学习的只是学会了语言,而很少有人学会 ...
- leetcode415---字符串大数相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- vb asp.net的一些属性值
AutoGenerateColumns 就是自动产生列的意思gridview等控件,如果设置了AutoGenerateColumns=true,就能够根据数据源的实际情况,自动生成gridview表格 ...
- 使用OpenCV查找二值图中最大连通区域
http://blog.csdn.net/shaoxiaohu1/article/details/40272875 使用OpenCV查找二值图中最大连通区域 标签: OpenCVfindCoutour ...
- java泛型小问题
几年前当Java5还未正式发布的时候,看到过一些人写的介绍Tiger中的新特性,当时对我第一感觉冲击最大的就是泛型(generics)和注释(annotation),因为它们直接影响了我们编码的语法习 ...