CF1540A.Great Graphs
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Farmer John has a farm that consists of n pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.
Unfortunately, Farmer John lost the map of the farm. All he remembers is an array d, where di is the smallest amount of time it took the cows to reach the i-th pasture from pasture 1 using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory.
约翰农民拥有一座由 n 片牧场组成的农场,牧场之间由有向道路连接。每条道路都有一个权重,表示从道路起点到达终点所需的时间。道路的权重可以为负数——此时奶牛跑得如此之快,以至于“回到了过去”!然而,约翰农民保证:奶牛不可能陷入时间循环(即无法通过沿一系列道路行进而无限地回到过去)。此外,任意两片牧场之间在每个方向上至多只有一条道路。
不幸的是,约翰农民丢失了农场的地图。他仅记得一个数组 d,其中 di 表示奶牛从第 1 片牧场出发、经由若干条道路到达第 i 片牧场所需的最短时间。农场的代价定义为所有道路权重之和,约翰农民需要知道:所有与他的记忆一致的农场中,代价最小者是多少?
输入格式
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t cases follow.
The first line of each test case contains a single integer n (1≤n≤105) — the number of pastures.
The second line of each test case contains n space separated integers d1,d2,…,dn (0≤di≤109) — the array d. It is guaranteed that d1=0.
It is guaranteed that the sum of n over all test cases does not exceed 105.
第一行包含一个整数 t(1≤t≤104)—— 测试用例的数量。随后是 t 个测试用例。
每个测试用例的第一行包含一个整数 n(1≤n≤105)—— 牧场的数量。
每个测试用例的第二行包含 n 个以空格分隔的整数 d1,d2,…,dn(0≤di≤109)—— 数组 d。保证 d1=0。
保证所有测试用例的 n 之和不超过 105。
输出格式
For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory.
对于每个测试用例,输出与约翰农民记忆一致的农场的最小可能成本。
输入输出样例
输入#1
3 3 0 2 3 2 0 1000000000 1 0
输出#1
-3 0 0
说明/提示
In the first test case, you can add roads
- from pasture 1 to pasture 2 with a time of 2,
- from pasture 2 to pasture 3 with a time of 1,
- from pasture 3 to pasture 1 with a time of −3,
- from pasture 3 to pasture 2 with a time of −1,
- from pasture 2 to pasture 1 with a time of −2.
The total cost is 2+1+−3+−1+−2=−3.
In the second test case, you can add a road from pasture 1 to pasture 2 with cost 1000000000 and a road from pasture 2 to pasture 1 with cost −1000000000. The total cost is 1000000000+−1000000000=0.
In the third test case, you can't add any roads. The total cost is 0.
在第一个测试用例中,你可以添加以下道路:
- 从牧场 1 到牧场 2,耗时为 2;
- 从牧场 2 到牧场 3,耗时为 1;
- 从牧场 3 到牧场 1,耗时为 −3;
- 从牧场 3 到牧场 2,耗时为 −1;
- 从牧场 2 到牧场 1,耗时为 −2。
总代价为 2+1+−3+−1+−2=−3。
在第二个测试用例中,你可以添加一条从牧场 1 到牧场 2、代价为 1000000000 的道路,以及一条从牧场 2 到牧场 1、代价为 −1000000000 的道路。总代价为 1000000000+−1000000000=0。
在第三个测试用例中,你无法添加任何道路。总代价为 0。
输入解题思路,AI测评打分。不知道怎么写?