HDU 6343 - Problem L. Graph Theory Homework - [(伪装成图论题的)简单数学题]
题目链接: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 - [(伪装成图论题的)简单数学题]的更多相关文章
- 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 - ...
- 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 ...
- HDU 6330.Problem L. Visual Cube-模拟到上天-输出立方体 (2018 Multi-University Training Contest 3 1012)
6330.Problem L. Visual Cube 这个题就是输出立方体.当时写完怎么都不过,后来输出b<c的情况,发现这里写挫了,判断失误.加了点东西就过了,mdzz... 代码: //1 ...
- HDU 6437 Problem L.Videos (最大费用)【费用流】
<题目链接> 题目大意: 一天有N个小时,有m个节目(每种节目都有类型),有k个人,连续看相同类型的节目会扣w快乐值.每一种节目有都一个播放区间[l,r].每个人同一时间只能看一个节目,看 ...
- HDU - 6437 Problem L.Videos 2018 Multi-University Training Contest 10 (最小费用最大流)
题意:M个影片,其属性有开始时间S,结束时间T,类型op和权值val.有K个人,每个人可以看若干个时间不相交的影片,其获得的收益是这个影片的权值val,但如果观看的影片相邻为相同的属性,那么收益要减少 ...
- 【HDOJ6343】Graph Theory Homework(贪心)
题意: 给定n个点,每个点有权值a[i],从A走到B的花费是下取整sqrt(a[i]-a[j]),求从1号点走到n号点的最小花费 1<=n,a[i]<=1e5 思路: #include&l ...
- Introduction to graph theory 图论/脑网络基础
Source: Connected Brain Figure above: Bullmore E, Sporns O. Complex brain networks: graph theoretica ...
- HDU 5876:Sparse Graph(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5876 Sparse Graph Problem Description In graph theory, t ...
- HDU 6430 Problem E. TeaTree(虚树)
Problem E. TeaTree Problem Description Recently, TeaTree acquire new knoledge gcd (Greatest Common D ...
随机推荐
- AngularJS------报错"The selector "app-user-item" did not match any elements"
原因:新建的组件没有在任何界面使用到 解决方法:在界面使用该组件
- Dubbo -- 系统学习 笔记 -- 示例 -- 多协议
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 多协议 可以自行扩展协议,参见:协议扩展 (1) 不同服务不同协议 比如:不同服务 ...
- PostgreSQL存储过程(4)-return语句
1. return语句 有三个命令可以用来从函数中返回数据: RETURN RETURN NEXT RETURN QUERY 2. RETURN命令 语法: RETURN RETURN express ...
- python卸载或者安装时提示There is a problem with this Windows Installer package.A program required for this install to complete could not be run. Contact your support personnel or package vendor
1.卸载时报这个错,先进行下修复,再执行卸载: 2.安装时报这个错,安装的过程中,没有取得管理员的权限. Msi格式的文件,点右键后,也没有“以管理员身份运行”的菜单项,那怎么办呢?你可以点“开始”菜 ...
- 使用java连接数据库以后显示“ Establishing SSL connection without server's identity verification is not recommended”的警告如何解决
今天写了一段查询数据库的操作,如下 package MySQL; import java.sql.*; public class MySQL { //JDBC驱动名以及数据库URL static fi ...
- linux下复制文件报cp: omitting directory `XXX'
错误操作:这个错误是因为在linux下我使用cp复制一个文件夹A到另一个目录下时报错cp: omitting directory `XXX' 原因:文件夹A中还有子目录文件,所以linux不允许直接复 ...
- MyEclipse中自定义maven命令(添加maven 命令)
关于常用的Maven命令 mvn archetype:create :创建 Maven 项目 mvn compile :编译源代码 mvn test-compile :编译测试代码 mvn test ...
- matplotlib包画基本的图
画直线图 1.最简单的用法: import matplotlib.pyplot as plt import numpy as np x=np.linspace(-3,3,50) #在(-1,1)范围内 ...
- Qt自定义控件大全(一)云台仪表盘控件
做过安防视频监控的同学都清楚,在视频监控系统软件上都可以看到一个云台控制区域,可以对球机进行下下左右等八个方位的运动控制,还可以进行复位,一般都是美工作图好,然后贴图的形式加入到软件中,好处是程序简单 ...
- 【大数据系列】Hive安装及web模式管理
一.什么是Hive Hive是建立在Hadoop基础常的数据仓库基础架构,,它提供了一系列的工具,可以用了进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在Hadoop中的按规模数据的 ...