UVA1347-Tour(动态规划基础)
Accept: 667 Submit: 3866
Time Limit: 3000 mSec
Problem Description
John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane pi =< xi,yi >. John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known that the points have distinct x-coordinates. Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John’s strategy.
Input
The program input is from a text file. Each data set in the file stands for a particular set of points. For each set of points the data set contains the number of points, and the point coordinates in ascending order of the x coordinate. White spaces can occur freely in input. The input data are correct.
Output
Note: An input/output sample is in the table below. Here there are two data sets. The first one contains 3 points specified by their x and y coordinates. The second point, for example, has the x coordinate 2, and the y coordinate 3. The result for each data set is the tour length, (6.47 for the first data set in the given example).
Sample Input
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2
Sample Output
6.47
7.89
题解:这个题代码虽然比较短,但是状态的定义真的是很秀,不知道下一次再遇到类似的题自己能不能想到这样定义状态。首先,从左到右再从右到左一般都是化为两个从左到右,dp[i][j]为第一个人到i,第二个人到j,并且标号<=min(i,j)都已经走过,很显然dp[i][j] == dp[j][i],因此不妨规定i > j,因此dp[i][j]只能从dp[i+1][i],或dp[i+1][j]转移过来,记忆化搜索就好。
#include <bits/stdc++.h>
using namespace std;
const int maxn = + ;
int n;
struct Point {
int x, y;
}point[maxn];
double dist[maxn][maxn];
double dp[maxn][maxn];
double DP(int i, int j) {
double& ans = dp[i][j];
if (ans > ) return ans;
ans = 0.0;
if (i == n - ) {
ans = dist[n - ][n] + dist[j][n];
}
else {
ans = min(dist[i][i + ] + DP(i + , j), dist[j][i + ] + DP(i + , i));
}
return ans;
}
int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d", &n) && n) {
for (int i = ; i <= n; i++) {
scanf("%d%d", &point[i].x, &point[i].y);
}
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++) {
dp[i][j] = -1.0;
dist[j][i] = dist[i][j] = sqrt(1.0*(point[i].x - point[j].x)*(point[i].x - point[j].x) + 1.0*(point[i].y - point[j].y)*(point[i].y - point[j].y));
}
}
printf("%.2f\n", DP(, ));
}
return ;
}
UVA1347-Tour(动态规划基础)的更多相关文章
- UVA-1347 Tour 动态规划 难以确定的状态
题目链接:https://cn.vjudge.net/problem/UVA-1347 题意 给出按x坐标排序的几个点. 欲从最左边不回头的走到最右边,然后再返回最左边. 每个点都要被访问,且只能经过 ...
- nyist oj 79 拦截导弹 (动态规划基础题)
拦截导弹 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...
- Problem C: 动态规划基础题目之数字三角形
Problem C: 动态规划基础题目之数字三角形 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 208 Solved: 139[Submit][Sta ...
- F - Free DIY Tour(动态规划,搜索也行)
这道题可用动态规划也可以用搜索,下面都写一下 Description Weiwei is a software engineer of ShiningSoft. He has just excelle ...
- Chapter_9 DP : uva1347 tour (bitonic tour)
https://cn.vjudge.net/problem/UVA-1347 这道题居然可以O(n^2)解决, 让我太吃惊了!!! 鄙人见识浅薄, 这其实是一个经典问题: bitonic tour. ...
- Codeforces Flipping game 动态规划基础
题目链接:http://codeforces.com/problemset/problem/327/A 这道题目有O(N^3)的做法,这里转化为动态规划求解,复杂度是O(N) #include < ...
- UVA437-The Tower of Babylon(动态规划基础)
Problem UVA437-The Tower of Babylon Accept: 3648 Submit: 12532Time Limit: 3000 mSec Problem Descrip ...
- 《挑战程序设计竞赛》2.3 动态规划-基础 POJ3176 2229 2385 3616 3280
POJ3176 Cow Bowling 题意 输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个 ...
- UVa1347 Tour
/*----UVa1347 ---首相两边方向走不方便,可以看做:两个人同时从最左边出发,沿着两条不同路径走到终点,除了起点和中点外 其他点恰好被走过一遍 ---用dp[i][j]表示1-max(i, ...
随机推荐
- 【Spring】DispatcherServlet的启动和初始化
使用过SpringMVC的都知道DispatcherServlet,下面介绍下该Servlet的启动与初始化.作为Servlet,DispatcherServlet的启动与Serlvet的启动过程是相 ...
- JSJ——主数据类型和引用
变量有两种:primitive主数据类型和引用. Java注重类型.它不会让你做出把长颈鹿类型变量装进兔子类型变量中这种诡异又危险的举动——如果有人对长颈鹿调用“跳跃”这个方法会发生什么悲剧?并且它也 ...
- 【Redis】2、CentOS 7 上安装 redis3.2.3安装与配置
一.redis源码安装 [更正]现在最新稳定的版本已经到了3.2.8 截至到2016.8.11,redis最新稳定版本为3.2.3.本篇文章我们就以此版本为基础,进行相关的讲解. 下载redis源码, ...
- netty入门demo(一)
目录 前言 正文 代码部分 服务端 客服端 测试结果一: 解决粘包,拆包的问题 总结 前言 最近做一个项目: 大概需求: 多个温度传感器不断向java服务发送温度数据,该传感器采用socket发送数据 ...
- loj#6032. 「雅礼集训 2017 Day2」水箱(并查集 贪心 扫描线)
题意 链接 Sol 神仙题+神仙做法%%%%%%%% 我再来复述一遍.. 首先按照\(y\)坐标排序,然后维护一个扫描线从低处往高处考虑. 一个连通块的内状态使用两个变量即可维护\(ans\)表示联通 ...
- react 会员登录
会员登录在我们的好多项目中都有用到,比如在后台管理系统,它的第一步就需要你进行登录,还有在我们常见的京东.淘宝.网易云音乐等一系列的软件上面都需要进行登录. 下面我们直接上代码 fetch(url,{ ...
- 全方位理解Android权限之底层实现概览
0000 这个阶段搞了很多和Android文件权限相关的问题,虽然一知半解,但也算是对Android权限机制有一些自己的理解.遂将这些内容整理出来.因为权限这部分涉及到的内容很多,故将知识分为几块内容 ...
- Windows Zip/CentOS/Radhat系统安装Mysql5.7.x方法
CentOS/Redhat 安装: wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm rpm -Uvh ...
- turnserver 配置说明记录
coTurn工程提供了较完整的STUN和TURN服务,记录其主要的命令行参数配置说明 针对TURN/STUN服务进程turnserver.exe的使用参数做简单说明 -L 监听的IP地址 -p 监听端 ...
- coTurn 使用测试方法
做个记录 1.从"../examples/etc/" 目录拷贝turnserver.conf文件到"/usr/local/etc/"目录 2.修改配置文件 主要 ...