zoukankan      html  css  js  c++  java
  • 牛客寒假算法基础集训营4 G(最小生成树)

    题目链接
    题目要求的是得到k种不同的元素,(题解)将每种类型视为一个点,跑一边克鲁斯卡尔即可。

    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <math.h>
    #include <bitset>
    #include <vector>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #define MAXN 1010100
    #define LL long long
    #define fi first
    #define se second
    #define mp make_pair
    #define pb push_back
    #define ll __int64
    #define INF 0x7fffffff
    #define cs(s) freopen(s,"r",stdin)
    #define mem(x) memset(x,0,sizeof(x))
    #define PI acos(-1)
    #define eps 1e-10
    using namespace std;
    int gcd(int a,int b){return b?gcd(b,a%b):a;}
    int lcm(int a,int b){return a/gcd(a,b)*b;}
    LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    //head
    int f[100001];
    int find(int x){
        return x==f[x]?x:f[x]=find(f[x]);
    }
    struct uzi
    {
        int a,b,w;
        bool operator <(const uzi &t)const {
            return w<t.w;
        }
    };
    vector<uzi>v;
    map<int,int>ma;
    int a[100001],vis[100001],cnt,num;
    int main(){
        ios::sync_with_stdio(false);
        int n,m,ka;
        cin>>n>>m>>ka;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        for(int i=1;i<=n;i++)f[i]=i;
        for(int i=1;i<=m;i++){
            int s,t,w;
            cin>>s>>t>>w;
            if(a[s]!=a[t])v.pb({a[s],a[t],w});
        }
        sort(v.begin(), v.end());
        LL sum=0;
        for(auto k:v){
            int x=find(k.a);
            int y=find(k.b);
            if(x==y)continue;
            f[y]=x;
            sum+=k.w;
            if(++cnt==ka-1)break;
        }
        if(cnt<ka-1)cout<<-1<<endl;
        else cout<<sum<<endl;
        return 0;
    }
    
  • 相关阅读:
    【转】SQL SERVER函数无法执行对数据库的修改语句
    【转】用SQL实现树的查询
    HTML: < 和 > 是何方神圣
    ASP.NET的一些小问题
    C#的MD5哈希值计算
    高度自适应的CSS
    [转]WCF类型共享技巧
    使用.net的跟踪诊断来记录wcf消息
    【转】js frame 框架编程
    js点击button按钮跳转到页面代码
  • 原文地址:https://www.cnblogs.com/pubgoso/p/10759727.html
Copyright © 2011-2022 走看看