zoukankan      html  css  js  c++  java
  • Uva1347 Tour

    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

    https://odzkskevi.qnssl.com/f85205c68ae5f131a579c799f71b7b69?v=1507888848

    【题解】

    写了一种网上的解法,比紫书上的状态更容易想到。

    dp[i][j]表示1->i,j->1的最短路,且保证i > j(因为dp[i][j] = dp[j][i])
    dp[i][j] = dp[i - 1][j] + distant(i - 1, i) 这里要保证i - 1 > i,我们
    还需单独做dp[i][i - 1]

    dp[i][i - 1] = min(dp[i - 1][j] + dist(i - 1, i));'

    玄学DP,bestdp

     1 #include<iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <algorithm>
     6 #include <cmath>
     7 #define max(a, b) ((a) > (b) ? (a) : (b))
     8 #define min(a, b) ((a) < (b) ? (a) : (b))
     9 #define abs(a) (((a) < 0) ? (-1 * (a)) : (a))
    10 inline void swap(int &a, int &b)
    11 {
    12     int tmp = a;a = b;b = tmp;
    13 }
    14 inline void read(int &x)
    15 {
    16     x = 0;char ch = getchar(), c = ch;
    17     while(ch < '0' || ch > '9')c = ch, ch = getchar();
    18     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
    19     if(c == '-')x = -x; 
    20 }
    21 
    22 const double INF = 0x3f3f3f3f;
    23 const int MAXN = 4000 + 10;
    24 
    25 double dp[MAXN][MAXN];
    26 int x[MAXN], y[MAXN],n;
    27 
    28 inline double dist(int a, int b)
    29 {
    30     return sqrt(abs(x[a] - x[b]) * abs(x[a] - x[b]) + abs(y[a] - y[b]) * abs(y[a] - y[b]));
    31 }
    32 
    33 int main()
    34 {
    35     while(scanf("%d", &n) != EOF)
    36     {
    37         for(register int i = 1;i <= n;++ i) read(x[i]), read(y[i]);
    38         memset(dp, 0x3f, sizeof(dp));
    39         dp[2][1] = dist(2, 1);
    40         for(register int i = 3;i <= n;++ i)
    41         {
    42             dp[i][i - 1] = INF;
    43             for(register int j = 1;j < i - 1;++ j)
    44             {
    45                 dp[i][i - 1] = min(dp[i][i - 1], dp[i - 1][j] + dist(i, j));
    46                 dp[i][j] = dp[i - 1][j] + dist(i - 1, i); 
    47             }
    48         }
    49         double ans = INF;
    50         for(register int i = 1;i < n;++ i)
    51             ans = min(ans, dp[n][i] + dist(n, i));
    52         printf("%.2lf
    ", ans);
    53     }
    54     return 0;
    55 } 
    Uva1347



  • 相关阅读:
    Go语言通道(chan)——goroutine之间通信的管道
    GO语言数组,切片,MAP总结
    GO数组
    GO切片
    GO语言测试
    GO语言html模板
    Go语言中defer语句使用小结
    微信小程序 某个页面直接返回首页
    小程序常用变量
    bzoj1030
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7679785.html
Copyright © 2011-2022 走看看