1. 程式人生 > 實用技巧 >旅遊 雙調歐幾里得旅行商演算法

旅遊 雙調歐幾里得旅行商演算法

PDF連結:點我

vj題目連結:點我

題目:

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 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個點,你需要把它們連成一個環,且每一個點只能經過一次,問你這個環的長度最小是多少。(輸入資料保證不存在兩個點的x值相同

題解:

我們我們首先看一下什麼是旅行商(Bitonic)問題:

平面上n個點,確定一條連線各點的最短閉合旅程。這個解的一般形式為NP的(在多項式時間內可以求出)

演算法複雜度為O(n^2)

我們首先定義一下變數:

路徑Pi,j(i<=j),對於路徑Pi,j來說,它包含的點為p1,p2,..pj。其中pi點為起始點,pi,j表示從pi點開始走,一直向左走到p1然後從p1點沿不同的路徑向右走直到pj

陣列d[i,j]表示pi,j路徑上的最小Bitonic路線。很容易看出,題目所求的即是d[n,n]。

dist(i,j)表示i點和j點的距離(兩點之間距離

求解過程:

1、對所有點按照x橫座標的值從小到大排序(所以這個演算法的前提就是不能存在x值相同的兩個點

2、首先d[1,1]無意義,d[1,2]就是p1,p2兩點的距離dist(1,2)

3、對於路徑Pi,j來說,如果pi不是pj的相鄰點(相鄰:i=j-1),d[i,j]=d[i,j-1]+dist(j-1,j)

由定義可知,點Pj-1一定在路徑Pj-Pi上,而且又由於i<j-1,因此Pj的左邊的相鄰點一定是Pi-1.因此可以得出上述等式。

4、當i=j-1時,對於pj點來說,有一側必與pi點相連,此時要判斷pj點的另一側與哪個點相連。根據最優解的結構,此時,定義整數k(1<=k<j-1),計算每個d[k,j-1]+dist(k,j)的值,選擇其中最小的一個作為d[i,j]的值,並且記錄k值,將pj與pk相連。

d(i,j) = min{d(k,i) + dist(j,k)},

5、最後

題目的最優解為d[n,n],此時,分析當pn點未連通的情況。因為點是排好序的,而且根據題意中的嚴格方向的要求,可以知道pn點必有一側是跟pn-1點相連的,那麼此時d[n-1,n]應該為pn-1,n的最優解。採用反證法,如果存在d’[n-1,n]<d[n-1,n],那麼用d’[n-1,n]來替換d[n-1,n]就可以得到一個更小的d[n,n],與已知d[n,n]為最優解矛盾,因此假設不成立。

所以,最後答案:dp[n][n]=dp[n-1][n]+dist(n-1,n)

本題就是例題:

程式碼:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 1000 + 10;
const long long ll_INF=0x3f3f3f3f3f3f3f3fLL;
const int inf=200000000;
const int INF=0x3f3f3f3f;
double dp[maxn][maxn];
struct Point
{
    double x,y;
}point[maxn];
bool cmp(Point x,Point y)
{
    return x.x<y.x;
}
double dist(int i,int j)
{
    return sqrt((point[i].x-point[j].x)*(point[i].x-point[j].x)+(point[i].y-point[j].y)*(point[i].y-point[j].y));
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;++i)
        {
            scanf("%lf%lf",&point[i].x,&point[i].y);
        }
        sort(point+1,point+n+1,cmp);
        dp[1][2]=dist(1,2);
        for(int i=3;i<=n;++i)
        {
            for(int j=1;j<=i-2;++j)
            {
                dp[j][i]=dp[j][i-1]+dist(i,i-1);
            }
            dp[i-1][i]=INF;
            for(int k=1;k<=i-2;++k)
            {
                double temp=dist(k,i)+dp[k][i-1];
                if(temp<dp[i-1][i])
                {
                    dp[i-1][i]=temp;
                }
            }
        }
        dp[n][n]=dp[n-1][n]+dist(n-1,n);
        printf("%.2lf\n",dp[n][n]);
    }
    return 0;
}