zoukankan      html  css  js  c++  java
  • UVa 1347 Tour

    Time Limit: 3000MS   Memory Limit: Unknown   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 must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane pi = < xiyi ><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

    可以把一个人的往返看作是两个人从起点同时出发走到终点,两人走到的点不重复

    则对于一个点,要么一个人过去,要么另一个人过去(不需要区分到底是哪个人)

     1 /*by SilverN*/
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 using namespace std;
     8 const int mxn=1024;
     9 int x[mxn],y[mxn];
    10 double mp[mxn][mxn];
    11 double f[mxn][mxn];
    12 int n;
    13 int main(){
    14     while(scanf("%d",&n)!=EOF){
    15         memset(f,11,sizeof(f));
    16         int i,j;
    17         for(i=1;i<=n;i++){
    18             scanf("%d%d",&x[i],&y[i]);
    19         }
    20         for(i=1;i<=n;i++){
    21             for(j=1;j<=n;j++){
    22                 mp[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
    23             }
    24         }
    25         for(i=n-1;i>=2;i--){//i倒推 
    26             for(j=1;j<i;j++){
    27                 if(i==n-1)f[i][j]=mp[i][n]+mp[j][n];//边界 
    28                 else f[i][j]=min(mp[i][i+1]+f[i+1][j],mp[j][i+1]+f[i+1][i]);
    29             }
    30         }
    31         printf("%.2f
    ",mp[1][2]+f[2][1]);
    32         //最终状态是一个人走到了点2,一个人走到了点1,需要再加上从2到1的距离 
    33     }
    34     return 0;
    35 }
  • 相关阅读:
    HDU 1213 How Many Tables(并查集,简单)
    POJ 1611 The Suspects(并查集,简单)
    HDU 4539 郑厂长系列故事――排兵布阵(曼哈顿距离)
    POJ 2411 Mondriaan'sDream(状压DP)
    ZOJ 4257 MostPowerful(状压DP,简单)
    HDU 3001 Traveling(状压DP)
    POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
    POJ 1185 炮兵阵地(状态压缩DP)
    POJ 3254 Corn Fields(状态压缩DP)
    XueXX and Chessboard(dp)
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5589518.html
Copyright © 2011-2022 走看看