题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6343

Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 262144/262144 K (Java/Others)

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 ⌊sqrt(|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≤10^5) denoting the number of vertices in the graph.
The second line contains n integers, the i-th integer denotes wi (1≤wi≤10^5).

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

 

题意:

给出一张完全图由n个点组成,编号1~n,每个点有一个权重 ${w_i }$,对于任意不同两点 i 和 j 之间的边的长度为 $\left\lfloor {\sqrt {\left| {w_i - w_j } \right|} } \right\rfloor$,

要求给出从1到n的最短路长度。

题解:

先说结论:对于任意两点 i 和 j,${\mathop{\rm edge}\nolimits} \left( {i,j} \right)$ 这条边是最短路;

我们首先来证明:

$\left\lfloor {\sqrt a } \right\rfloor + \left\lfloor {\sqrt b } \right\rfloor \ge \left\lfloor {\sqrt {a + b} } \right\rfloor$

其中 $a,b$ 均为正整数。

证明:

设有两个正整数 $m,n$ 满足 $a \in \left[ {m^2 ,\left( {m + 1} \right)^2 - 1} \right],b \in \left[ {n^2 ,\left( {n + 1} \right)^2 - 1} \right]$,则 $\left\lfloor {\sqrt a } \right\rfloor = m,\left\lfloor {\sqrt b } \right\rfloor = n$,

那么自然就有

$a + b \in \left[ {m^2 + n^2 ,m^2 + n^2 + 2m + 2n} \right]$

$\sqrt {a + b} \in \left[ {\sqrt {m^2 + n^2 } ,\sqrt {m^2 + n^2 + 2m + 2n} } \right]$

此时,我们考察两个完全平方数 $\left( {m + n} \right)^2 ,\left( {m + n + 1} \right)^2$,将他们展开:$m^2 + n^2 + 2mn\;\;,\;\;m^2 + n^2 + 2mn + 2m + 2n + 1$,

显然 $\left( {m + n + 1} \right)^2 = \;m^2 + n^2 + 2mn + 2m + 2n + 1 > m^2 + n^2 + 2m + 2n$,也就是说平方数 $\left( {m + n + 1} \right)^2$ 大于 $a+b$ 所属区间的右端点,

再分类讨论 $\left( {m + n} \right)^2$ 与 $a+b$ 所属区间的右端点的关系:

①若 $mn \ge m + n$,则

$\left( {m + n} \right)^2 = m^2 + n^2 + 2mn \ge m^2 + n^2 + 2m + 2n$

$m + n \ge \sqrt {m^2 + n^2 + 2m + 2n}$

 即

$\left\lfloor {\sqrt a } \right\rfloor + \left\lfloor {\sqrt b } \right\rfloor \ge \sqrt {m^2 + n^2 + 2m + 2n} \ge \sqrt {a + b} \ge \left\lfloor {\sqrt {a + b} } \right\rfloor$

②若 $mn < m + n$,则

$m^2 + n^2 + 2mn < m^2 + n^2 + 2m + 2n$

 也就是说,$a + b$ 所属区间 $\left[ {m^2 + n^2 ,m^2 + n^2 + 2m + 2n} \right]$ 的右端点在两个完全平方数 $\left( {m + n} \right)^2 ,\left( {m + n + 1} \right)^2$ 之间,

 那么根据开根号再向下取整的性质,显然有

$\left\lfloor {\sqrt {a + b} } \right\rfloor \le \sqrt {\left( {m + n} \right)^2 } = m + n = \left\lfloor {\sqrt a } \right\rfloor + \left\lfloor {\sqrt b } \right\rfloor$

综上所述,就证明了 $\left\lfloor {\sqrt a } \right\rfloor + \left\lfloor {\sqrt b } \right\rfloor \ge \left\lfloor {\sqrt {a + b} } \right\rfloor$,

而且不难发现,将 $a,b$ 范围扩大成均为非负整数也不会影响上述不等式成立。

