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 (i≠j) is ?|wi?wj|???????√?.
Calculate the length of the shortest path from 1 to n.

Input

The first line of the input contains an integer T (1≤T≤10) denoting the number of test cases.
Each test case starts with an integer n (1≤n≤105) denoting the number of vertices in the graph.
The second line contains n integers, the i-th integer denotes wi (1≤wi≤105).

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

2018 Multi-University Training Contest 4

题意概括:

给出每个点的权值,点与点之间的距离等于 √| 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\n", ans);
34     }
35     return 0;
36 }

原文地址:https://www.cnblogs.com/ymzjj/p/10330782.html

时间: 2024-10-03 02:02:01

2018 Multi-University Training Contest 4 Problem L. Graph Theory Homework 【YY】的相关文章

2018 Multi-University Training Contest 4 Problem K. Expression in Memories 【模拟】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6342 Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2150    Accepted Submission(s): 772Special Judge Problem De

2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1363    Accepted Submission(s): 717 Problem Description Sudoku i

华农oj Problem L: CreatorX背英语【STL】

Problem L: CreatorX背英语 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 53 Solved: 36 [Submit][Status][Web Board] Description CreatorX最近在忙着背英语, Hzk is a young and beautiful and cute boy 但是正常背诵的效果他觉得不好,于是他又想了一个点子,倒着背. Boy cute and beautiful and young a i

HDU 2018 Multi-University Training Contest 3 Problem A. Ascending Rating 【单调队列优化】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6319 Problem A. Ascending Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 5943    Accepted Submission(s): 2004 Problem Description Before

华农oj Problem B: Averyboy找密码【STL】

Problem B: Averyboy找密码 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 83 Solved: 29 [Submit][Status][Web Board] Description Averyboy获得了一个串只由大小写字母组成的密码,他现在要想办法解开密码的key,这个密码的key就是其中每个字母出现的次数的中位数.他现在重金求key,你能帮助他吗? Input 第一行一个数字T代表测试的组数.(T<=10) 对于每组测试一行只

2018 Nowcoder Multi-University Training Contest 2

Practice Link A. run 题意: 白云每次可以移动\(1\)米或者\(k\)米,询问移动的米数在\([L, R]\)范围内的方案数有多少. 思路: \(dp[i][2]\)表示到第\(i\)米,是通过\(1\)米的方式过来的还是\(k\)米的方式过来的,递推即可. 代码: #include <bits/stdc++.h> using namespace std; #define N 100010 const int p = 1e9 + 7; int f[N][2], g[N];

2018 Nowcoder Multi-University Training Contest 1

Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdots, a_n\)中有多少个不同的数. 思路: 先分别离线求出\(a_1, \cdots a_i\)以及\(a_j, \cdots, a_n\)中有多少个不同的数. 再考虑有多少个数既在\([1, i]\)中也在\([j, n]\)中,再离线做一次. 考虑一个数第一次出现的时候,那么这个数下一次出现

2018 Nowcoder Multi-University Training Contest 5

Practice Link A. gpa 题意: 有\(n\)门课程,每门课程的学分为\(s_i\),绩点为\(c_i\),要求最多删除\(k\)门课程,使得gpa最高. gpa计算方式如下: \[ \begin{eqnarray*} gpa = \frac{\sum s_ic_i}{\sum s_i} \end{eqnarray*} \] 思路: 首先删去的课程越多,gpa肯定不会变得更差. 所以我们肯定是删去\(k\)门课程. 考虑二分答案,check的时候要满足: \[ \begin{eq

ZOJ Problem Set - 3203 Light Bulb 【三分法】

题目:ZOJ Problem Set - 3203 Light Bulb 题意: 如图,有个人在地上走,然后他的影子可以投影到墙上或者地上,问影子最长是多少? 分析: 我们知道,三分法是解决一个凹或凸函数的极大极小值,发现这个影子从刚好投影到右下角开始便是一个凸函数,他的影子长度是先递增后递减的,所以可以用三分法. 三分法的原理: AC代码: #include <cstdio> #include <cstring> #include <vector> #include