14分求调

P3366 【模板】最小生成树

Vlexander @ 2024-09-19 20:57:40

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

int n,m,f[5010],ans;
bool vis[5010];
struct Edge{
    int u,v,t;
}mp[200010];

bool cmp(Edge a,Edge b){
    return a.t<a.t;
}

int find(int x){
    if(x!=f[x]) f[x]=find(f[x]);
    return f[x];
}

int main(){
    for(int i=0;i<5001;i++) f[i]=i;
    scanf("%d %d",&n,&m);

    for(int i=0;i<m;i++)
        scanf("%d %d %d",&mp[i].u,&mp[i].v,&mp[i].t);

    sort(mp,mp+m,cmp);

    for(int i=0;i<m;i++){
        if(find(mp[i].u)==find(mp[i].v)) continue;
        f[find(mp[i].u)]=f[find(mp[i].v)];
        ans+=mp[i].t;
        vis[mp[i].u]=vis[mp[i].v]=1;
    }
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            printf("orz");
            return 0;
        }
    }
    printf("%d",ans);
    return 0 ;
}

by littlep001 @ 2024-09-19 21:06:55

bool cmp(Edge a,Edge b){
    return a.t<a.t; //这是怎么个事
}

by littlep001 @ 2024-09-19 21:07:04

@Vlexander


by Vlexander @ 2024-09-19 21:29:58

@littlep001 额。。。谢谢!


|