• 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]);
        }
    }
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    php静态调用非静态方法
    phalcon 框架3.0更新时报错
    centos7.5更换docker-ce镜像源
    腾讯云更换镜像源遇到的坑
    php cli模式下调试
    审查php.ini自动分析程序
    docker WARNING: IPv4 forwarding is disabled. Networking will not work.
    git常用命令,制作缩写命令
    学习GRPC(一) 简单实现
    mac与linux服务器之间使用ssh互通有无
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/7429766.html
Copyright © 2020-2023  润新知