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

这题就是DP,思路什么的书上说的很清楚了

 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[10001];
11 double dis[1001][1001];
12 double dp[1001][1001];
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[2][1]+dis[1][2]);
30     }
31     return 0;
32 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • HDU 1506 Largest Rectangle in a Histogram(单调栈)

    attack
  • POJ 3308 Paratroopers

    Description It is year 2500 A.D. and there is a terrible war between the forces...

    attack
  • P3507 [POI2010]GRA-The Minima Game

    题目描述 Alice and Bob learned the minima game, which they like very much, recently....

    attack
  • 如何在SAP C4C AdvancedListPane上批量执行若干BO实例的action

    Recently one partner asked me about this question. Suppose I have a custom BO wi...

    Jerry Wang
  • leetcode: 66. Plus One

    JNingWei
  • 语境很重要:手语识别的自我注意(cs,AI)

    本文为连续手语识别任务提出了一注意力网络。拟议的方法利用相互依赖的数据流来建模手语模式。这些不同的信息渠道可以共享彼此复杂的时间结构。因此,我们关注同步并帮助捕...

    天然 8129060
  • B. Minimize the Permutation (贪心)

    You are given a permutation of length n. Recall that the permutation is an array...

    dejavu1zz
  • SAP CRM IBASE的archive方法

    There is one IBASE 112467 which contains one object component. This IBASE is int...

    Jerry Wang
  • 多目标进化算法应用于提高医药数据领域学习器的性能(CS AI)

    原文标题完整翻译:多目标进化算法应用于提高在医药数据领域使用整体特征选择和离散化模型的学习器的性能

    Donuts_choco
  • 聊聊flink LocalEnvironment的execute方法

    flink-java-1.6.2-sources.jar!/org/apache/flink/api/java/DataSet.java

    codecraft

扫码关注云+社区

领取腾讯云代金券