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】的更多相关文章

  1. UVA 1347 Tour 双调TSP

    TSP是NP难,但是把问题简化,到最右点之前的巡游路线只能严格向右,到最右边的点以后,返回的时候严格向左,这个问题就可以在多项式时间内求出来了. 定义状态d[i][j]表示一个人在i号点,令一个人在j ...

  2. ACM - 动态规划 - UVA 1347 Tour

    UVA 1347 Tour 题解 题目大意:有 \(n\) 个点,给出点的 \(x\).\(y\) 坐标.找出一条经过所有点一次的回路,从最左边的点出发,严格向右走,到达最右点再严格向左,回到最左点. ...

  3. hdu 4281 Judges' response(多旅行商&DP)

    Judges' response Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. UVA - 1347 Tour(DP + 双调旅行商问题)

    题意:给出按照x坐标排序的n个点,让我们求出从最左端点到最右短点然后再回来,并且经过所有点且只经过一次的最短路径. 分析:这个题目刘汝佳的算法书上也有详解(就在基础dp那一段),具体思路如下:按照题目 ...

  5. UVA 1347"Tour"(经典DP)

    传送门 参考资料: [1]:紫书 题意: 欧几里得距离???? 题解: AC代码: #include<bits/stdc++.h> using namespace std; ; int n ...

  6. UVa 1347 Tour

    Tour Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description   Joh ...

  7. 洛谷P1523 旅行商简化版(DP)

    题目: P1523 旅行商简化版 解析 可以看做是两个人同时从西往东走,经过不一样的点,走到最东头的方案数 设\(f[i][j]\)表示一个人走到i,一个人走到j的最短距离(\(i<j\)) 第 ...

  8. P1523 旅行商简化版

    P1523 旅行商简化版 题目背景 欧几里德旅行商(Euclidean Traveling Salesman)问题也就是货郎担问题一直是困扰全世界数学家.计算机学家的著名问题.现有的算法都没有办法在确 ...

  9. vijosP1014 旅行商简化版

    vijosP1014 旅行商简化版 链接:https://vijos.org/p/1014 [思路] 双线DP. 设ab,ab同时走.用d[i][j]表示ab所处结点i.j,且定义i>j,则有转 ...

随机推荐

  1. PJSIP-PJLIB(samples) (the usage of the pjlib lib) (eg:string/I/O)

    Here are some samples about  PJLIB! PJLIB is the basic lib of PJSIP, so we need master the lib first ...

  2. 剑指Offer - 九度1385 - 重建二叉树

    剑指Offer - 九度1385 - 重建二叉树2013-11-23 23:53 题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的 ...

  3. 《Cracking the Coding Interview》——第9章:递归和动态规划——题目9

    2014-03-20 04:08 题目:八皇后问题. 解法:DFS解决. 代码: // 9.9 Eight-Queen Problem, need I say more? #include <c ...

  4. Nodejs的那些事

    前言: Node.js实际上是算是个前端开发,但是我们要做APP自动化涉及到 node.js ,也需要它来帮我们安装一些 packing 一.Node.js安装 1.安装Node.js:立即下载 2. ...

  5. 转: jsp之c标签

    http://www.gbsou.com/2009/10/12/1028.htmljsp标签之c标签 核心标签库 它是JSTL中的核心库,为日常任务提供通用支持,如显示和设置变量.重复使用一组项目.测 ...

  6. Ext JS 的一个非常好的学习网站

    起飞网 http://www.qeefee.com/zt-extjs 发现一个非常好的学习ExtJS的中文网站

  7. 第二阶段团队冲刺-three

    昨天: 修复博客作业查询功能. 今天: 绘制logo. 遇到的问题: 无.

  8. restorator 运行后其他所有EXE文件都无法运行的解决方案

    昨天要反编译一个EXE,用RESTORATOR来查看资源罗列情况,倒霉的事情发生了,所有EXE文件点右键后‘打开’都没有了,刚开始以为中度了,进安全模式看,发现文件都没有异常,并且在安全模式下问题照样 ...

  9. Android程序猿必须警示的13个坑

        Android开发中,犯错是难免的,不犯错是不正常的,但是犯了错以后,我们必须时刻谨记这些坑,避免再次被坑,下面小编整理了13个,日常工作中,比较常见且易犯的错误,分享给大家.    1.类的 ...

  10. LACP学习笔记

    LACP学习笔记 来源: https://blog.csdn.net/zhengmx100/article/details/53893902 参考文档:download.h3c.com.cn/down ...