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

    http://acm.hdu.edu.cn/showproblem.php?pid=2224

    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.
     

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    int N;
    double dist[210][210];
    double dp[210][210];
    
    struct Node {
        int x;
        int y;
    }node[210];
    
    double dis(int a, int b) {
        return sqrt((double)(node[a].x - node[b].x) * (node[a].x - node[b].x) + (node[a].y - node[b].y) * (node[a].y - node[b].y));
    }
    
    int main() {
        while(~scanf("%d", &N)) {
            for(int i = 1; i <= N; i ++) {
                scanf("%d%d", &node[i].x, &node[i].y);
                for(int j = 1; j < i; j ++)
                    dist[i][j] = dist[j][i] = dis(i, j);
            }
    
            memset(dp, 99999999, sizeof(dp));
    
            dp[1][2] = dp[2][1] = dist[1][2];
            for(int j = 3; j <= N; j ++) {
                dp[1][j] = dp[j][1] = dp[1][j - 1] + dist[j - 1][j];
            }
    
            for(int i = 2; i <= N; i ++) {
                double ans = 99999999;
                for(int k = 1; k < i; k ++)
                    ans = min(ans, dp[i][k] + dist[k][i + 1]);
    
                dp[i][i + 1] = dp[i + 1][i] = ans;
                for(int j = i + 2; j <= N; j ++)
                    dp[i][j] = dp[j][i] = dp[i][j - 1] + dist[j - 1][j];
            }
            dp[N][N] = dp[N][N - 1] + dist[N - 1][N];
            printf("%.2lf
    ", dp[N][N]);
        }
    
        return 0;
    }
    

      

  • 相关阅读:
    SQL判断如果一列值为null则取另一列值代替 isnull()
    关于js的function.来自百度知道的回答,学习了.
    OSI七层与TCP/IP五层网络架构
    504 Gateway Time-out
    nginx中关于并发数的问题worker_connections,worker_processes
    php实现二叉树的遍历
    nginx负载均衡的简单实现
    linux shell数据重定向
    数据库范式的思考以及数据库的设计
    msyql中myism和innodb的区别
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9873122.html
Copyright © 2011-2022 走看看