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

https://blog.csdn.net/c20180630/article/details/75245665

https://blog.csdn.net/rechard_chen/article/details/41357173

https://blog.csdn.net/acvay/article/details/43565183

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=;
const int M=;
const int INF=1e8;
int n,t,l,r,kcase;//n车站数,t时间,l向右车辆数,kcase样例数
int station_time[N];//车站之间的时间
int have_train[M][N][],dp[M][N];
void test()
{
cout<<'n'<<n<<'t'<<t<<endl;
for (int i=;i<n;i++)
{
cout<<station_time[i]<<' ';
}
cout<<endl;
for (int i=;i<=t;i++)
{
for (int j=;j<=n;j++)
{
printf("[%9d]",dp[i][j]);
}
printf("\n");
}
getchar();
}
void init()
{
cin>>t;
memset(station_time,,sizeof(station_time));
for (int i=;i<n-;i++)
{
cin>>station_time[i];
}
cin>>r;
memset(have_train,,sizeof(have_train));
while (r--)
{
int d;
cin>>d;
for (int j=;j<n;j++)
{
if (d<=t)
{
have_train[d][j][]=;
}
d+=station_time[j-];
}
}
cin>>l;
while (l--)
{
int d;
cin>>d;
for (int j=n;j>;j--)
{
if (d<=t)
{
have_train[d][j][]=;
}
d+=station_time[j-];
}
}
}
void dpp()
{
memset(dp,,sizeof(dp));
for (int j=;j<=n;j++)
{
dp[t][j]=INF;
}
dp[t][n]=;
for (int i=t-;i>=;i--)//注意i>=0!
{
for (int j=;j<=n;j++)
{
dp[i][j]=dp[i+][j]+;
if (j<n&&have_train[i][j][]&&i+station_time[j-]<=t)
{
dp[i][j]=min(dp[i][j],dp[i+station_time[j-]][j+]);
}
if (j>&&have_train[i][j][]&&i+station_time[j-]<=t)//注意判断条件不要写错!
{
dp[i][j]=min(dp[i][j],dp[i+station_time[j-]][j-]);
}
} }
// test();
cout<<"Case Number "<<kcase++<<": ";
if (dp[][]>=INF)
{
cout<<"impossible"<<endl;
}
else
{
cout<<dp[][]<<endl;
}
}
int main()
{
kcase=;
// freopen("btext.txt","r",stdin);
while (cin>>n&&n)
{
init();
dpp();
} return ;
}

【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍的更多相关文章

  1. UVa-1025城市里的间谍 A Spy in the Metro

    原题 城市里的间谍 分析 动态规划,dp[i][j]表示你在时刻i,车站j,最少还要等待的时间. 边界条件d[T][n]=0 已经到达,其他d[T][i]=inf不可达. 在一个站点时,有以下三种决策 ...

  2. 城市里的间谍B901

    城市里的间谍   城市里的间谍 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 某城市的地铁是线性的,有 n(2 <= n ...

  3. 9-1 A Spy in the Metro uva1025 城市里的间谍 (DP)

    非常有价值的dp题目  也是我做的第一题dp    真的效率好高 题意:某城市的地铁是线性的 有n个车站 从左到右编号为1-n  有m1辆列车从第一站开始往右开 还有m2辆列车从第n站开始往左开  在 ...

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

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

  5. UVA1025 A Spy in the Metro —— DP

    题目链接: https://vjudge.net/problem/UVA-1025 题解: 详情请看紫书P267. 与其说是DP题,我觉得更像是模拟题,特别是用记忆化搜索写. 递推: #include ...

  6. Uva1025 A Spy in the Metro

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

  7. 题解:UVa1025 A Spy in the Metro

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

  8. UVA1025 城市里的间谍

    #include<iostream> #include<cstdio> #include<memory.h> using namespace std; #defin ...

  9. UVa 1025 城市里的间谍

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

随机推荐

  1. python学习笔记(1)----python安装

    1.下载Python for windows 废话不说,直接上网址:https://www.python.org/ftp/python/3.5.1/python-3.5.1.exe 2.安装Pytho ...

  2. 简单记录一下http请求的7个步骤

    1.建立TCP连接 2.客户端发送请求命令 3.客户端发送请求头信息 4.服务端应答请求,返回版本号和状态码 5.服务端应答头信息 6.服务端向客户端发送数据 7.服务器关闭TCP连接(Connect ...

  3. debug问题记录

    debug进不去,发现断点没有带勾. 方案:去掉所有断点,重新编译,debug启动,重新打断点. source里面加入自己的java project. 关掉所有的java页面!!! dobbo服务用d ...

  4. CentOS7 查询已安装的包方便整理 /卸载

    以PHP为例.首先查看已安装的PHP rpm -qa |grep php 输出结果: php-cli--.el7.x86_64 php-common--.el7.x86_64 查询rpm包的安装时间和 ...

  5. May 21st 2017 Week 21st Sunday

    The smallest deed is better than the greatest intention. 最微小的行动胜过最伟大的打算. Several years ago, just aft ...

  6. March 8 2017 Week 10 Wednesday

    Rules are meant to be broken. 规则就是用来被打破的. What is innovation? Some may tell you innovation is to bre ...

  7. FFT算法实现——基于GPU的基2快速傅里叶变换

    最近做一个东西,要用到快速傅里叶变换,抱着蛋疼的心态,自己尝试写了一下,遇到一些问题. 首先看一下什么叫做快速傅里叶变换(FFT)(来自Wiki): 快速傅里叶变换(英语:Fast Fourier T ...

  8. 【转载】#446 - Deciding Between an Abstract Class and an Interface

    An abstract class is a base class that may have some members not implemented in the base class, but ...

  9. UVA515 King

    嘟嘟嘟 题目翻译:有n个数,m个限制条件.每一个限制条件形如:1.x y gt c:表示ax + ax+1 + … +ay > c.2.x y It c:表示ax + ax+1 + …… +ay ...

  10. 如何使用MiniProfiler(附最新版MiniProfiler使用心得)

    MiniProfiler这个工具早就久仰大名,不过之前一直没有动力去用,正好最近手上有个ASP.NET MVC的项目,正好拿来试试手,下面是使用最新的4.0.138版本的心得体会以及踩到一些小坑的解决 ...