题目链接: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. iframe设置高度为100%

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 新版本的body-parser中间件和morgan中间件引用问题:body-parser deprecated bodyParser和morgan deprecated morgan(options)

    引用新版本的body-parser中间件和morgan中间件时,报如下问题: Fri, 09 Jan 2015 06:32:04 GMT morgan deprecated morgan(option ...

  3. mysql的wait_timeout配置(此处处理方法是有问题的,不建议作为操作参考)

    mysql数据库有一个wait_timeout的配置,默认值为28800(即8小时). 在默认配置不改变的情况下,如果连续8小时内都没有访问数据库的操作,再次访问mysql数据库的时候,mysql数据 ...

  4. 【Leaflet】鼠标提取坐标

    map.on('mousemove', function (e) { document.getElementById('info').innerHTML = /* innerHTML 属性设置或返回表 ...

  5. 【Python】Docx解析

    1.cd D:\ProgramData\Anaconda3 2.pip install python-docx 3.python代码处理 # -*- coding: utf-8 -*- import ...

  6. Weblogic CVE-2018-2894 漏洞复现

    0x01 前言 Oracle官方发布了7月份的关键补丁更新CPU(Critical Patch Update),其中针对可造成远程代码执行的高危漏洞 CVE-2018-2894 进行修复:http:/ ...

  7. Ansible Playbook 使用循环语句

    如下,with_items 是循环的对象,with_items 是 python list 数据结构,task 会循环读取 list 里面的值,key 的名称是 item [root@localhos ...

  8. HTML 格式化

    格式化标签: <!DOCTYPE HTML> <html> <body> <b> This text is bold </b> # < ...

  9. 流程控制与数组——Java疯狂讲义

    顺序结构 if分支语句      if{} 可以有多个else if{} else{} 可以省略 switch分支语句 while循环 do while循环 for循环 嵌套循环 控制循环结构 理解数 ...

  10. MQTT-C-PUB

    /* ============================================================================ Name        : mqtest ...