UVA-1347 Tour

Description

Download as PDF
 

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

Write a program that, given a set of n<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> and y<tex2html_verbatim_mark> coordinates. The second point, for example, has the x<tex2html_verbatim_mark> coordinate 2, and the y<tex2html_verbatim_mark> 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

题目大意:在二维直角坐标系中,有一系列横坐标互不相同的点。现在,要从最左边的点开始严格向右连线,到达最右边的点时再严格向左连线,要求在此两个过程中除最左边点之外的任何点都总共被经过一次,求总的最短的轨迹长度
题目解析:这是《入门经典》上的一道题。它的思路是这样的:将向左连线的过程也视为向右连线,那么最后的两条轨迹一定交在最右边的点上。定义状态dp(i,j)表示在1~max(i,j)中的所有点都已经过,并且目前停留在i点和j点时的轨迹总长度,最终答案为dp(n,n)。显然有dp(i,j)=dp(j,i),不妨令i>j。因为状态定义时就已经决定了1~max(i,j)中的所有点都要被经过,所以状态就只能转移到dp(i+1,i)和dp(i+1,j),显然,这样转移并不会产生遗漏。状态转移方程是这样的:dp(i+1,i)=min(dp(i+1,i),dp(i,j)+dist(i+1,j)),dp(i+1,j)=min(dp(i+1,j),dp(i,j)+dist(i+1,i))。注意处理边界。

代码如下:
# include<iostream>
# include<cstdio>
# include<cmath>
# include<cstring>
# include<algorithm>
using namespace std;
const double INF=1e9;
struct Position
{
    int x,y;
};
double dist(const Position &a,const Position &b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
Position p[1005];
double dp[1005][1005];
void solve(int n)
{
    for(int i=0;i<n;++i)
        for(int j=i;j<n;++j)
            dp[i][j]=dp[j][i]=INF;
    dp[0][0]=0.0;
    dp[1][0]=min(dp[1][0],dp[0][0]+dist(p[0],p[1]));
    dp[0][1]=dp[1][0];
    for(int i=0;i<n-1;++i){
        for(int j=0;j<i;++j){
            dp[i+1][j]=min(dp[i+1][j],dp[i][j]+dist(p[i],p[i+1]));
            dp[j][i+1]=dp[i+1][j];
            dp[i+1][i]=min(dp[i+1][i],dp[i][j]+dist(p[j],p[i+1]));
            dp[i][i+1]=dp[i+1][i];
        }
    }
    for(int i=0;i<n-1;++i)
        dp[n-1][n-1]=min(dp[n-1][n-1],dp[n-1][i]+dist(p[i],p[n-1]));
    printf("%.2lf\n",dp[n-1][n-1]);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;++i)
            scanf("%d%d",&p[i].x,&p[i].y);
        solve(n);
    }
    return 0;
}

  

posted @ 2015-08-26 10:35  20143605  阅读(361)  评论(0编辑  收藏  举报