POJ2677 Tour(DP+双调欧几里得旅行商问题)

Tour

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3929   Accepted: 1761

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

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. 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

3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2

Sample Output

6.47
7.89

参考博客:http://www.cnblogs.com/-sunshine/archive/2012/07/23/2605251.html

思路【转】:

欧几里得旅行商问题是对平面上给定的n个点确定一条连接各点的最短闭合旅程的问题。如图(a)给出了一个7个点问题的解。这个问题的一般形式是NP完全的,故其解需要多于多项式的时间。

J.L. Bentley 建议通过只考虑双调旅程(bitonic tour)来简化问题,这种旅程即为从最左点开始,严格地从左到右直至最右点,然后严格地从右到左直至出发点。下图(b)显示了同样的7个点的最短双调路线。在这种情况下,多项式的算法是可能的。事实上,存在确定的最优双调路线的O(n*n)时间的算法。

 图a            图b

注:在一个单位栅格上显示的平面上的七个点。 a)最短闭合路线,长度大约是24.89。这个路线不是双调的。b)相同点的集合上的最短双调闭合路线。长度大约是25.58。

这是一个算导上的思考题15-1。

首先将给出的点排序,关键字x,重新编号,从左至右1,2,3,…,n。

定义p[i][j],表示结点i到结点j之间的距离。

定义d[i][j],表示从i连到1,再从1连到j,(注意,i>j,且并没有相连。)

对于任意一个点i来说,有两种连接方法,一种是如图(a)所示,i与i-1相连,另一种呢是如图(b),i与i-1不相连。

根据双调旅程,我们知道结点n一定与n相连,那么,如果我们求的d[n][n-1],只需将其加上p[n-1][n]就是最短双调闭合路线。

根据上图,很容易写出方程式:

dp[i][j]=dp[i-1][j]+dist[i][i-1];    i这一点是i+1走到的

dp[i][i-1]=min(dp[i][i-1],dp[i-1][j]+dist[j][i]);   i这一点是从j走到的,找到j的最小值

自己的理解:刘汝佳书上的思路看懂了,但是老卡,抽空在想想他的思路,这个思路也好理解,在dp[i][j]这个位置,到下一个i+1,有两种情况要么是i走到的,要么是j走到的,如果是i走到的就对应第一个转移方程,如果是j走到的 dp[i][j] == dp[j][i],所以对应第二个方程

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX = 10000 + 10;
struct points
{
    double x,y;
};
points point[MAX];
double d[MAX][MAX];
int cmp(points a, points b)
{
    return (b.x - a.x > 0.00001);
}
double Min(double a, double b)
{
    if(a - b > 0.00001)
        return b;
    else
        return a;
}
double dist(int a, int b)
{
    return sqrt( (point[a].x - point[b].x) * (point[a].x - point[b].x) + (point[a].y - point[b].y) * (point[a].y - point[b].y));
}
int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        for(int i = 1; i <= n; i++)
            scanf("%lf%lf", &point[i].x, &point[i].y);
        sort(point + 1, point + n + 1, cmp);
        if(n == 1)
        {
            printf("0\n");
            continue;
        }
        d[1][1] = 0;
        for(int i = 1; i <= n; i++)
            d[i][1] = dist(i, 1);

        for(int i = 2; i < n; i++)
        {
            d[i + 1][i] = 10000000.0;
            for(int j = 1; j < i; j++)
            {
                d[i + 1][j] = d[i][j] + dist(i, i + 1);
                d[i + 1][i] = Min(d[i + 1][i], d[i][j] + dist(j,i + 1));
            }
        }
        printf("%.2lf\n", d[n][n - 1] + dist(n - 1, n));
    }
    return 0;
}
时间: 2024-08-02 02:45:37

POJ2677 Tour(DP+双调欧几里得旅行商问题)的相关文章

UVA 1347(POJ 2677)Tour(双调欧几里得旅行商问题)

Tour                 Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu 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 mus

[ACM] POJ 2677 Tour (动态规划,双调欧几里得旅行商问题)

Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3585   Accepted: 1597 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

UVA - 1347 Tour 双调欧几里得旅行商问题

题目大意:给出n个点,要求你从最左边那个点走到最右边那个点,每个点都要被遍历过,且每个点只能走一次,问形成的最短距离是多少 解题思路:用dp[i][j]表示第一个人走到了第i个点,第二个人走到了第j个点且已经遍历了1–max(i,j)的所有点的最短距离.因为dp[i][j] = dp[j][i]的,所以我们设i > j的 那么就有 当j < i-1 时,dp[i][j] = dp[i-1][j] + dis(i, i -1) 当j == i + 1时情况就比较特别了,这里将j用i-1代替 dp

2014年百度之星程序设计大赛 - 资格赛 1002 Disk Schedule(双调欧几里得旅行商问题)

Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景.磁盘有许多轨道,每个轨道有许多扇区,用于存储数据.当我们想在特定扇区来读取数据时,磁头需要跳转到特定的轨道.具体扇区进行读取操作.为了简单,我们假设磁头可以在某个轨道顺时针或逆时针匀速旋转,旋转一周的时间是360个单位时间.磁头也可以随意移动到某个轨道进行读取,每跳转到一个相邻轨道的时间为400个单位时间,跳转

hdu 4824 Disk Schedule(双调欧几里得旅行商问题)

题目链接:hdu 4824 Disk Schedule 题目大意:中文题. 解题思路:需要的时,很明显每到一层是要读取一次数据的,但是因为需要返回00,所以有些层的数据可以在返回的过程中读取会较优.于是转化成了双调欧几里得旅行商问题. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int N = 1

【算法学习】双调欧几里得旅行商问题(动态规划)(转)

双调欧几里得旅行商问题是一个经典动态规划问题.<算法导论(第二版)>思考题15-1和北京大学OJ2677都出现了这个题目. 旅行商问题描述:平面上n个点,确定一条连接各点的最短闭合旅程.这个解的一般形式为NP的(在多项式时间内可以求出) J.L. Bentley 建议通过只考虑双调旅程(bitonictour)来简化问题,这种旅程即为从最左点开始,严格地从左到右直至最右点,然后严格地从右到左直至出发点.下图(b)显示了 同样的7个点的最短双调路线.在这种情况下,多项式的算法是可能的.事实上,存

双调欧几里得旅行商问题

杭电ACM的一道题~~ Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景.磁盘有许多轨道,每个轨道有许多扇区,用于存储数据.当我们想在特定扇区来读取数据时,磁头需要跳转到特定的轨道.具体扇区进行读取操作.为了简单,我们假设磁头可以在某个轨道顺时针或逆时针匀速旋转,旋转一周的时间是360个单位时间.磁头也可以随意移动到某个轨道进行读取,每跳转到一个相邻轨道的时间

《算法导论》思考题15-1 双调欧几里得旅行商问题(动态规划)

欧几里得旅行商问题 是对平面上给定的n个点确定一条连接各点的最短闭合旅程的问题.图a给出了7个点问题的解,这个问题的一般形式是NP完全的,故其解需要多于多项式的时间. J.K.Bentley建议通过只考虑双调旅程来简化问题,这种旅程即为从最左点开始,严格从左到最右点,再严格地从最右点回到最左点.图b显示了同样的7个点的问题的最短双调路线,在这种情况下,多项式的时间的算法是有可能的. 描述一个确定最优双调路线的O(n^2)时间的算法.可以假设任何两点的x坐标都不相同. 解法: 读了很多遍这个题,也

HDU4824 双调欧几里得

Disk Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 240 Accepted Submission(s): 121 Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景. 磁盘有许多轨道