๊ธฐ๋ก๋ฐฉ

BOJ_1197 : ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1197 : ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ

Soom_1n 2024. 3. 13. 16:20

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

1197๋ฒˆ: ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ

์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ V(1 ≤ V ≤ 10,000)์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E(1 ≤ E ≤ 100,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ E๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ ์ •์ˆ˜ A, B, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋Š” A๋ฒˆ ์ •์ ๊ณผ B๋ฒˆ ์ •์ ์ด

www.acmicpc.net


๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ(MST, Minimum Spanning Tree)๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.
  • ํฌ๋ฃจ์Šค์นผ(Kruskal)๊ณผ ํ”„๋ฆผ(Prim) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ๋Š”๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” ํฌ๋ฃจ์Šค์นผ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ธฐ๋ณธ ํ˜•ํƒœ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
    • ๊ฐ„์„ ์„ ๊ฐ€์ค‘์น˜ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
    • ์‚ฌ์ดํด ๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด ๊ฐ™์€ ์ง‘ํ•ฉ์— ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธ์ด ํ•„์š”ํ•˜๋ฏ€๋กœ Union-Find ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {
    private static int[] parent;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st = new StringTokenizer(br.readLine());

        int V = Integer.parseInt(st.nextToken());
        int E = Integer.parseInt(st.nextToken());
        parent = new int[V + 1];

        for (int i = 1; i <= V; i++) {
            parent[i] = i;
        }

        PriorityQueue<Edge> pq = new PriorityQueue<>();

        for (int i = 0; i < E; i++) {
            st = new StringTokenizer(br.readLine());
            pq.add(new Edge(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
                    Integer.parseInt(st.nextToken())));
        }

        int sum = 0;
        while (!pq.isEmpty()) {
            Edge edge = pq.poll();

            if (union(edge.start, edge.end)) {
                sum += edge.cost;
            }
        }

        bw.write(Integer.toString(sum));
        bw.flush();
    }

    private static boolean union(int a, int b) {
        int ap = find(a);
        int bp = find(b);

        if (parent[ap] == parent[bp]) {
            return false;
        } else {
            parent[bp] = parent[ap];
            return true;
        }
    }

    private static int find(int a) {
        if (a == parent[a]) {
            return a;
        }
        return parent[a] = find(parent[a]);
    }

    private static class Edge implements Comparable<Edge> {
        int start, end, cost;

        public Edge(int start, int end, int cost) {
            this.start = start;
            this.end = end;
            this.cost = cost;
        }

        @Override
        public int compareTo(Edge o) {
            return this.cost - o.cost;
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ๊ฐ„์„ ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ , ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด Edge ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ–ˆ๋‹ค.
  • Union-Find ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์œ„ํ•ด union๊ณผ find ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„ํ–ˆ๋‹ค.
  • ์šฐ์„ ์ˆœ์œ„ ํ์— ์›์†Œ๊ฐ€ ๋‚จ์ง€ ์•Š์„ ๋•Œ ๊นŒ์ง€ ๊ณ„์‚ฐ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
    • ๊ฐ™์€ ์ง‘ํ•ฉ์ด ์•„๋‹ˆ๋ผ๋ฉด, ํ•˜๋‚˜์˜ ์ง‘ํ•ฉ์œผ๋กœ ํ•ฉ์ณ์ฃผ๊ณ  ๊ฐ€์ค‘์น˜๋ฅผ ๋ˆ„์ ํ•œ๋‹ค.
  • ์ตœ์ข… ๋ˆ„์ ๋œ ๊ฐ€์ค‘์น˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  •  ์˜ค๋žœ๋งŒ์— MST ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜๋Š”๋ฐ ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ ์ ์šฉ๋˜๋Š”๊ฑด ๊ธฐ์–ตํ–ˆ์ง€๋งŒ, Union-Find๊ฐ€ ์‚ฌ์šฉ๋˜๋Š”๊ฒŒ ๊ธฐ์–ต ์•ˆ๋‚˜์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๋ช…์„ ๋‹ค์‹œ ์ฝ์–ด๋ณด์•˜๋‹ค.
    • MST ๊ด€๋ จ ๋ฌธ์ œ๋ฅผ ๋” ์—ฐ์Šตํ•  ํ•„์š”๊ฐ€ ์žˆ์„ ๊ฒƒ ๊ฐ™๊ณ , ๋‹ค์Œ์—” Prim ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด๋ด์•ผ๊ฒ ๋‹ค.
728x90