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

|-----------|-----|-------|-------|-------|-------|--------|
| root[i] | 初始化 | 合并1 2 | 合并1 3 | 合并2 3 | 合并3 4 | 最终root |
| root[1] | 1 | 2 | | | | 4 |
| root[2] | 2 | | 3 | | | 4 |
| root[3] | 3 | | | | 4 | 4 |
| root[4] | 4 | | | | | 4 |

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

|-----------|-----|-------|-------|-------|-------|--------|
| root[i] | 初始化 | 合并2 3 | 合并1 2 | 合并1 3 | 合并3 4 | 最终root |
| root[1] | 1 | | 3 | | | 4 |
| root[2] | 2 | 3 | | | | 4 |
| root[3] | 3 | | | | 4 | 4 |
| root[4] | 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博客

相关推荐
皇族崛起18 小时前
【docker安装部署】- 一个可用的Docker 镜像配置 和 DNS配置
java·docker·容器
互亿无线明明19 小时前
国际短信通知服务:如何为全球业务构建稳定的跨国消息触达体系?
java·c语言·python·php·objective-c·ruby·composer
深盾科技19 小时前
Linux跨进程内存操作的3种方法及防护方案
java·linux·网络
亭上秋和景清19 小时前
qsort函数(快速排序)
数据结构·算法
Jerry9527062819 小时前
1.什么式可用性
java·分布式·后端·架构·高可用·秒杀
轻描淡写60619 小时前
二进制存储数据
java·开发语言·算法
爱潜水的小L19 小时前
自学嵌入式day28,文件操作
linux·数据结构·算法
2301_8003997219 小时前
误用sizeof()计算指针
算法
君爱学习19 小时前
JVM对象分配内存如何保证线程安全?
java
ULTRA??19 小时前
QT向量实现GJK碰撞检测算法几何图形二维版本
c++·qt·算法