zoukankan      html  css  js  c++  java
  • poj 2677 Tour

                                                                                     Tour
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4556   Accepted: 1993

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

    Source

    思路:双调欧几里得旅行商问题板子。
    #include<iostream>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n;
    double dis[550][550],f[550][550];
    struct nond{
        int x,y;
    }v[550];
    int cmp(nond a,nond b){
        if(a.x==b.x)    return a.y<b.y;
        return a.x<b.x;
    }
    void pre(){
        for(int i=1;i<=n;i++)
            for(int j=i;j<=n;j++)
                dis[i][j]=dis[j][i]=sqrt((double)(v[i].x-v[j].x)*(v[i].x-v[j].x)+(v[i].y-v[j].y)*(v[i].y-v[j].y));
    }
    int main(){
        while(scanf("%d",&n)!=EOF){
            memset(dis,0,sizeof(dis));
            for(int i=1;i<=n;i++)
                scanf("%d%d",&v[i].x,&v[i].y);
            sort(v+1,v+1+n,cmp);
            pre();
            f[1][2]=f[2][1]=dis[1][2];
            f[2][2]=2*dis[1][2];
            for(int i=3;i<=n;i++){
                for(int j=1;j<i-1;j++)
                    f[i][j]=f[j][i]=f[i-1][j]+dis[i][i-1];
                f[i][i-1]=f[i-1][i]=f[i][i]=0x7f7f7f7f;
                for(int j=1;j<=i-1;j++)
                    f[i-1][i]=f[i][i-1]=min(f[i][i-1],f[j][i-1]+dis[j][i]);
                for(int j=1;j<=i;j++)
                    f[i][i]=min(f[i][i],f[j][i]+dis[j][i]);
            }
            printf("%.2lf
    ",f[n][n]);
        }
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    ES6箭头函数中this的指向问题
    不借助vue-cli,自行构建一个vue项目
    Vue组件props选项-实现父子组件动态数据绑定
    Linux 基本操作命令
    Javascript
    ES6
    利用Gulp和Webpack进行项目自动化构建
    自定义View实现图片热区效果
    新年学习计划
    Activity跳转通过EventBus传值问题
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/7429766.html
Copyright © 2011-2022 走看看