接下来,对于完全图上的任意两点 i 和 j,若任取其他一个点 k,我们来证明 $\left\lfloor {\sqrt {\left| {w_i - w_k } \right|} } \right\rfloor + \left\lfloor {\sqrt {\left| {w_k - w_j } \right|} } \right\rfloor \ge \left\lfloor {\sqrt {\left| {w_i - w_j } \right|} } \right\rfloor$,

换句话说,我们要证明 ${\mathop{\rm edge}\nolimits} \left( {i,k} \right) + {\mathop{\rm edge}\nolimits} \left( {k,j} \right) \ge {\mathop{\rm edge}\nolimits} \left( {i,j} \right)$,此处 ${\mathop{\rm edge}\nolimits} \left( {i,j} \right)$ 代表连接 i 和 j 两点的边的长度。

证明:

首先,根据绝对值不等式可以知道

$\left| {w_i - w_k } \right| + \left| {w_k - w_j } \right| \ge \left| {w_i - w_k + w_k - w_j } \right| = \left| {w_i - w_j } \right|$

其次,易知若两非负整数满足 $m \ge n$,则 $\left\lfloor {\sqrt m } \right\rfloor \ge \left\lfloor {\sqrt n } \right\rfloor$,

那么自然就有

$\left\lfloor {\sqrt {\left| {w_i - w_k } \right| + \left| {w_k - w_j } \right|} } \right\rfloor \ge \left\lfloor {\sqrt {\left| {w_i - w_j } \right|} } \right\rfloor$

再者,根据上文证明的公式 $\left\lfloor {\sqrt a } \right\rfloor + \left\lfloor {\sqrt b } \right\rfloor \ge \left\lfloor {\sqrt {a + b} } \right\rfloor$,有

$\left\lfloor {\sqrt {\left| {w_i - w_k } \right|} } \right\rfloor + \left\lfloor {\sqrt {\left| {w_k - w_j } \right|} } \right\rfloor \ge \left\lfloor {\sqrt {\left| {w_i - w_k } \right| + \left| {w_k - w_j } \right|} } \right\rfloor$

最后,上面两个不等式连起来即

$\left\lfloor {\sqrt {\left| {w_i - w_k } \right|} } \right\rfloor + \left\lfloor {\sqrt {\left| {w_k - w_j } \right|} } \right\rfloor \ge \left\lfloor {\sqrt {\left| {w_i - w_j } \right|} } \right\rfloor$

证毕。

那么,我们就知道了图上任意两点 i 和 j,不会有第三个点 k 存在,使得 ${\mathop{\rm edge}\nolimits} \left( {i,k} \right) + {\mathop{\rm edge}\nolimits} \left( {k,j} \right)$ 比 ${\mathop{\rm edge}\nolimits} \left( {i,j} \right)$ 更小,

那么同样不会存在其他两个点 k 和 p,使得 ${\mathop{\rm edge}\nolimits} \left( {i,k} \right) + {\mathop{\rm edge}\nolimits} \left( {k,p} \right) + {\mathop{\rm edge}\nolimits} \left( {p,j} \right)$ 比 ${\mathop{\rm edge}\nolimits} \left( {i,j} \right)$ 更小,

原因很简单,因为 ${\mathop{\rm edge}\nolimits} \left( {k,p} \right) + {\mathop{\rm edge}\nolimits} \left( {p,j} \right) \ge {\mathop{\rm edge}\nolimits} \left( {k,j} \right)$ 且 ${\mathop{\rm edge}\nolimits} \left( {i,k} \right) + {\mathop{\rm edge}\nolimits} \left( {k,j} \right) \ge {\mathop{\rm edge}\nolimits} \left( {i,j} \right)$,

所以,对于任意两点 i 和 j,不管另取多少个点,都不会让从 i 到 j 的路径比 ${\mathop{\rm edge}\nolimits} \left( {i,j} \right)$ 更短,因而 ${\mathop{\rm edge}\nolimits} \left( {i,j} \right)$ 这条边就是最短路。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
int T,n;
cin>>T;
while(T--)
{
scanf("%d",&n);
int w,a,b;
for(int i=;i<=n;i++)
{
scanf("%d",&w);
if(i==) a=w;
if(i==n) b=w;
}
printf("%d\n",(int)floor(sqrt(abs(a-b))));
}
}

