poj 2677 Tour(双调欧几里德旅行商问题,dp)

Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3277 Accepted: 1462

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
 
 
 
思路【转】:
  欧几里得旅行商问题是对平面上给定的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];

dp[i][i-1]=min(dp[i][i-1],dp[i-1][j]+dist[j][i]);

AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <map>
#include <algorithm>
#define ll __int64
#define L(rt) (rt<<1)
#define R(rt)  (rt<<1|1)

using namespace std;

const double INF = 1e9 + 7;
const int maxn = 205;

struct node
{
    double x, y;
} p[maxn];
int n;
double dp[maxn][maxn], dis[maxn][maxn];
bool cmp(node a, node b)
{
    return a.x < b.x;
}
double get_dis(node a, node b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main()
{
    while(~scanf("%d", &n))
    {
        for(int i = 0; i < n; i++)
            scanf("%lf%lf", &p[i].x, &p[i].y);
        //sort(p, p + n, cmp);
        for(int i = 0; i < n; i++)
        {
            dis[i][i] = 0;
            for(int j = i + 1; j < n; j++)
                dis[i][j] = dis[j][i] = get_dis(p[i], p[j]);
        }

        for(int i = 0; i < n; i++) dp[i][0] = dis[i][0];
        for(int i = 1; i < n - 1; i++)
        {
            dp[i + 1][i] = INF;
            for(int j = 0; j < i; j++)
            {
                dp[i + 1][j] = dp[i][j] + dis[i][i + 1];
                dp[i + 1][i] = min(dp[i + 1][i], dp[i][j] + dis[j][i + 1]);
            }
        }
        printf("%.2lf\n", dp[n - 1][n - 2] + dis[n - 1][n - 2]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值