zoukankan      html  css  js  c++  java
  • ZOJ2581Tour

    题目

    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 = . 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 oints according to John's strategy.

    Input

    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. 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

    思路分析

    刷表法--考虑每个状态影响到的状态
    填表法--考虑每个状态的依赖
    状态方向:has/remain

    法1:刷表法

    d(i,j)=min(d(i+1,j)+dist(i,i+1),d(i+1,i)+dist(j,i+1));

    #include<bits/stdc++.h>
    using namespace std;
    float d(int xi,int yi,int xj,int yj){
        return sqrt((xi-xj)*(xi-xj)+(yi-yj)*(yi-yj));
    }
    int main(){
        int n,i,j;
        while(~scanf("%d",&n)){
            vector<int> x(n),y(n);
            vector<vector<float>> dp(n,vector<float>(n,0.0));
            for(i=0;i<n;++i)scanf("%d%d",&x[i],&y[i]);
            for(i=n-2;i>=0;--i)dp[n-1][i]=d(x[i],y[i],x[n-1],y[n-1]);
            for(i=n-2;i>=0;--i)
            for(j=0;j<=(i==0?i:i-1);++j)
            dp[i][j]=min(dp[i+1][j]+d(x[i],y[i],x[i+1],y[i+1]),dp[i+1][i]+d(x[j],y[j],x[i+1],y[i+1]));
            printf("%.2f
    ",dp[0][0]);
        }
        return 0;
    }
    

    法2:填表法

    当j < i-1时 d[i][j] = d[i-1][j] + dist[i][i-1];
    当j = i-1时 d[i][j] = min(d[i][j],d[i-1][k] + dist[i][k]) (k:0->i-2);

    代码

    #include<bits/stdc++.h>
    using namespace std;
    float d(int xi,int yi,int xj,int yj){
        return sqrt((xi-xj)*(xi-xj)+(yi-yj)*(yi-yj));
    }
    int main(){
        int n,i,j;
        while(~scanf("%d",&n)){
            vector<int> x(n),y(n);
            vector<vector<float>> dp(n,vector<float>(n,0.0));
            for(i=0;i<n;++i)scanf("%d%d",&x[i],&y[i]);
            dp[1][0]=d(x[1],y[1],x[0],y[0]);
            for(i=2;i<n;++i){
                for(j=0;j<i-1;++j)dp[i][j]=dp[i-1][j]+d(x[i-1],y[i-1],x[i],y[i]);
                dp[i][j]=dp[i-1][0]+d(x[0],y[0],x[i],y[i]);
                for(int k=1;k<i-1;++k)
                dp[i][j]=min(dp[i][j],dp[i-1][k]+d(x[k],y[k],x[i],y[i]));
            }
            printf("%.2f
    ",dp[n-1][n-2]+d(x[n-2],y[n-2],x[n-1],y[n-1]));
        }
        return 0;
    }
    
  • 相关阅读:
    URL记录
    Mongodb集群节点故障恢复场景分析(转)
    IO 和 NIO 的区别
    VUE 前端项目优化方法
    缓存的穿透和雪崩
    接口如何处理重复请求?
    线程池构造类 ThreadPoolExecutor 的 5 个参数
    大型网站在架构上应当考虑哪些问题
    synchronized 和 lock 的区别
    JVM虚拟机 YGC和FGC发生的具体场景
  • 原文地址:https://www.cnblogs.com/chanceYu/p/12059832.html
Copyright © 2011-2022 走看看