본문 바로가기

PS/Problems

[Java] 백준 1197번 최소 스패닝 트리

https://www.acmicpc.net/problem/1197

 

1197번: 최소 스패닝 트리

첫째 줄에 정점의 개수 V(1 ≤ V ≤ 10,000)와 간선의 개수 E(1 ≤ E ≤ 100,000)가 주어진다. 다음 E개의 줄에는 각 간선에 대한 정보를 나타내는 세 정수 A, B, C가 주어진다. 이는 A번 정점과 B번 정점이

www.acmicpc.net

2년 전에 부장께 배운 이론이 정말 큰 도움이 되었다.

감사합니다!

import java.util.*;
import java.io.*;

public class g4_1197 {
    static FastScanner fs = new FastScanner();
    static PrintWriter pw = new PrintWriter(System.out);
    static List<List<Node>> list = new ArrayList<>();
    static boolean[] isLinked;
    static PriorityQueue<Node> priq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1.weight , o2.weight));
    static int v, e, cnt = 0, ans = 0;

    public static void main(String[] args) {
        v = fs.nextInt();
        e = fs.nextInt();

        int first1 = -1, first2 = -1, min = Integer.MAX_VALUE;
        for (int i=0;i<=v;i++) list.add(new ArrayList<>());
        for (int i=0;i<e;i++){
            int n1 = fs.nextInt(), n2 = fs.nextInt(), w = fs.nextInt();
            list.get(n1).add(new Node(n2, w));
            list.get(n2).add(new Node(n1, w));

            if (w < min){
                first1 = n1;
                first2 = n2;
                min = w;
            }
        }

        isLinked = new boolean[v + 1];
        isLinked[first1] = true;
        isLinked[first2] = true;
        for (Node n : list.get(first1)) priq.add(n);
        for (Node n : list.get(first2)) priq.add(n);
        cnt += 2;
        ans += min;

        while (true){
            if (cnt == v) break;
            Node curNode = priq.poll();
            if (isLinked[curNode.index]) continue;

            isLinked[curNode.index] = true;
            for (Node n : list.get(curNode.index)) priq.add(n);
            cnt++;
            ans += curNode.weight;
        }
        pw.println(ans);
        pw.close();
    }

    static class Node {
        int index, weight;

        public Node(int index, int weight){
            this.index = index;
            this.weight = weight;
        }
    }

    // ----------input function----------

    static void sort(int[] a) {
        ArrayList<Integer> L = new ArrayList<>();
        for (int i : a)
            L.add(i);
        Collections.sort(L);
        for (int i = 0; i < a.length; i++)
            a[i] = L.get(i);
    }

    static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] readArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}