HDU 6343 - Problem L. Graph Theory Homework - [(伪装成图论题的)简单数学题]的更多相关文章

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

  2. 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 ...

  3. HDU 6330.Problem L. Visual Cube-模拟到上天-输出立方体 (2018 Multi-University Training Contest 3 1012)

    6330.Problem L. Visual Cube 这个题就是输出立方体.当时写完怎么都不过,后来输出b<c的情况,发现这里写挫了,判断失误.加了点东西就过了,mdzz... 代码: //1 ...

  4. HDU 6437 Problem L.Videos (最大费用)【费用流】

    <题目链接> 题目大意: 一天有N个小时,有m个节目(每种节目都有类型),有k个人,连续看相同类型的节目会扣w快乐值.每一种节目有都一个播放区间[l,r].每个人同一时间只能看一个节目,看 ...

  5. HDU - 6437 Problem L.Videos 2018 Multi-University Training Contest 10 (最小费用最大流)

    题意:M个影片,其属性有开始时间S,结束时间T,类型op和权值val.有K个人,每个人可以看若干个时间不相交的影片,其获得的收益是这个影片的权值val,但如果观看的影片相邻为相同的属性,那么收益要减少 ...

  6. 【HDOJ6343】Graph Theory Homework(贪心)

    题意: 给定n个点,每个点有权值a[i],从A走到B的花费是下取整sqrt(a[i]-a[j]),求从1号点走到n号点的最小花费 1<=n,a[i]<=1e5 思路: #include&l ...

  7. Introduction to graph theory 图论/脑网络基础

    Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...

  8. HDU 5876:Sparse Graph(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description   In graph theory, t ...

  9. HDU 6430 Problem E. TeaTree(虚树)

    Problem E. TeaTree Problem Description Recently, TeaTree acquire new knoledge gcd (Greatest Common D ...

随机推荐

  1. 双调旅行商问题 (Bitonic TSP)

    问题描写叙述: 上述问题能够使用动态规划的方法来解决. 以下是解决思路的详细介绍: 1. 最优子结构: 如果d[i][j]表示从起点1出发到达i及j两个顶点的最短路程之和. 为此能够如果K为此段路程上 ...

  2. Dubbo -- 系统学习 笔记 -- 示例 -- 多注册中心

    Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 多注册中心 可以自行扩展注册中心,参见:注册中心扩展 (1) 多注册中心注册 比如 ...

  3. iOS Ad hoc

    There's one situation in which you need an Ad Hoc profile, and that's when you want to test Push Not ...

  4. python BeautifulSoup库用法总结

    1. Beautiful Soup 简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...

  5. eclipse+pydev 怎么导入已有的python项目

    转自:https://zhidao.baidu.com/question/2117277007790501747.html 已有的python项目导入eclipse的步骤: 1.首先,打开Eclips ...

  6. 转载mysql数据库配置优化

    网上有很多的文章教怎么配置MySQL服务器,但考虑到服务器硬件配置的不同,具体应用的差别,那些文章的做法只能作为初步设置参考,我们需要根据自己的情况进行配置优化,好的做法是MySQL服务器稳定运行了一 ...

  7. phpadmin试用

    在Windows或者Linux下mysql安装后默认的密码为空,又当我们又安装了mysql的管理工具phpmyadmin后登陆时 出现“空密码登陆呗禁止(参见允许密码为空)”.不能登录成功       ...

  8. 基础知识《零》---一张图读懂JDK,JRE,JVM的区别与联系

  9. Failure to transfer org.springframework.boot:spring-boot-starter-parent:pom:2.0.1.RELEASE from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempt

    第一次用 Spring Starter Project 创建一个Spring应用时,POM 文件报错: Project build error: Non-resolvable parent POM f ...

  10. Linux设备驱动剖析之SPI(三)

    572至574行,分配内存,注意对象的类型是struct spidev_data,看下它在drivers/spi/spidev.c中的定义: struct spidev_data { dev_t de ...