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,则有转 ...
 
随机推荐
- groupSum6后向遍历
			
http://codingbat.com/prob/p199368 public boolean groupSum6(int start, int[] nums, int target) { if( ...
 - 《Cracking the Coding Interview》——第5章:位操作——题目2
			
2014-03-19 05:47 题目:给定一个double型浮点数,输出其二进制表示,如果不能在32个字符内完成输出,则输出“ERROR”. 解法:如果你熟悉IEEE754标准,应该知道double ...
 - WPF and Silverlight.ComboBox 如何通过 Binding  IsDropDownOpen 实现下拉菜单展开
			
In the WPF example the Popup and the ToggleButton (the arrow on the right) are bound with the proper ...
 - 解决使用Oracle数据库,项目启动由于表原因无法成功启动问题
			
1.仔细看异常信息,如果出现一个 翻译过来是 不仅仅这一张表,那就说明,在连接数据库,定位到表的时候有多张表,不知道连哪一张. 原因: 有多个用户,这两个用户下有相同的表. 就算是在不同的表空间也不 ...
 - markdown备忘
			
文章主要(99%)参考自:markdown基本语法 这是二级标题 这是加粗的文字 这是倾斜的文字` 这是引用的内容 这是引用的内容 以下是分割线: 超链接 列表内容1 列表内容2 列表内容3 1.列表 ...
 - Python全栈工程师(列表、拷贝)
			
ParisGabriel 感谢 大家的支持 你们的阅读评价就是我最好的更新动力 我会坚持吧排版做的越来越好 每天坚持 一天一篇 点个订阅吧 灰常感谢 当个死粉也阔以 Py ...
 - (原)Unreal 渲染模块 渲染流程
			
@author:白袍小道 浏览分享随缘,评论不喷亦可. 扯淡部分: 在temp中,乱七八糟的说了下大致的UE过程.下面我们还是稍微别那么任性,一步步来吧. UE渲染模块牵扯到场景遍历. ...
 - springboot集成shiro——使用RequiresPermissions注解无效
			
在Springboot环境中继承Shiro时,使用注解@RequiresPermissions时无效 @RequestMapping("add") @RequiresPermiss ...
 - Charts & canvas & RGBA
			
Charts & canvas RGBA color let stopFlag = 0; // show Charts const showCharts = (name = "&qu ...
 - 【bzoj3438】小M的作物  网络流最小割
			
原文地址:http://www.cnblogs.com/GXZlegend/p/6801522.html 题目描述 小M在MC里开辟了两块巨大的耕地A和B(你可以认为容量是无穷),现在,小P有n中作物 ...