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,并且等车时间最短, 也就是尽量多坐车,最后输出最少等待时间. 析:这个挺复杂,首先时间是 ...
随机推荐
- digitalocean最新优惠码赠送10美元
digitalocean是我非常喜欢的vps服务商,目前手头还有十来个digitalocean vps服务器.用了三年多digitalocean后,我发现digitalocean一点小技巧.比如,如果 ...
- UITextField的属性设置
1.背景颜色 field.backgoundColor = [UIColor redColor]; 2.设置field文字 field.text = @"输入文字"; 3.设置fi ...
- 单例:用AFNetworking封装举例
创建单例的方式 import AFNetworking class NetworkTools: AFHTTPSessionManager { //let是线程安全的 //使用单例实例只需要拿到Netw ...
- cookie和session的区别(搜狐笔试考到的一个题目)
一.cookie机制和session机制的区别***************************************************************************** ...
- POJ 3279 Fliptile[二进制状压DP]
题目链接[http://poj.org/problem?id=3279] 题意:给出一个大小为M*N(1 ≤ M ≤ 15; 1 ≤ N ≤ 15) 的图,图中每个格子代表一个灯泡,mp[i][j] ...
- drawableLayout的使用(转载讲的比较清晰的文章)
创建drawbler的布局文件 初始化drawbler的列表 响应drawable列表点击事件 现在侧滑菜单使用很多,大都是通过SlidingMenu实现.现在也可以通过DrawerLayout 创建 ...
- ListView间隔设置颜色
在Adapter的getView中 if (position % 2 == 0) holder.shoucangbt.setBackgroundColor( Color.rgb(246, 246, 2 ...
- 浅析PageRank算法
很早就对Google的PageRank算法很感兴趣,但一直没有深究,只有个轮廓性的概念.前几天趁团队outing的机会,在动车上看了一些相关的资料(PS:在动车上看看书真是一种享受),趁热打铁,将所看 ...
- python实现邮件发送
实例补充: #**************************利用STMP自动发送邮件******************************import smtplibsmtp = smtp ...
- Chapter 2 Open Book——21
I kept my head down and glanced up under my lashes. 我一直保持低着头并且在睫毛之下瞟了一眼. None of them were looking t ...