UVA 1025 A Spy in the Metro (动态规划)
来源:互联网 发布:百度人工智能发展现状 编辑:程序博客网 时间:2024/06/15 20:57
紫书上动态规划例题,也是2003的Finals题。
紫书上说的十分浅显易懂。
对于这道题,我们首先要明白每一秒我们可以干什么。我们可以去前一个车站,可以去下一个车站,也可以在原地等一秒。
而影响我们选择哪种方案的要素为当前时间和所处的车站。
所以这两个就成为我们要不断调节的元素。我们采用dp[i][j]代表当前是第i分钟,所处第j个车站,我们希望停留的时间尽量少,所以我们dp里面每个位置记录的就是当前情况下所花费的最小等待时间。
我们首先令dp[T][n]为0,因为你必须在第T时刻恰好到达n车站。然后就是不断向前递推了。
每一秒在某个车站都满足,dp[i][j] = dp[i + 1][j] + 1.这样就相当于等了一秒。
但是如果当前有地铁经过的话,我们可以选择上与不上,此时递推式满足dp[i][j] = min(dp[i][j], dp[i + t[j]][j + 1])去下一个车站或者是反向的dp[i][j] = min(dp[i][j], dp[i - t[j - 1]][j - 1])去前一个车站。
至于如何判断当前时刻当前车站是否有地铁是很简单了。
如果最后发现第一个车站是没有更新过的,那就说明没有办法在指定时间内从车站1到车站n,直接输出impossible、
代码如下:
#include<iostream>#include<cstdio>#include<vector>#include<queue>#include<utility>#include<stack>#include<algorithm>#include<cstring>#include<string>#include<cmath>#include<set>#include<map>using namespace std;const int maxn = 300;const int INF = 0x3f3f3f3f;int n, T, m1, m2;int d1[maxn], d2[maxn], t[maxn];int dp[maxn][maxn];bool has_train[maxn][maxn][2];int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endifint Case = 1;while(scanf("%d", &n) != EOF) {if(n == 0)break;memset(has_train, 0, sizeof(has_train));memset(dp, 0, sizeof(dp));scanf("%d", &T);for(int i = 1; i < n; i++) {scanf("%d", &t[i]);}scanf("%d", &m1);for(int i = 0; i < m1; i++) {scanf("%d", &d1[i]);}scanf("%d", &m2);for(int i = 0; i < m2; i++) {scanf("%d", &d2[i]);}for(int i = 0; i < m1; i++) {int tmp = d1[i];for(int j = 1; j <= n; j++) {has_train[tmp][j][0] = 1;tmp += t[j];//if(tmp > T)//break;}}for(int i = 0; i < m2; i++) {int tmp = d2[i];for(int j = n; j >= 1; j--) {has_train[tmp][j][1] = 1;tmp += t[j - 1];//if(tmp > T)//break;}}for(int i = 1; i <= n - 1; i++)dp[T][i] = INF;dp[T][n] = 0;for(int i = T - 1; i >= 0; i--) {for(int j = 1; j <= n; j++) {dp[i][j] = dp[i + 1][j] + 1;if(j < n && has_train[i][j][0] && i + t[j] <= T)dp[i][j] = min(dp[i][j], dp[i + t[j]][j + 1]);if(j > 1 && has_train[i][j][1] && i + t[j - 1] <= T)dp[i][j] = min(dp[i][j], dp[i + t[j - 1]][j - 1]);}}printf("Case Number %d: ", Case++);if(dp[0][1] >= INF)printf("impossible\n");elseprintf("%d\n", dp[0][1]);}return 0;}
阅读全文
0 0
- UVA 1025 - A Spy in the Metro(动态规划)
- UVa 1025 - A Spy in the Metro(动态规划)
- UVA 1025 A Spy in the Metro [动态规划] [DAGdp]
- UVa 1025 -A Spy in the Metro(动态规划)
- UVA 1025 A Spy in the Metro (动态规划)
- uva 1025 A Spy in the Metro DAG上的动态规划(固定终点)
- UVA 1025A Spy in the Metro 动态规划 _ SPFA
- UVA 1025 A Spy in the Metro
- UVa 1025 A Spy in the Metro
- Uva 1025 A Spy in the Metro
- UVa - 1025 - A Spy in the Metro
- UVA 1025 A Spy in the Metro
- uva 1025 - A Spy in the Metro
- uva 1025 A Spy in the Metro
- UVa 1025 - A Spy in the Metro
- UVA 1025 A Spy in the Metro
- uva 1025 A Spy in the Metro
- UVa 1025 A Spy in the Metro
- java并发编程学习5--并行流
- 解决 Implementation of project facet jst.web.jstl could not be found. Functionality will be limited.
- 【有始有终,不让自己的付出辜负了自己期望 】
- 对于类和对象的简单认识
- 年总结兼英语小黑屋总结
- UVA 1025 A Spy in the Metro (动态规划)
- CopyOnWriteArrayList
- springmvc--无法找到视图PageNotFound.noHandlerFound No mapping found for HTTP request
- C语言:二叉树的三种遍历方式
- 有根树同构 (Hash)
- 20170827_int 转换成 string 的两种方法
- Java拾遗
- 二维数组练习05_计算鞍点
- php 日志类