zoukankan      html  css  js  c++  java
  • 2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6343

    Problem L. Graph Theory Homework

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 1536    Accepted Submission(s): 830


    Problem Description
    There is a complete graph containing n vertices, the weight of the i-th vertex is wi.
    The length of edge between vertex i and j (ij) is |wiwj|−−−−−−−√.
    Calculate the length of the shortest path from 1 to n.
     
    Input
    The first line of the input contains an integer T (1T10) denoting the number of test cases.
    Each test case starts with an integer n (1n105) denoting the number of vertices in the graph.
    The second line contains n integers, the i-th integer denotes wi (1wi105).
     
    Output
    For each test case, print an integer denoting the length of the shortest path from 1 to n.
     
    Sample Input
    1
    3
    1 3 5
     
    Sample Output
    2
     
    Source
     

    题意概括:

    给出每个点的权值,点与点之间的距离等于 √| wi-wj |  ,求起点到终点的最短距离。

    解题思路:

    一道伪装成图论的水题。

    最短距离就是两点距离,因为如果中间放入其他点来进行更新路径是不会获得更短的路径的。

    因为 √a + √b  > √(a+b) ;

    AC code:

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<vector>
     6 #include<queue>
     7 #include<cmath>
     8 #include<set>
     9 #define INF 0x3f3f3f3f
    10 #define LL long long
    11 using namespace std;
    12 int N;
    13 
    14 int myabs(int x)
    15 {
    16     if(x < 0) return -x;
    17     return x;
    18 }
    19 
    20 int main()
    21 {
    22     int w, st, ed;
    23     int T_case;
    24     scanf("%d", &T_case);
    25     while(T_case--){
    26         scanf("%d", &N);
    27         for(int i = 1; i <= N; i++){
    28             scanf("%d", &w);
    29             if(i == 1) st = w;
    30             if(i == N) ed = w;
    31         }
    32         int ans = sqrt(myabs(st-ed));
    33         printf("%d
    ", ans);
    34     }
    35     return 0;
    36 }
    View Code
  • 相关阅读:
    Python介绍
    产品经理知识体系之产品运营
    go rabbitmq延时队列
    docker安装PHP7.2及扩展
    关于js初始化对象的时间点的笔记
    gorm学习随笔
    Ubuntu18.04 安装PHP7.3
    PHP 冒泡、选择、插入排序
    MySQL 间隙锁的一些个人理解
    小程序插件 wx.navigateTo路由url设置
  • 原文地址:https://www.cnblogs.com/ymzjj/p/10330782.html
Copyright © 2011-2022 走看看