文章

166

粉丝

68

获赞

825

访问

49.6k

头像
继续畅通工程 题解:最简单的理解,你看不懂可以过来找我
P1311 浙江大学机试题
发布于2025年3月14日 14:26
阅读数 142

#include <bits/stdc++.h>
using namespace std;

struct road{
    int a,b,w,s;
    road(int a,int b,int w,int s):a(a),b(b),w(w),s(s){}
    bool operator <(road y){
        if(s==1)w=0;
        if(y.s==1)y.w=0;
        return w<y.w;
    }
};

vector<int>f;

int findroot(int x){
    return f[x]==x?x:f[x]=findroot(f[x]);
}

int main() {
    int n;
    while(cin>>n){
        if(n==0)break;
        f.resize(n+1);
        for(int i=1;i<=n;i++)f[i]=i;
        vector<road>a;
        for(int i=0;i<n*(n-1)/2;i++){
            int x,y,w,s;cin>>x>>y>>w>>s;
            a.push_back(road(x,y,w,s));
        }
        sort(a.begin(),a.end());
        int ans=0;
        for(auto x:a){
            int fa=findroot(x.a);
            int fb=findroot(x.b);
            if(fa!=fb){
                f[fa]=fb;
                if(x.s==0)ans+=x.w;
            }
        }
        cout<<ans<<endl;
    }...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发