zoukankan      html  css  js  c++  java
  • CH模拟赛 拆地毯

    /*
    MST,注意只能加K条边,但是备选是M条边
    */
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<vector>
    #define ll long long
    #define fo(i,l,r) for(int i = l;i <= r;i++)
    #define fd(i,l,r) for(int i = r;i >= l;i--)
    using namespace std;
    const int N = 100050;
    ll read(){
        ll x=0,f=1;
        char ch=getchar();
        while(!(ch>='0'&&ch<='9')){if(ch=='-')f=-1;ch=getchar();};
        while(ch>='0'&&ch<='9'){x=x*10+(ch-'0');ch=getchar();};
        return x*f;
    }
    struct edge{
        int u,v;ll w;
    }e[N*3];
    int n,m,k,cnt,head[N],f[N];ll ans;
    bool cmp(edge a,edge b){return a.w > b.w;}
    int findf(int x) {return f[x] == x ? f[x] : f[x] = findf(f[x]);}
    int main(){
        freopen("carpet.in","r",stdin);
        freopen("carpet.out","w",stdout);
        n =read();m=read();k=read();
        int u,v;ll w;
        fo(i,1,m){
            e[i].u=read();e[i].v=read();e[i].w=read();
        }
        sort(e+1,e+1+m,cmp);
        fo(i,1,n) f[i] = i;
        int fa,fb;
        fo(i,1,m){
            fa = findf(e[i].u);fb=findf(e[i].v);
            if(fa != fb){
                f[fa] = fb,ans += e[i].w;
                k--;
            }
            if(!k) break;
        }
        cout<<ans;
        return 0;
    } 
  • 相关阅读:
    筛选法求素数
    C/C++经典面试题目
    操作系统笔试面试基本内容
    Win32/MFC的基本概念
    STL采用的标准模板库
    数据结构基本概念
    SQL基础语句
    C/C++基础概念
    计算机网络基础概念
    流水作业 批作业调度
  • 原文地址:https://www.cnblogs.com/hyfer/p/6035535.html
Copyright © 2011-2022 走看看