zoukankan      html  css  js  c++  java
  • HDU 2224 The shortest path

    The shortest path

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1507    Accepted Submission(s): 773

    Problem Description
    There are n points on the plane, Pi(xi, yi)(1 <= i <= n), and xi < xj (i<j). You begin at P1 and visit all points then back to P1. But there is a constraint: 
    Before you reach the rightmost point Pn, you can only visit the points those have the bigger x-coordinate value. For example, you are at Pi now, then you can only visit Pj(j > i). When you reach Pn, the rule is changed, from now on you can only visit the points those have the smaller x-coordinate value than the point you are in now, for example, you are at Pi now, then you can only visit Pj(j < i). And in the end you back to P1 and the tour is over.
    You should visit all points in this tour and you can visit every point only once.
     
    Input
    The input consists of multiple test cases. Each case begins with a line containing a positive integer n(2 <= n <= 200), means the number of points. Then following n lines each containing two positive integers Pi(xi, yi), indicating the coordinate of the i-th point in the plane.
     
    Output
    For each test case, output one line containing the shortest path to visit all the points with the rule mentioned above.The answer should accurate up to 2 decimal places.
     
    Sample Input
    3 1 1 2 3 3 1
     
    Sample Output
    6.47 Hint: The way 1 - 3 - 2 - 1 makes the shortest path.
     
    Author
    8600
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1217 2807 2544 1142 1548 
    思路:双调欧几里得旅行商板子。
    #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]);
        }
    }
     
    细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
  • 相关阅读:
    数据结构3 特殊二叉树
    数据结构2 树与二叉树
    数据结构1 线性结构
    《数据库系统概念》20-恢复系统
    《数据库系统概念》19-并发控制
    数据库系统概念》18-事务
    巨杉内核笔记 | 会话(Session)
    巨杉Tech|SequoiaDB 巨杉数据库高可用容灾测试
    巨杉学习笔记 | SequoiaDB MySQL导入导出工具使用实战
    保险行业持续扩展,巨杉数据库再次中标人保财险
  • 原文地址:https://www.cnblogs.com/cangT-Tlan/p/7429780.html
Copyright © 2011-2022 走看看