OJ-5G网络建设

示例1

复制代码
输入:
3
3
1 2 3 0
1 3 1 0
2 3 5 0
输出:
4

示例2

复制代码
输入:
3
1
1 2 5 0
输出:
-1

示例3

复制代码
输入:
3
3
1 2 3 0
1 3 1 0
2 3 5 1
输出:
1

分析:压缩路径

顺序:1 2;1 3;2 3;3 4

|-----------|-----|-------|-------|-------|-------|--------|
| rooti | 初始化 | 合并1 2 | 合并1 3 | 合并2 3 | 合并3 4 | 最终root |
| root1 | 1 | 2 | | | | 4 |
| root2 | 2 | | 3 | | | 4 |
| root3 | 3 | | | | 4 | 4 |
| root4 | 4 | | | | | 4 |

顺序:2 3;1 2;1 3;3 4

|-----------|-----|-------|-------|-------|-------|--------|
| rooti | 初始化 | 合并2 3 | 合并1 2 | 合并1 3 | 合并3 4 | 最终root |
| root1 | 1 | | 3 | | | 4 |
| root2 | 2 | 3 | | | | 4 |
| root3 | 3 | | | | 4 | 4 |
| root4 | 4 | | | | | 4 |

结论:优先按照边的权重小的连接,上述4个节点至少需要3条边连接,1 2,1 3,2 3中只需其中任意两条边即能将1、2、3节点连接。

代码:

java 复制代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class 五G网络建设2 {
    private static int[] root;
    private static final List<int[]> edge = new ArrayList<>();

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        
        root = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            root[i] = i;
        }

        for (int i = 0; i < m; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            int z = in.nextInt();
            int p = in.nextInt();

            if (p == 1) {
                merge(x, y);
            } else {
                edge.add(new int[]{x, y, z});
            }
        }
        edge.sort((o1, o2) -> o1[2] - o2[2]);

        int price = 0;
        for (int[] e : edge) {
            if (merge(e[0], e[1]) == 1) {
                price += e[2];
            }
        }

        boolean ok = true;
        for (int i = 1; i <= n; i++) {
            if (getRoot(i) != getRoot(1)) {
                ok = false;
                break;
            }
        }
        if (ok) {
            System.out.println(price);
        } else {
            System.out.println(-1);
        }
    }

    private static int merge(int x, int y) {
        int rootX = getRoot(x);
        int rootY = getRoot(y);
        if (rootX == rootY) {
            return 0;
        }
        root[rootX] = rootY;
        return 1;
    }

    private static int getRoot(int x) {
        if (root[x] == x) {
            return x;
        }
        return getRoot(root[x]);
    }
}

251.【华为OD机试】5G网络建设(最小生成树算法-Java&Python&C++&JS实现)_java算法5g基站-CSDN博客

相关推荐
裕晟资质规划8 小时前
武器装备科研生产单位保密资质申请方法论:条件模型与流程拆解
人工智能·算法
EXI-小洲9 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
2601_9619017010 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
en.en..10 小时前
C语言 标准输入 / 输出缓冲区
算法
何以解忧,唯有..10 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
QQ_216962909611 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端
吃好睡好便好11 小时前
查找函数的使用
学习·算法·matlab·生活·查找函数
学习星球11 小时前
单调栈——从“找下一个更大的“到柱状图中的最大矩形
数据库·c++·算法·leetcode·xcode
前端 贾公子12 小时前
第09章:上下文与记忆 (2)
java·服务器·前端
2601_9622035112 小时前
Java进阶07集合(续)
java·开发语言