文章

37

粉丝

67

获赞

3

访问

9.5k

头像
还是畅通工程 题解:简单的
P1341 浙江大学机试题
发布于2024年3月12日 10:40
阅读数 181

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

//定义结点之间的连接信息
struct node {
    int x;
    int y;
    int fee;
};

int fa[100];//记录结点的自己的父亲
int sum = 0,ans=0, n;//记录总建路数
struct node va[100];

bool cmp(struct node a, struct node b) {
    return a.fee < b.fee;
}

int find(int x) {
    if (fa[x] == x) return x;
    fa[x] = find(fa[x]);//压缩了路径
    return fa[x];
}

void createRoute(int len) {
    
    for (int i = 0; i < len; i++) {
       
        int fx = find(va[i].x);
        int fy = find(va[i].y);
        if (fx != fy) {
            fa[fy] = fx;
            sum++;
            ans += va[i].fee;
    ...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发