题目链接: https://vjudge.net/problem/UVA-1025

题解:

详情请看紫书P267。 与其说是DP题,我觉得更像是模拟题,特别是用记忆化搜索写。

递推:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 200+10; int n, T, M1, M2;
int t[maxn], has_train[maxn][maxn][2], dp[maxn][maxn];
int kase = 0; void init()
{
ms(t,0);
ms(has_train,0);
ms(dp,0); scanf("%d",&T);
for(int i = 1; i<n; i++)
scanf("%d",&t[i]); int M1, x;
scanf("%d",&M1);
for(int i = 1; i<=M1; i++)
{
scanf("%d",&x);
for(int j = 1; j<=n && x<=T; j++)
{
has_train[x][j][0] = 1;
x += t[j];
}
} int M2;
scanf("%d",&M2);
for(int i = 1; i<=M2; i++)
{
scanf("%d",&x);
for(int j = n; j>=1 && x<=T; j--)
{
has_train[x][j][1] = 1;
x += t[j-1];
}
}
} void solve()
{
for(int i = 1; i<=n-1; i++)
dp[i][j] = INF; dp[T][n] = 0;
for(int i = T-1; i>=0; i--)
for(int j = n; j>=1; 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: ",++kase);
if(dp[0][1]<INF)
printf("%d\n",dp[0][1]);
else
puts("impossible");
} int main()
{
while(scanf("%d",&n) && n)
{
init();
solve();
}
return 0;
}

记忆化搜索:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 200+10; int n, T, M1, M2;
int t[maxn], has_train[maxn][maxn][2], dp[maxn][maxn];
int kase = 0; void init()
{
ms(t,0);
ms(has_train,0);
ms(dp,-1); scanf("%d",&T);
for(int i = 1; i<n; i++)
scanf("%d",&t[i]); int M1, x;
scanf("%d",&M1);
for(int i = 1; i<=M1; i++)
{
scanf("%d",&x);
for(int j = 1; j<=n && x<=T; j++)
{
has_train[x][j][0] = 1;
x += t[j];
}
} int M2;
scanf("%d",&M2);
for(int i = 1; i<=M2; i++)
{
scanf("%d",&x);
for(int j = n; j>=1 && x<=T; j--)
{
has_train[x][j][1] = 1;
x += t[j-1];
}
} for(int i = 1; i<n; i++)
dp[T][i] = INF;
dp[T][n] = 0;
} int dfs(int i, int j)
{
if(dp[i][j]!=-1) return dp[i][j]; dp[i][j] = dfs(i+1, j) + 1; if(j<n && has_train[i][j][0] && i+t[j]<=T)
dp[i][j] = min( dp[i][j], dfs( 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], dfs( i+t[j-1], j-1 ) ); return dp[i][j];
} int main()
{
while(scanf("%d",&n) && n)
{
init();
dfs(0,1); printf("Case Number %d: ",++kase);
if(dp[0][1]<INF)
printf("%d\n",dp[0][1]);
else
puts("impossible");
}
return 0;
}

UVA1025 A Spy in the Metro —— DP的更多相关文章

  1. 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 ...

  2. UVA 1025 -- A Spy in the Metro (DP)

     UVA 1025 -- A Spy in the Metro  题意:  一个间谍要从第一个车站到第n个车站去会见另一个,在是期间有n个车站,有来回的车站,让你在时间T内时到达n,并且等车时间最短, ...

  3. 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍

    参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...

  4. Uva1025 A Spy in the Metro

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ]; ] ...

  5. 题解:UVa1025 A Spy in the Metro

    原题链接 pdf 题目大意 给出一张无向图图,求该图的最小瓶颈生成树. 无向图的瓶颈生成树:无向图\(G\)的一颗瓶颈生成树是这样的一颗生成树:它最大的边权值在\(G\)的所有生成树中是最小的.瓶颈生 ...

  6. 【Uva1025 A Spy in the Metro】动态规划

    题目描述 某城市地铁是线性的,有n(2≤n≤50)个车站,从左到右编号1~n.有M1辆列车从第1站开始往右开,还有M2辆列车从第n站开始往左开.列车在相邻站台间所需的运行时间是固定的,因为所有列车的运 ...

  7. 洛谷2583 地铁间谍 (UVa1025A Spy in the Metro)

    洛谷2583 地铁间谍(UVa1025A Spy in the Metro) 本题地址:http://www.luogu.org/problem/show?pid=2583 题目描述 特工玛利亚被送到 ...

  8. UVA1025-A Spy in the Metro(动态规划)

    Problem UVA1025-A Spy in the Metro Accept: 713  Submit: 6160Time Limit: 3000 mSec Problem Descriptio ...

  9. uva 1025 A Spy in the Metro 解题报告

    A Spy in the Metro Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug Secr ...

随机推荐

  1. java retry:详解

    发现 今天在探秘线程池原理知识点,在阅读JDK源码时遇到程序代码中出现如下代码,因为之前没有遇到过,于是特地记录下来并谷歌了一番,后面我自己做了一些简要的验证和分析. 验证 网上溜达一番发现,这ret ...

  2. Uncaught TypeError: window.showModalDialog is not a function

    if(window.showModalDialog == undefined){ window.showModalDialog = function(url,mixedVar,features){ w ...

  3. java 字符串压缩长度并解压

    package com.wy.data.emaildata.util; import org.apache.commons.io.FileUtils; import java.io.ByteArray ...

  4. spring-aop AnnotationAwareAspectJAutoProxyCreator类

    类图结构如上所示.

  5. Eclipse工程中Java Build Path中的JDK版本和Java Compiler Compiler compliance level的区别(转)

    在这里记录一下在eclipse中比较容易搞混淆和设置错误的地方.如下图所示的功能: 最精准的解释如下: Build Path是运行时环境  Compiler是编译时环境  假设,你的代码用到泛型,Bu ...

  6. TIdHTTPServer制作REST中间件

    TIdHTTPServer制作REST中间件 使用DELPHI7+INDY9开发 // 陈新光 2017-2-21// LIS数据同步服务器// 浏览器输入:http://127.0.0.1:8000 ...

  7. webpack入门(六)——html-webpack-plugin

    html-webpack-plugin 该插件可以简化创建调用webpack bundles的html文件.在每次编译后,文件名会包含有hash值的bundles 特别有用.你可以让插件为您生成一个H ...

  8. Delphi图像处理 -- 文章索引

    转载:http://blog.csdn.net/maozefa/article/details/7188354 本文对已发布<Delphi图像处理>系列文章进行索引链接,以方便阅读和查找. ...

  9. urllib(最基本的)库的应用

    Urllib库 python内置的http请求库 1.urllib.request 请求模块 2.urllib.error 异常处理模块(try,catch) 3.urllib.parse url解析 ...

  10. 更改已经签名的app中的内容

    转载请说明出处http://blog.csdn.net/andywuchuanlong 记得上次在南昌中兴的一个项目中遇到过一个这种需求:一个app能够给多个渠道商去运营,渠道商推广出去能够获得对应的 ...