DAG的动态规划 (UVA 1025 A Spy in the Metro)
第一遍,刘汝佳提示+题解;回头再看!!!
POINT:
dp[time][sta]; 在time时刻在车站sta还需要最少等待多长时间;
终点的状态很确定必然是的 dp[T][N] = 0 ---即在T时刻的时候正好达到N站点
我们可以 从终点的状态往起始的状态转化, 一步步走就可以了。
has_train[t][i][0]; t时刻在i车站是否有往右开的火车
has_train[t][i][1]; t时刻在i车站是否有往左开的火车
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
using namespace std; #define INF 0xffffff7
#define maxn 200+10 int N, T;
int t[maxn];
int dp[maxn][maxn];//dp[i][j] i时刻在j车站最少等候时间
int has_train[maxn][maxn][];
int main()
{
//freopen("out.txt", "r", stdout);
int kase = ;
int N;
while(scanf("%d", &N) && N)
{
memset(has_train, , sizeof(has_train));
memset(t, , sizeof(t));
kase++;
scanf("%d", &T);
for(int i = ; i < N; i++)
scanf("%d", &t[i]); int M1, M2;
scanf("%d", &M1);
for(int i = ; i <= M1; i++)
{
int start;
scanf("%d", &start);
has_train[start][][] = ; // 标记初始站台
for(int j = ; j < N; j++)
{
int time = start + t[j];
if(time <= T)
{
has_train[time][j+][] = ;
start += t[j];
}
}
}
scanf("%d", &M2);
for(int i = ; i <= M2; i++)
{
int start;
scanf("%d", &start);
has_train[start][N][] = ; // 标记初始站台
for(int j = N-; j >= ; j--)
{
int time = start + t[j];
if(time <= T)
{
has_train[time][j][] = ;
//cout << "has[" << time << "][" << j << "][1] " << endl;
start += t[j];
}
}
}
printf("Case Number %d: ", kase); 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] + ; //第T-1时刻在j车站最少等候时间 = 第T时刻在j车站最少等候时间+1; 即决策一---等一分钟
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-]);
}
}
} if(dp[][] >= INF) printf("impossible\n");
else printf("%d\n", dp[][]);
}
return ;
}
DAG的动态规划 (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 " (DAG上的动态规划?? or 背包问题??)
传送门 参考资料: [1]:算法竞赛入门经典:第九章 DAG上的动态规划 题意: Algorithm城市的地铁有 n 个站台,编号为 1~n,共有 M1+M2 辆列车驶过: 其中 M1 辆列车从 1 ...
- 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 in the Metro 【DAG上DP/逆推/三维标记数组+二维状态数组】
Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After s ...
- World Finals 2003 UVA - 1025 A Spy in the Metro(动态规划)
分析:时间是一个天然的序,这个题目中应该决策的只有时间和车站,使用dp[i][j]表示到达i时间,j车站在地上已经等待的最小时间,决策方式有三种,第一种:等待一秒钟转移到dp[i+1][j]的状态,代 ...
- UVa 1025 A Spy in the Metro (DP动态规划)
题意:一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, 也就是尽量多坐车,最后输出最少等待时间. 析:这个挺复杂,首先时间是 ...
- uva 1025 A Spy int the Metro
https://vjudge.net/problem/UVA-1025 看见spy忍俊不禁的想起省赛时不知道spy啥意思 ( >_< f[i][j]表示i时刻处于j站所需的最少等待时间,有 ...
随机推荐
- Java设计模式系列之适配器模式
适配器模式的定义 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.(就类似于我们充电器的转接头将220V的电压转换成我们的手机端 ...
- Xshell异常断开
这可能是由于 SSH 超时断开连接 导致的!可以这样做...修改/etc/ssh/sshd_config文件,找到 ClientAliveInterval 0和ClientAliveCountMax ...
- [iOS基础控件 - 6.9] 聊天界面Demo
A.需求 做出一个类似于QQ.微信的聊天界面 1.每个cell包含发送时间.发送人(头像).发送信息 2.使用对方头像放在左边,我方头像在右边 3.对方信息使用白色背景对话框,我方信息使用蓝色背景对话 ...
- [C语言 - 10] C语言保留字
一 数据类型关键字 12 个: 1 . char 2 . short 3 . int 4 . long 5. enum 6. float 7. dou ...
- string中c_str()、data()、copy(p,n)函数的用法
标准库的string类提供了3个成员函数来从一个string得到c类型的字符数组:c_str().data().copy(p,n). 1. c_str():生成一个const char*指针,指向以空 ...
- Javascript高级篇-JS闭包
Javascript闭包 1.变量的作用域 1.1局部变量 1.2全局变量(声明在外边或不用var来声明的变量) 2.外部读取方法内部的局部(私有)变量 function a(){ var b = & ...
- 转载“启动\关闭Oracle数据库的多种方法”--来自百度#Oracle
启动\关闭Oracle数据库的多种方法 启动和关闭oracle有很多种方法. 这里只给出3种方法: l Sql*plus l OEM控制台 l Wind ...
- 短信验证倒计时60s
$("#zphone").click(function(){ var tel2 = $("#regTel").val(); if(flag.tel){ $.po ...
- SQL创建linkserver
建立链接服务器并创建同义词,有一个最大的好处,就是可以跨数据库实例进行操作数据库,可以在一个数据库连接内完成数据操作,方便做事务查询. 在SQL SERVER 2008里,可以按以下的方式建立 ...
- Codeforces Round #332 (Div. 2) B. Spongebob and Joke 水题
B. Spongebob and Joke Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599 ...