Notice
Recent Posts
Recent Comments
Today
Total
01-10 22:06
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[๋ฐฑ์ค€ 1197] ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ (JAVA) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[๋ฐฑ์ค€ 1197] ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ (JAVA)

WonderJay 2023. 1. 19. 14:59
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

๋”๋ณด๊ธฐ

๋ฌธ์ œ

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

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

์ž…๋ ฅ

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

๊ทธ๋ž˜ํ”„์˜ ์ •์ ์€ 1๋ฒˆ๋ถ€ํ„ฐ V๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๊ณ , ์ž„์˜์˜ ๋‘ ์ •์  ์‚ฌ์ด์— ๊ฒฝ๋กœ๊ฐ€ ์žˆ๋‹ค. ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ -2,147,483,648๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 2,147,483,647๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1 

3 3
1 2 1
2 3 2
1 3 3

์˜ˆ์ œ ์ถœ๋ ฅ 1 

3

๊ทธ๋ƒฅ Minimum spanning tree(MST) ๋ฅผ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.

 

์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ edge list ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜๋Š”๋ฐ, ์ค‘์š”ํ•œ ๊ฒƒ์€

 

๊ฐ€์ค‘์น˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ €์žฅํ•œ๋‹ค.

 

์ด๋ฅผ ์œ„ํ•ด์„œ ์šฐ์„ ์ˆœ์œ„ํ ํ˜•ํƒœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ฑ„ํƒํ•œ๋‹ค.

 

์šฐ์„ ์ˆœ์œ„ํ์— ์—ฃ์ง€ ์ •๋ณด๊ฐ€ ์ €์žฅ๋˜๋ฉด,

 

ํ˜„์žฌ ์ƒํƒœ์—์„œ ๊ฐ€์žฅ ๊ฐ€์ค‘์น˜๊ฐ€ ์ตœ์†Œ์ธ ์—ฃ์ง€๋ฅผ ์„ ํƒํ•˜๊ณ ,

 

์‚ฌ์ดํด์ด ์ด๋ฃจ์–ด์ง€์ง€ ์•Š๋„๋ก ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค.

 

์ด๋ฅผ ์œ„ํ•ด์„œ ์œ ๋‹ˆ์˜จ&ํŒŒ์ธํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜์—ฌ

 

์‚ฌ์ดํด์„ ์ด๋ฃจ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํŒŒ์•…ํ•ด์•ผ ํ•œ๋‹ค.

 

๊ตฌํ˜„์€ ์–ด๋ ต์ง€ ์•Š๋‹ค.

 

import java.io.*;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {
    static PriorityQueue<Edge> pq;
    static int [] parent;
    public static void main(String[] args) throws IOException {
        System.setIn(new FileInputStream("src/input.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer stk = new StringTokenizer(br.readLine());
        int v = Integer.parseInt(stk.nextToken()); // ๋…ธ๋“œ ๊ฐœ์ˆ˜
        int e = Integer.parseInt(stk.nextToken()); // ์—์ง€ ๊ฐœ์ˆ˜
        pq = new PriorityQueue<>();
        for (int i = 0; i < e; i++) {
            stk = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(stk.nextToken());
            int b = Integer.parseInt(stk.nextToken());
            int cost = Integer.parseInt(stk.nextToken());
            pq.add(new Edge(a, b, cost));
        }

        parent = new int[v + 1];
        for (int i = 1; i <= v; i++)
            parent[i] = i; // ์œ ๋‹ˆ์˜จ&ํŒŒ์ธํŠธ ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
        int used = 0;
        int min_cost=0;
        while(used < v-1){
            Edge now_edge = pq.poll();
            if(find(now_edge.s) != find(now_edge.e)){
                // ๋ถ€๋ชจ๊ฐ€ ๋‹ค๋ฅด๋ฉด ์‚ฌ์ดํด์ด ํ˜•์„ฑ๋˜์ง€ ์•Š๋Š” ๊ฒƒ
                union(now_edge.s, now_edge.e); // ๊ทธ๋Ÿฌ๋ฉด ์—ฐ๊ฒฐ
                min_cost+=now_edge.w;
                used++;
            }
        }
        System.out.println(min_cost);
    }
    static void union(int a, int b){
        a = find(a);
        b = find(b);
        if(a!=b)
            parent[b] = a;
    }
    static int find(int a){
        if(parent[a] == a)
            return a;
        else return parent[a] = find(parent[a]);
    }
    static class Edge implements Comparable<Edge> {
        int s;
        int e;
        int w;

        public Edge(int s, int e, int w) {
            this.s = s;
            this.e = e;
            this.w = w;
        }

        @Override
        public int compareTo(Edge v) {
            return this.w > v.w ? 1 : -1;
        }
    }
}

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments