题目大意:求最短路的总和
思路解析:
二话不说,拆开绝对值来看,不难看出w(u,v)=2min(,),本题想要求最短路的话就得考虑绕路了,举个栗子:假如排完序后的权值为:1,2,4,5,8,9;索引为1,2,3,4,5,6,那么从索引1到其他点的最短路就是2,但如果从索引3到4的距离不是2*a[3]=8,而是经过索引1绕的路的距离:2*a[1]+2*a[1]=4,绕路只能通过索引1来绕,因为其值最小。(注意开long long 哦)
废话不多说,代码如下:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using T = pair<int, int>;
//set<int>S;
//unordered_map<int, int>mp;
const int N = 2e5 + 10;
int a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t; cin >> t;
while (t--) {
int n;
cin >> n;
int i,j;
ll ans{};
for (i = 1; i <= n; ++i)cin >> a[i];
sort(a + 1, a + 1 + n);
ans += 2LL * a[1] * (n - 1);
for (i = 2; i <= n; ++i) {
ans += 1LL*min(4 * a[1], 2 * a[i]) * (n - i);
}
cout << 2LL * ans << endl;
}
return 0;
}