View

๋ฐ˜์‘ํ˜•

SW Expert Academy

๋ฌธ์ œ

๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ทธ ๊ทธ๋ž˜ํ”„์˜ ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋Š”, ์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ์ •์ ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋ถ€๋ถ„ ๊ทธ๋ž˜ํ”„ ์ค‘์—์„œ ๊ทธ ๊ฐ€์ค‘์น˜์˜ ํ•ฉ์ด ์ตœ์†Œ์ธ ํŠธ๋ฆฌ๋ฅผ ๋งํ•œ๋‹ค.

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๊ทธ๋ž˜ํ”„๋Š” ํ•˜๋‚˜์˜ ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„์ž„์ด ๋ณด์žฅ๋œ๋‹ค.

์ž…๋ ฅ

๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ์ค„์—๋Š” ์ „์ฒด test case์˜ ์ˆ˜ T(1โ‰คTโ‰ค10)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๊ฐ ์ผ€์ด์Šค์˜ ์ฒซ์งธ ์ค„์—๋Š” ์ •์ ์˜ ๊ฐœ์ˆ˜ V(1โ‰คVโ‰ค100,000)์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E(1โ‰คEโ‰ค200,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

๋‹ค์Œ E๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๊ฐ„์„ ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ ์ •์ˆ˜ A, B, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ด๋Š” A๋ฒˆ ์ •์ ๊ณผ B๋ฒˆ ์ •์ ์ด ๊ฐ€์ค‘์น˜ C์ธ ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

C๋Š” ์Œ์ˆ˜์ผ ์ˆ˜๋„ ์žˆ์œผ๋ฉฐ, ์ ˆ๋Œ€๊ฐ’์ด 1,000,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

์ถœ๋ ฅ

๊ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งˆ๋‹ค ํ•œ ์ค„์— ๊ฑธ์ณ, ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ˆ˜ โ€œ#(ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ๋ฒˆํ˜ธ) โ€œ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

ํžŒํŠธ

์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ Prim's algorithm๊ณผ Kruskal's algorithm์ด ์žˆ๋‹ค.

๋ณธ ๋ฌธ์ œ์—์„œ๋Š” ์ฃผ์–ด์ง€๋Š” ์ •์ ๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ด ํ•„์š”ํ•˜๋‹ค.

Prim's algorithm์„ ์ด์šฉํ•  ๊ฒฝ์šฐ, ์ž๋ฃŒ๊ตฌ์กฐ ํž™(heap)์„ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค.

Kruskal's algorithm์„ ์ด์šฉํ•  ๊ฒฝ์šฐ, Disjoint Set(์„œ๋กœ์†Œ ์ง‘ํ•ฉ) ํ˜น์€ Union-Find ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1

1
3 3
1 2 1
2 3 2
1 3 3

์˜ˆ์ œ ์ถœ๋ ฅ 1

#1 3

S๊ฐ€ 0์ผ๋•Œ๋Š” sum=0์˜ ์ดˆ๊ธฐํ™”์™€ ๊ฒน์น˜๋ฏ€๋กœ ์‹ ๊ฒฝ์จ์ค˜์•ผํ•œ๋‹ค

๋ถ€๋ถ„์ง‘ํ•ฉ์œผ๋กœ ํ’€๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์Œ


๐Ÿคฆโ€โ™€๏ธ My Solution


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Solution_3124 {
    static class Edge implements Comparable<Edge> {
        int start;
        int end;
        int weight;

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

        @Override
        public int compareTo(Edge o) { // ๊ฐ€์ค‘์น˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ €์žฅ
            return this.weight - o.weight;
        }

    }

    static Edge[] edgeList;
    static int[] parents;
    static int V, E;

    public static void make() { //์œ ์ผํ•œ ๋ฉค๋ฒ„๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด ์ง‘ํ•ฉ์„ ์ƒ์„ฑ 
        for (int i = 0; i < V; i++) {
            parents[i] = i;
        }
    }

    public static int find(int a) { ์ง‘ํ•ฉ ์ฐพ๋Š” ์—ฐ์‚ฐ 
        if (parents[a] == a) {
            return a;
        }
        return parents[a] = find(parents[a]);
    }

    public static boolean union(int a, int b) {  //๋‘ ์ง‘ํ•ฉ์„ ํ•ฉ์น˜๋Š” ์—ฐ์‚ฐ 
        int aRoot = find(a);
        int bRoot = find(b);
        if (aRoot == bRoot) {
            return false;
        }

        parents[bRoot] = aRoot;
        return true;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        for (int i = 1; i <= N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            V = Integer.parseInt(st.nextToken());
            E = Integer.parseInt(st.nextToken());
            parents = new int[V];
            edgeList = new Edge[E];

            for (int k = 0; k < E; k++) {
                st = new StringTokenizer(br.readLine().trim());
                int from = Integer.parseInt(st.nextToken());
                int to = Integer.parseInt(st.nextToken());
                int weight = Integer.parseInt(st.nextToken());

                edgeList[k] = new Edge(from, to, weight);

            } // ๊ฐ„์„  ์ €์žฅ

            make();

            // ๊ฐ„์„ ๋น„์šฉ์ด ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ
            Arrays.sort(edgeList);
            long result = 0;
            int count = 0;// ์—ฐ๊ฒฐ ๊ฐ„์„ ์ˆ˜
            for (Edge edge : edgeList) {
                if (union(edge.start - 1, edge.end - 1)) { // ์‹ธ์ดํด์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์œผ๋ฉด
                    result += edge.weight; // ๊ฐ€์ค‘์น˜๊ฐ’ ์ €์žฅ 
                    if (++count == V - 1) { // ์—ฐ๊ฒฐ ๊ฐ„์„ ์ˆ˜๊ฐ€ ์ •์ ์ˆ˜ -1์ด๋ฉด ๋‹ค ์—ฐ๊ฒฐํ•จ 
                        break;
                    }
                }
            }
            System.out.println("#" + i + " " + result);
        }
    }
}

๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30