# Tour UVA - 1347

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

``` 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 using namespace std;
6 int n;
7 struct node
8 {
9     double x,y;
10 }a;
11 double dis;
12 double dp;
13 int main()
14 {
15     while(scanf("%d",&n)==1)
16     {
17         for(int i=1;i<=n;i++)
18         {
19             scanf("%lf%lf",&a[i].x,&a[i].y);
20             for(int j=i-1;j>=1;j--)
21             dis[j][i]=sqrt(((a[i].x-a[j].x)*(a[i].x-a[j].x))+((a[i].y-a[j].y)*(a[i].y-a[j].y)));
22         }
23         //pre();
24         for(int i=n-2;i>=1;i--)
25             dp[n-1][i]=dis[n-1][n]+dis[i][n];
26         for(int i=n-2;i>=2;i--)
27             for(int j=i-1;j>=1;j--)
28             dp[i][j]=min(dp[i+1][j]+dis[i][i+1],dp[i+1][i]+dis[j][i+1]);
29         printf("%.2lf\n",dp+dis);
30     }
31     return 0;
32 }```

1811 篇文章121 人订阅

0 条评论

## 相关文章

342100

### HOJ 2985 Wavio Sequence（最长递增子序列以及其O(n*logn)算法）

Wavio Sequence My Tags (Edit) Source : UVA Time limit : 1 sec Memor...

29460

### POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submission...

36740

### HDU 1412 {A} + {B}

{A} + {B} Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K...

380150

### HDUOJ---1195Open the Lock

Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276...

28760

### PAT 甲级 1104 sum of Number Segments

1104. Sum of Number Segments (20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16...

28650

362120

### UESTC 482 Charitable Exchange(优先队列+bfs)

Charitable Exchange Time Limit: 4000/2000MS (Java/Others)     Memory Limit: 65...

34350

27860

### HDUOJ---1867 A + B for you again

A + B for you again Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 3276...

36060 