answer
analyze
Because the contribution degree of each city is \(a[i]*(the number of cities directly connected to the city i)\) , it actually refers to the number of its out-degree or in-degree, each out-degree or In-degree, it will have a contribution value \(a[i]\) ,
then, that is, subtract \(a[i]\) from the edge connecting it .
So, for an edge \((x, y)\) , subtract its cost from \(a[x]+a[y]\) .
Then run a minimum spanning tree.
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
const long long maxlongint=2147483647;
const long long mo=1000000007;
const long long N=100005;
using namespace std;
long long a[N],n,m,ans,fa[N];
struct ddx
{
long long x,y,cost;
}b[N];
bool cmp(ddx x,ddx y)
{
return x.cost<y.cost;
}
long long get(long long x)
{
if(x==fa[x]) return x;
fa[x]=get(fa[x]);
return fa[x];
}
int main()
{
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<=m;i++)
{
scanf("%lld%lld%lld",&b[i].x,&b[i].y,&b[i].cost);
b[i].cost-=a[b[i].x]+a[b[i].y];
}
sort(b+1,b+m+1,cmp);
for(int k=0,i=1;i<=m && k<n-1;i++)
{
int x=get(b[i].x),y=get(b[i].y);
if(x!=y)
{
fa[x]=y;
ans+=b[i].cost;
}
}
printf("%lld",ans);
}