UVA 1025 "A Spy in the Metro " (DAG上的动态规划?? or 背包问题??)
参考资料:
[1]:算法竞赛入门经典:第九章 DAG上的动态规划
题意:
Algorithm城市的地铁有 n 个站台,编号为 1~n,共有 M1+M2 辆列车驶过;
其中 M1 辆列车从 1 号站台驶向 n 号站台,M2 辆列车从 n 号站台驶向 1 号地铁;
(单程线,M1 辆列车到达 n 号站台后不会回返,同理 M2)
特工 Maria 要在 T 时刻到达 n 号站台与特务会面,但为了保证安全,在会面前尽量呆在行进的列车中;
现给出你这 M1+M2 辆列车的发车时刻;
问如何换乘列车使得特工 Maria 能在 T 时刻前到达 n 号站台,并且在换乘期间在站台的停留时间最短;
如果可以在规定时间到达 n 站台,输出在站台停留的最短时间,反之,输出 "impossible";
题解:
看完书上的解释后,感觉,不像是DAG上的动态规划,倒有点像背包的味道;
int n,t;
int m1,m2;
int f[maxn];///前m1辆列车的发车时刻
int e[maxn];///后m2辆列车的发车时刻
int c[maxn];///c[i]:车站i到车站i+1的时间花费
/**
(i,j):i时刻在车站j
dp[i][j]:从(i,j)->(t,n)所需等待的最少时间
*/
int dp[maxn][];
/**
hasTrain[i][j][0]=true:i时刻在车站j有到j+1的火车
hasTrain[i][j][1]=true:i时刻在车站j有到j-1的火车
*/
bool hasTrain[maxn][][];
最关键的便是dp[ i ][ j ]的定义;
之所以定义成二维的,是因为决策受当前时间和所处车站的影响,有两个影响因素;
定义好后,便是找状态转移方程了;
首先预处理出 hasTrain 数组:
void Init()///预处理hasTrain
{
mem(hasTrain,false);
for(int i=;i <= m1;++i)
{
int cnt=f[i];
hasTrain[cnt][][]=true;
for(int j=;j <= n;++j)
{
cnt += c[j-];
hasTrain[cnt][j][]=true;
}
}
for(int i=;i <= m2;++i)
{
int cnt=e[i];
hasTrain[cnt][n][]=true;
for(int j=n-;j >= ;--j)
{
cnt += c[j];
hasTrain[cnt][j][]=true;
}
}
}
预处理hasTrain[]
令dp[t][n]=0,dp[t][1,2,...,n-1]=INF;
按照时间逆序遍历,对于状态 dp[ i ][ j ]:
①等一分钟,下一分钟从车站 j 出发到达(t , n);
②搭乘往右开的列车;
③搭乘往左开的列车;

AC代码:
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
const int maxn=+; int n,t;
int m1,m2;
int f[maxn];///前m1辆列车的发车时刻
int e[maxn];///后m2辆列车的发车时刻
int c[maxn];///c[i]:车站i到车站i+1的时间花费
/**
(i,j):i时刻在车站j
dp[i][j]:从(i,j)->(t,n)所需等待的最少时间
*/
int dp[maxn][];
/**
hasTrain[i][j][0]=true:i时刻在车站j有到j+1的火车
hasTrain[i][j][1]=true:i时刻在车站j有到j-1的火车
*/
bool hasTrain[maxn][][]; void Init()///预处理hasTrain
{
mem(hasTrain,false);
for(int i=;i <= m1;++i)
{
int cnt=f[i];
hasTrain[cnt][][]=true;
for(int j=;j <= n;++j)
{
cnt += c[j-];
hasTrain[cnt][j][]=true;
}
}
for(int i=;i <= m2;++i)
{
int cnt=e[i];
hasTrain[cnt][n][]=true;
for(int j=n-;j >= ;--j)
{
cnt += c[j];
hasTrain[cnt][j][]=true;
}
}
}
void Solve()
{
Init();
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 && hasTrain[i][j][] && i+c[j] <= t)
dp[i][j]=min(dp[i][j],dp[i+c[j]][j+]);
if(j > && hasTrain[i][j][] && i+c[j-] <= t)
dp[i][j]=min(dp[i][j],dp[i+c[j-]][j-]);
}
}
if(dp[][] >= INF)
puts("impossible");
else
printf("%d\n",dp[][]);
}
int main()
{
int kase=;
while(~scanf("%d",&n) && n)
{
scanf("%d",&t);
for(int i=;i < n;++i)
scanf("%d",c+i);
scanf("%d",&m1);
for(int i=;i <= m1;++i)
scanf("%d",f+i);
scanf("%d",&m2);
for(int i=;i <= m2;++i)
scanf("%d",e+i); printf("Case Number %d: ",++kase);
Solve();
}
return ;
}
UVA 1025 "A Spy in the Metro " (DAG上的动态规划?? or 背包问题??)的更多相关文章
- 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 437 The Tower of Babylon(DAG上的动态规划)
题目大意是根据所给的有无限多个的n种立方体,求其所堆砌成的塔最大高度. 方法1,建图求解,可以把问题转化成求DAG上的最长路问题 #include <cstdio> #include &l ...
- 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 ...
- 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(动态规划)
传送门 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
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35913 预处理出每个时间.每个车站是否有火车 为了方便判断是否可行,倒推处理 ...
随机推荐
- 无线传感网络协议——Smart Mesh IP
前言: SmartMesh IP 专为实现 IP 兼容性而设计,并基于 6LoWPAN 和 802.15.4e 标准.SmartMesh IP 产品线实现了网络适应性.可靠性和可扩展性水平,并拥有高级 ...
- Find Minimumd in Rotated Sorted Array
二分搜索查最小数,from mid to分别为区间的第一个,中位数,和最后一个数 if(from<=mid&&mid<=to)//顺序,第一个即为最小值 return fr ...
- golang数据运算符
- IDEA:将WEB-INF\lib下的Jar包添加到项目中
打开Project Structure[可以使用快捷键:Ctrl+Alt+Shift+S] 左侧选中Modules,在Dependecies中,点击右侧"+"号,选择JARS or ...
- IntelliJ IDEA 添加项目后编译显示包不存在的解决方案
File -> Project Structure -> Modules 看看是否有多个项目,删掉无用的.或者调整一下项目,重新 Mark as 一下,指定成 Sources
- oralce如何修改默认的XDB监听端口
Oracle9i默认的XML DB把HTTP的默认端口设为8080,这是一个太常用的端口了,很多别的WebServer都会使用这个端口, 如果我们安装了它,最好修改一下,避免冲突,如果不使用呢,就最好 ...
- JavaScript学习之 倒计时
倒计时很常见,例如离XX活动还有XX天XX小时XX分XX秒,然后逐秒减少,实现很简单,我只是想记录这过程中的一点小坑. 先上代码: <html> <head> <meta ...
- 登录注册beta版
注册 login_count = 0 username_inp = input('请输入用户名:') while login_count < 3: pwd_inp = input('请输入密码: ...
- poj3308 最小割
因为行可以了,那列就不行,所以根据行列建立最小割模型. 然后这题精妙之处在于把乘法取对数后转化为加法,瞬间就简单了. 保证精度,C++AC ,16MS G++WA. #include<stdio ...
- Chrome的使用技巧总结
设置一>” 首先设置打开特定的网页 设置--> “下载位置” (Ctrl+H),快速查找自己浏览器访问网页的历史记录 (Ctrl+D),将目前认为比较好的网页保存. ctrl+t 新建标 ...