HDU 6343 Graph Theory Homework(思维)

题目链接:Graph Theory Homework

题意

给定 n n 个节点的完全图,每个节点都有一个权值 wi,节点 i i 和节点 j 之间的边权为 |wiwj| ⌊ | w i − w j | ⌋ ,问从节点 1 1 到节点 n 的最短路径长度。

输入

第一行为一个整数 T (1T10) T   ( 1 ≤ T ≤ 10 ) ,接下来有 T T 组数据,每组数据第一行为一个整数 n (1n105),第二行有 n n 个整数 w1,w2,,wn (1wi105)

输出

输出节点 1 1 到节点 n 的最短路径。

样例

输入
1
3
1 3 5
输出
2
题解

从节点 1 1 到节点 n 之间,经历的中间节点越多, 1 1 n 的距离越长,最短距离就是从节点 1 1 n 的边权。

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <sstream>
using namespace std;

#define LL long long
const int maxn = 100000 + 100;
int T, n;
int num[maxn];

int main() {
    #ifdef Dmaxiya
    freopen("test.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    #endif // Dmaxiya
    ios::sync_with_stdio(false);

    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &num[i]);
        }
        printf("%d\n", (int)sqrt(abs(num[n] - num[1])));
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值