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

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值