Description
The Solution
在每条边上减去两边城市的值,题目就转变成最小生成树了。
最小生成树??<( ̄3 ̄)> ,果断克鲁斯卡尔嘿嘿嘿
Code
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define fo(i,a,b) for (int i=a;i<=b;i++)
#define N 100005
using namespace std;
int n,m,Dad[N];
long long a[N],ans=0;
struct note
{
int x,y;long long z;
}b[N];
bool cmp(note a,note b){return a.z<b.z;}
int get(int x){return !Dad[x]?x:Dad[x]=get(Dad[x]);}
int main()
{
scanf("%d%d",&n,&m);
fo(i,1,n) scanf("%lld",&a[i]);
fo(i,1,m)
{
scanf("%d%d%lld",&b[i].x,&b[i].y,&b[i].z);
b[i].z-=a[b[i].x]+a[b[i].y];
}
sort(b+1,b+m+1,cmp);
int tot=0;
fo(i,1,m)
{
int xx=get(b[i].x),yy=get(b[i].y);
if (xx!=yy)
{
tot++;
ans+=b[i].z;
Dad[xx]=yy;
}
if (tot>=n-1) break;
}
printf("%lld",ans);
return 0;
}