UVA 1347 Tour 【双调旅行商/DP】
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
For each set of data, your program should print the result to the standard output from the beginning
of a line. The tour length, a floating-point number with two fractional digits, represents the result.
Note: An input/output sample is in the table below. Here there are two data sets. The first one
contains points specified by their x and y coordinates. The second point, for example, has the x
coordinate , and the y coordinate . The result for each data set is the tour length, (6.47 for the first
data set in the given example).
Sample Input Sample Output
6.47
7.89
【分析】:
首先按横坐标递增给所有点排序。
定义状态dp[i][j]表示从点i向n走一条路L1,从点j向n走另一条路L2(如下图,两条路互不相交,并且L1在L2上面),L1 + L2的最小值。程序中用distance(i, j)表示点i到点j的距离。
如何计算dp[i, j]呢?
我们考虑k = max(i, j) + 1这个点,这个点肯定在L1或者L2上。
k在L1上时,k在L2上时,如图
dp[i][j]取这两者最小值即可。
可能还是有点抽象,举个实际的例子吧。
假如i = 5, j = 4。在计算dp[5][4]的时候,考虑6这个点。6只有两种选择,要么在L1上(上面的路),这时候的代价为dp[6][4] + distance(5, 6)。要么在L2上(下面的路),这时候的代价为dp[5][6] + distance(4, 6)。
所以状态转移方程为:dp[i][j] = min(dp[i][k] + distance(j, k),dp[k][j] + distance(i, k))
一、临界情况
1. i = n: dp[i][j] = distance(j, n)
2. j = n: dp[i][j] = distance(i, n)
二、其余情况
k = max(i, j) + 1
dp[i][j] = min(dp[i][k] + distance(j, k), dp[k][j] + distance(i,k))
【讲解】:http://blog.sina.com.cn/s/blog_51cea4040100gkcq.html
【代码】:
#include <bits/stdc++.h>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> using namespace std;
const int N = ;
double dp[N][N];
struct node
{
double x, y;
bool operator < (const node& a)
{
return x < a.x; //按横坐标递增给所有点排序
}
}a[N];
//bool cmp(node a,node b)
//{
// return a.x > b.x;
//}
double dis(int i, int j)
{
return sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
}
int main()
{
int n;
while(cin >> n)
{
for(int i=; i<=n; i++) cin >> a[i].x >> a[i].y;
sort(a+, a+n+);
memset(dp,,sizeof(dp)); for(int i=n; i>=; i--){
for(int j=n; j>=; j--){
if(i==n && j==n) dp[i][j]=;
else if(j==n) dp[i][j]=dis(i,n);
else if(i==n) dp[i][j]=dis(j,n);
else{
int k=max(i,j)+;
dp[i][j]=min(dp[i][k]+dis(j,k), dp[k][j]+dis(i,k));
}
}
}
printf("%.2f\n", dp[][]);
}
}
UVA 1347 Tour 【双调旅行商/DP】的更多相关文章
- UVA 1347 Tour 双调TSP
TSP是NP难,但是把问题简化,到最右点之前的巡游路线只能严格向右,到最右边的点以后,返回的时候严格向左,这个问题就可以在多项式时间内求出来了. 定义状态d[i][j]表示一个人在i号点,令一个人在j ...
- ACM - 动态规划 - UVA 1347 Tour
UVA 1347 Tour 题解 题目大意:有 \(n\) 个点,给出点的 \(x\).\(y\) 坐标.找出一条经过所有点一次的回路,从最左边的点出发,严格向右走,到达最右点再严格向左,回到最左点. ...
- hdu 4281 Judges' response(多旅行商&DP)
Judges' response Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- UVA - 1347 Tour(DP + 双调旅行商问题)
题意:给出按照x坐标排序的n个点,让我们求出从最左端点到最右短点然后再回来,并且经过所有点且只经过一次的最短路径. 分析:这个题目刘汝佳的算法书上也有详解(就在基础dp那一段),具体思路如下:按照题目 ...
- UVA 1347"Tour"(经典DP)
传送门 参考资料: [1]:紫书 题意: 欧几里得距离???? 题解: AC代码: #include<bits/stdc++.h> using namespace std; ; int n ...
- UVa 1347 Tour
Tour Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Description Joh ...
- 洛谷P1523 旅行商简化版(DP)
题目: P1523 旅行商简化版 解析 可以看做是两个人同时从西往东走,经过不一样的点,走到最东头的方案数 设\(f[i][j]\)表示一个人走到i,一个人走到j的最短距离(\(i<j\)) 第 ...
- P1523 旅行商简化版
P1523 旅行商简化版 题目背景 欧几里德旅行商(Euclidean Traveling Salesman)问题也就是货郎担问题一直是困扰全世界数学家.计算机学家的著名问题.现有的算法都没有办法在确 ...
- vijosP1014 旅行商简化版
vijosP1014 旅行商简化版 链接:https://vijos.org/p/1014 [思路] 双线DP. 设ab,ab同时走.用d[i][j]表示ab所处结点i.j,且定义i>j,则有转 ...
随机推荐
- IOS客户端的个人中心可以查看自己的博客了。
IOS客户端的个人中心可以查看自己的博客了. 写这篇是为了在客户端显示之用. 下一步实现在客户端发博客.
- leetcode 【 Remove Duplicates from Sorted List 】 python 实现
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- WPF 利用键盘钩子来捕获键盘,做一些不为人知的事情...完整实例
键盘钩子是一种可以监控键盘操作的指令. 看到这句话是不是觉得其实键盘钩子可以做很多事情. 场景 当你的程序需要一个全局的快捷键时,可以考虑使用键盘钩子,如大家常用qq的截图快捷键,那么在WPF里怎么去 ...
- flask_入门教程之一
一.教程涉及开发语言.脚本.框架.数据库等内容 Python + Flask + requests 通过命令安装:pip install flask 二.创建第一个flask脚本 一个最小的 Flas ...
- 解决Navicat for MySQL 连接 Mysql 8.0.11 出现1251- Client does not support authentication protocol 错误
安装MySQL8.0之后,使用Navicat连接数据库,报1251错误. 上网搜索解决方案,网上说出现这种情况的原因是:mysql8 之前的版本中加密规则是mysql_native_password, ...
- 3D U-Net卷积神经网络
3D U-Net这篇论文的诞生主要是为了处理一些块状图(volumetric images),基本的原理跟U-Net其实并无大差,因为3D U-Net就是用3D卷积操作替换了2D的,不过在这篇博文中我 ...
- jQuery基础知识点(上)
jQuery是一个优秀的.轻量级的js库 ,它兼容CSS3,还兼容各种浏览器(IE 6.0+, FF1.5+, Safari 2.0+, Opera 9.0+),而jQuery2.0及后续版本将不再支 ...
- Python中的多线程编程,线程安全与锁(二)
在我的上篇博文Python中的多线程编程,线程安全与锁(一)中,我们熟悉了多线程编程与线程安全相关重要概念, Threading.Lock实现互斥锁的简单示例,两种死锁(迭代死锁和互相等待死锁)情况及 ...
- Password [分块]
题面 $n,m,x \leq 10^5$ 思路 首先$n=2$做法很多,不讲了 $n=3$的时候,分块维护两个东西:每一个数出现次数的前缀和,和出现次数的出现次数的前缀和(说的有点绕,但是应该挺好理解 ...
- SPOJ 1825 Free Tour | 终极之树分治
求树上最长路径使得经过的拥挤节点个数不超过K //欢迎访问这个博客!http://www.cnblogs.com/luyouqi233/p/8036828.html #include<cstdi ...