您的位置:首页 > 其它

【NOIP2016提高A组模拟7.15】修路

2016-07-17 18:48 232 查看
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
;
long long a
,ans=0;
struct note
{
int x,y;long long z;
}b
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  最小生成树