zoukankan      html  css  js  c++  java
  • [HDU6343]Graph Theory Homework

    Description
    There is a complete graph containing (n) vertices, the weight of the (i)-th vertex is (w_i).
    The length of edge between vertex (i) and (j) ((i≠j)) is (lfloor sqrt{|w_i-w_j|} floor).
    Calculate the length of the shortest path from 1 to (n).

    Input
    The first line of the input contains an integer (T (1leqslant Tleqslant 10)) denoting the number of test cases.
    Each test case starts with an integer (n (1leqslant nleqslant 10^5)) denoting the number of vertices in the graph.
    The second line contains n integers, the (i)-th integer denotes (w_i (1leqslant w_ileqslant 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
    

    根据(lfloorsqrt{a} floor+lfloorsqrt{b} floorgeqslantlfloorsqrt{a+b} floor,(a,bin))可得,对于任意两点(i,j)而言,必然有(lfloorsqrt{|w_i-w_k|} floor+lfloorsqrt{|w_k-w_j|} floorgeqslantlfloorsqrt{|w_i-w_k|+|w_k-w_j|} floorgeqslantlfloorsqrt{w_i-w_j} floor),故我们直接计算(1sim n)即可

    现对(lfloorsqrt{a} floor+lfloorsqrt{b} floorgeqslantlfloorsqrt{a+b} floor)进行证明

    (m^2leqslant aleqslant (m+1)^2)(n^2leqslant bleqslant (n+1)^2),则有(lfloorsqrt{a} floor=m,lfloorsqrt{b} floor=n)

    (m=0)(n=0),则(a=0)(b=0),此时命题显然成立

    欲证 (m+ngeqslantlfloorsqrt{a+b} floor)

    则只需证 (m+n+1geqslant sqrt{a+b})

    则只需证 (m+n+1geqslantsqrt{(m+1)^2+(n+1)^2})

    则只需证 (m^2+n^2+1+2m+2n+2mngeqslant m^2+1+2m+n^2+1+2n)

    则只需证 (2nm>1),这在(n,m>0)时是显然成立的,故得证

    /*program from Wolfycz*/
    #include<map>
    #include<cmath>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define Fi first
    #define Se second
    #define ll_inf 1e18
    #define MK make_pair
    #define sqr(x) ((x)*(x))
    #define pii pair<int,int>
    #define int_inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline char gc(){
    	static char buf[1000000],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    template<typename T>inline T frd(T x){
    	int f=1; char ch=gc();
    	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
    	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    template<typename T>inline T read(T x){
    	int f=1; char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x<0)	putchar('-'),x=-x;
    	if (x>9)	print(x/10);
    	putchar(x%10+'0');
    }
    const int N=1e5;
    int A[N+10];
    int main(){
    //	freopen(".in","r",stdin);
    //	freopen(".out","w",stdout);
    	int T=read(0);
    	while (T--){
    		int n=read(0);
    		for (int i=1;i<=n;i++)	A[i]=read(0);
    		printf("%d
    ",(int)trunc(sqrt(abs(A[n]-A[1]))));
    	}
    	return 0;
    }
    
    作者:Wolfycz
    本文版权归作者和博客园共有,欢迎转载,但必须在文章开头注明原文出处,否则保留追究法律责任的权利
  • 相关阅读:
    [oldboy-django][1初识django]阻止默认事件发生 + ajax + 模态编辑对话框
    第6章Zabbix分布式监控
    第5章Zabbix自动化监控
    第4章Zabbix监控实践
    第3章Zabbix完整监控
    第2章Zabbix基础进阶
    第1章1zabbix快速入门
    自动化利器-YUM仓库搭建实战
    CentOS7修改网卡为eth0
    NTP时间服务器
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/14930740.html
Copyright © 2011-2022 走看看