UVa 1347 Tour

Source: Internet
Author: User

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, which connects his destinations. Each destination was represented by a point in the plane pi = < xi, yi ><tex2html_v Erbatim_mark>. John uses the following strategy:he starts from the leftmost point and then he goes strictly Point, and then he goes strictly right back to the starting point. It is known that the points has distinct x<tex2html_verbatim_mark>-coordinates.

Write A program this, given a set of n<tex2html_verbatim_mark> points in the plane, computes the Shorte St Closed Tour This connects the points according to John ' s strategy.

Input

The program input was 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 is 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 the fractional digits, represents the result.


Note: An Input/output sample are in the table below. Here there is the data sets. The first one contains 3 points specified by their x<tex2html_verbatim_mark> and y<tex 2html_verbatim_mark>coordinates. The second point, for example, have the x<tex2html_verbatim_mark> coordinate 2, and the y&l t;tex2html_verbatim_mark> coordinate 3. The result for each data set was the tour length, (6.47 for the first data set in the given example).

Sample Input

3 1 12 33 14 1 1 2 33 14 2

Sample Output

6.477.89

Can think of a person's round trip as two people from the beginning of the same time to go to the end, two people go to the point of not repeating

Then for a point, either one person is past, or the other person is past (no need to distinguish who is who)

1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7 using namespacestd;8 Const intmxn=1024x768;9 intX[MXN],Y[MXN];Ten DoubleMP[MXN][MXN]; One DoubleF[MXN][MXN]; A intN; - intMain () { -      while(SCANF ("%d", &n)! =EOF) { theMemset (F, One,sizeof(f)); -         inti,j; -          for(i=1; i<=n;i++){ -scanf"%d%d",&x[i],&y[i]); +         } -          for(i=1; i<=n;i++){ +              for(j=1; j<=n;j++){ AMp[i][j]=sqrt ((X[i]-x[j]) * (X[i]-x[j]) + (Y[i]-y[j]) * (y[i]-y[j])); at             } -         } -          for(i=n-1; i>=2; i--) {//I reverse push -              for(j=1; j<i;j++){ -                 if(i==n-1) F[i][j]=mp[i][n]+mp[j][n];//Border -                 ElseF[i][j]=min (mp[i][i+1]+f[i+1][j],mp[j][i+1]+f[i+1][i]); in             } -         } toprintf"%.2f\n", mp[1][2]+f[2][1]); +         //The final state is a person went to point 2, a person went to point 1, need to add a distance from 2 to 1 -     } the     return 0; *}

UVa 1347 Tour

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.