传送门: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
 

题意概括:

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

解题思路:

一道伪装成图论的水题。

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

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

AC code:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
int N; int myabs(int x)
{
if(x < ) return -x;
return x;
} int main()
{
int w, st, ed;
int T_case;
scanf("%d", &T_case);
while(T_case--){
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%d", &w);
if(i == ) st = w;
if(i == N) ed = w;
}
int ans = sqrt(myabs(st-ed));
printf("%d\n", ans);
}
return ;
}

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

  1. 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: 200 ...

  2. 2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...

  3. HDU 6343 - Problem L. Graph Theory Homework - [(伪装成图论题的)简单数学题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6343 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  4. HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)

    6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...

  5. 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...

  6. 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...

  7. 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/100 ...

  8. 2017 Multi-University Training Contest - Team 9 1003&&HDU 6163 CSGO【计算几何】

    CSGO Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

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

    Problem L: CreatorX背英语 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 53 Solved: 36 [Submit][Status][ ...

随机推荐

  1. svn update 报错,必须先cleanup,然后cleanup失败解决方法

    一 问题描述: 1.svn update失败,提示已被locked,请执行cleanup 2.执行svn cleanup,提示cleanup failed to process the followi ...

  2. [android] 实现返回键操作思路

    记录用户点击的操作历史,使用栈数据结构,频繁的操作栈顶(添加,获取,删除),使用LinkedList 捕获用户的返回键操作,响应返回键,返回上一个界面 MainActivity.java /** * ...

  3. Java finally关键字

    关于finally语句块,有如下特点: 1.finally语句块可以直接和try语句块联用.try...finally... 2.try...catch...finally也可以 3.通常在final ...

  4. 七 FileChannel

    FileChannel是一个连接到文件的通道,可以通过文件通道读写文件 FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 打开FileChannel 在使用FileChannel之 ...

  5. Algorithm——两数之和

    题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中 ...

  6. git 命令收藏

    git init # 初始化本地git仓库(创建新仓库)   git config --global user.name "xxx" # 配置用户名   git config -- ...

  7. CSS属性: 阴影 轮廓 渐变

    注: 本文摘自 宁静致远 - CSDN / 但愿人长久 千里共婵娟 - CSDN 阴影 使用box-shadow属性可以为元素添加阴影效果, 比如 box-shadow: h-shadow v-sha ...

  8. echarts 表格与 div 之间 空白的设置

    一. options 图表选项,包含图表实例任何可配置选项: 公共选项 , 组件选项 , 数据选项 {Object} grid 二. grid 直角坐标系内绘图网格 名称 默认值 描述 {number ...

  9. jQuery阻止向上冒泡事件

    //阻止起泡取消下面的注释 e.stopPropagation(); //或者使用这种方式 //return false; }); $('.three').click(function(e){ ale ...

  10. C#链接数据库:SQL Server 2008

    自己学习C#编程,在WinForm编程中,代码测试连接数据库. 现在sqlserver中测试使用的数据库能否以指定的用户名和密码登录. 如图所示,计算机名为administrator,数据库实例为sq ...