文章

85

粉丝

0

获赞

531

访问

11.1k

头像
畅通工程 题解:
P1312 浙江大学机试题
发布于2026年3月7日 18:06
阅读数 85

#include <bits/stdc++.h>
using namespace std;
const int Max = 105;

int fa[Max];

struct node {
    int u,v,w;
};
int find(int x) {
    if (fa[x]== x) {
        return x;
    }
    fa[x] = find(fa[x]);
    return fa[x];
}

void myunion(int a ,int b){
    fa[find(a)] = find(b);
}

bool cmp(node a,node b) {
    return a.w<b.w;
}
int main() {
    int N;
    while (cin>>N) {
        if (N == 0) {
            break;
        }
        int M;
        cin>>M;
        for (int i=1;i<=M;i++) {
            fa[i] = i;
        }
        int sum = 0;
        int count = 0;
        vector<node> nodes;
        while (N--) {
            int u, v, w;
            cin>>u>>v>>w;
            nodes.push_back({u,v,w});
        }
        sort(nodes.begin(), nodes.end(), cmp);
        for (node n : nodes) {
            int cu = n.u;
            int cv = n.v;
            if (find(cu) != find(cv)) {
              ...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发