zoukankan      html  css  js  c++  java
  • 宁波多校(四) F题 修路(最小生成树)

    本题不用边读边做,可以先读入所有边记录序号

    排序后,求取m次最小生成树,参与生成的是序号小于当前次数的边

    #include<iostream>
    #include<algorithm>
    #include<stack>
    #include<vector>
    #include<cstring>
    #include<set>
    #include<map>
    using namespace std;
    typedef long long ll;
    const int N=3e5+10;
    const int inf=0x3f3f3f3f;
    int p[N];
    struct node{
        int a,b;
        int w;
        int k;
    }s[N];
    int find(int x){
        if(x!=p[x]){
            p[x]=find(p[x]);
        }
        return p[x];
    }
    bool cmp(node a,node b){
        return a.w<b.w;
    }
    int main(){
        ios::sync_with_stdio(false);
        int n,m;
        cin>>n>>m;
        int i;
        for(i=1;i<=m;i++){
            cin>>s[i].a>>s[i].b>>s[i].w;
            s[i].k=i;
        }
        sort(s+1,s+1+m,cmp);
        for(i=1;i<=m;i++){
            for(int j=1;j<=n;j++)
                p[j]=j;
            int cnt=n;
            int ans=0;
            for(int j=1;j<=m;j++){
                if(s[j].k<=i){
                    int pa=find(s[j].a),pb=find(s[j].b);
                    if(pa!=pb){
                        p[pa]=pb;
                        ans+=s[j].w;
                        cnt--;
                    }
                }
            }
            if(cnt!=1){
                cout<<-1<<endl;
            }
            else{
                cout<<ans<<endl;
            }
        }
    }
    View Code
  • 相关阅读:
    python locust 性能测试:locust安装和一些参数介绍
    输入一串字符,检查是否可以组成friend
    Django基础
    JQuery基础
    Javascript基础
    CSS基础
    HTML基础
    MYSQL数据库
    I/O模型
    协程-----Coroutine
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/13277623.html
Copyright © 2011-2022 走看看