【jzoj4629】【修路】【最小生成树】

22 篇文章 0 订阅

题目大意

有一些点和一些边,修建一条边有花费,但可以是连接的两个点有收益,花费远大于收益,求最小花费使图连通。

解题思路

显然每条边的花费(计算了收益的影响)是一定的,可以直接最小生成树。

code

#include<set>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
int const maxn=100000,maxm=100000;
int n,m,a[maxn+10],father[maxn+10];
LL read(){
    LL val=0;char ch=getchar();
    for(;(ch<'0')||(ch>'9');ch=getchar());
    for(;(ch>='0')&&(ch<='9');val=val*10+ch-'0',ch=getchar());
    return val;
}
struct ed{
    int x,y;LL z;
    friend bool operator<(ed x,ed y){return x.z<y.z;}
};
ed edge[maxm+10];
int getfather(int x){
    if(!father[x])return x;
    return father[x]=getfather(father[x]);
}
int main(){
    freopen("d.in","r",stdin);
    freopen("d.out","w",stdout);
    //scanf("%d%d",&n,&m);
    n=read();m=read();
    fo(i,1,n)a[i]=read();//scanf("%d",&a[i]);
    fo(i,1,m){
        edge[i].x=read();edge[i].y=read();edge[i].z=read();//scanf("%d%d%lld",&edge[i].x,&edge[i].y,&edge[i].z);
        edge[i].z-=a[edge[i].x]+a[edge[i].y];
    }
    sort(edge+1,edge+m+1);
    LL ans=0;
    fo(i,1,m){
        int fx=getfather(edge[i].x),fy=getfather(edge[i].y);
        if(fx!=fy){
            father[fx]=fy;
            ans+=edge[i].z;
        }
    }
    printf("%lld",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值