Notice
Recent Posts
Recent Comments
Today
Total
01-25 17:32
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[๋ฐฑ์ค€ 1414] ๋ถˆ์šฐ์ด์›ƒ๋•๊ธฐ (JAVA) ๋ณธ๋ฌธ

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

[๋ฐฑ์ค€ 1414] ๋ถˆ์šฐ์ด์›ƒ๋•๊ธฐ (JAVA)

WonderJay 2023. 1. 20. 11:34
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

1414๋ฒˆ: ๋ถˆ์šฐ์ด์›ƒ๋•๊ธฐ

์ฒซ์งธ ์ค„์— ์ปดํ“จํ„ฐ์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ๋žœ์„ ์˜ ๊ธธ์ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. i๋ฒˆ์งธ ์ค„์˜ j๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ 0์ธ ๊ฒฝ์šฐ๋Š” ์ปดํ“จํ„ฐ i์™€ ์ปดํ“จํ„ฐ j๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋žœ์„ ์ด ์—†์Œ์„ ์˜๋ฏธํ•œ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ๋Š” ๋žœ์„ 

www.acmicpc.net

 

๋”๋ณด๊ธฐ

๋ฌธ์ œ

๋‹ค์†œ์ด๋Š” ๋ถˆ์šฐ์ด์›ƒ ๋•๊ธฐ ํ™œ๋™์„ ํ•˜๊ธฐ ์œ„ํ•ด ๋ฌด์—‡์„ ํ• ์ง€ ์ƒ๊ฐํ–ˆ๋‹ค. ๋งˆ์นจ ์ง‘์— ์—„์ฒญ๋‚˜๊ฒŒ ๋งŽ์€ ๋žœ์„ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์•˜๋‹ค. ๋งˆ์นจ ๋žœ์„ ์ด ์ด๋ ‡๊ฒŒ ๋งŽ์ด ํ•„์š” ์—†๋‹ค๊ณ  ๋Š๋‚€ ๋‹ค์†œ์ด๋Š” ๋žœ์„ ์„ ์ง€์—ญ์‚ฌํšŒ์— ๋ด‰์‚ฌํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

๋‹ค์†œ์ด์˜ ์ง‘์—๋Š” N๊ฐœ์˜ ๋ฐฉ์ด ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ฐฉ์—๋Š” ๋ชจ๋‘ ํ•œ ๊ฐœ์˜ ์ปดํ“จํ„ฐ๊ฐ€ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์ปดํ“จํ„ฐ๋Š” ๋žœ์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค. ์–ด๋–ค ์ปดํ“จํ„ฐ A์™€ ์ปดํ“จํ„ฐ B๊ฐ€ ์žˆ์„ ๋•Œ, A์™€ B๊ฐ€ ์„œ๋กœ ๋žœ์„ ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๊ฑฐ๋‚˜, ๋˜ ๋‹ค๋ฅธ ์ปดํ“จํ„ฐ๋ฅผ ํ†ตํ•ด์„œ ์—ฐ๊ฒฐ์ด ๋˜์–ด์žˆ์œผ๋ฉด ์„œ๋กœ ํ†ต์‹ ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์†œ์ด๋Š” ์ง‘ ์•ˆ์— ์žˆ๋Š” N๊ฐœ์˜ ์ปดํ“จํ„ฐ๋ฅผ ๋ชจ๋‘ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค.

N๊ฐœ์˜ ์ปดํ“จํ„ฐ๊ฐ€ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋žœ์„ ์˜ ๊ธธ์ด๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๋‹ค์†œ์ด๊ฐ€ ๊ธฐ๋ถ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋žœ์„ ์˜ ๊ธธ์ด์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ปดํ“จํ„ฐ์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ๋žœ์„ ์˜ ๊ธธ์ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. i๋ฒˆ์งธ ์ค„์˜ j๋ฒˆ์งธ ๋ฌธ์ž๊ฐ€ 0์ธ ๊ฒฝ์šฐ๋Š” ์ปดํ“จํ„ฐ i์™€ ์ปดํ“จํ„ฐ j๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๋žœ์„ ์ด ์—†์Œ์„ ์˜๋ฏธํ•œ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ๋Š” ๋žœ์„ ์˜ ๊ธธ์ด๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ๋žœ์„ ์˜ ๊ธธ์ด๋Š” a๋ถ€ํ„ฐ z๋Š” 1๋ถ€ํ„ฐ 26์„ ๋‚˜ํƒ€๋‚ด๊ณ , A๋ถ€ํ„ฐ Z๋Š” 27๋ถ€ํ„ฐ 52๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. N์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋‹ค์†œ์ด๊ฐ€ ๊ธฐ๋ถ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋žœ์„ ์˜ ๊ธธ์ด์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งŒ์•ฝ, ๋ชจ๋“  ์ปดํ“จํ„ฐ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1 

3
abc
def
ghi

์˜ˆ์ œ ์ถœ๋ ฅ 1 

40

์˜ˆ์ œ ์ž…๋ ฅ 2 

2
a0
0b

์˜ˆ์ œ ์ถœ๋ ฅ 2 

-1

์˜ˆ์ œ ์ž…๋ ฅ 3 

4
0X00
00Y0
0000
00Z0

์˜ˆ์ œ ์ถœ๋ ฅ 3 

0

์˜ˆ์ œ ์ž…๋ ฅ 4 

2
Az
aZ

์˜ˆ์ œ ์ถœ๋ ฅ 4 

105

์˜ˆ์ œ ์ž…๋ ฅ 5 

4
0top
c0od
er0o
pen0

์˜ˆ์ œ ์ถœ๋ ฅ 5 

134

์ „์ฒด ๋žœ์„  ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๊ณ ,

 

MST ์˜ ๋žœ์„  ๊ธธ์ด๋ฅผ ๋นผ์ฃผ๋ฉด ๋œ๋‹ค.

 

import javax.imageio.spi.ImageInputStreamSpi;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {
    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));
        int n = Integer.parseInt(br.readLine());
        int total_length = 0;
        PriorityQueue<Edge> pq = new PriorityQueue<>();
        for (int i = 0; i < n; i++) {
            char[] arr = br.readLine().toCharArray(); // ์ž…๋ ฅ ๋ฐ›์Œ
            for (int j = 0; j < n; j++) {
                if (arr[j] == '0')
                    continue;
                else{
                    int t = toLength(arr[j]);
                    total_length += t;
                    if(i!=j&&t!=0) pq.add(new Edge(i, j, toLength(arr[j])));
                }
            }
        }
        // ์ด์ œ mst ๋ฅผ ์ฐพ์ž.
        parent = new int[n + 1];
        for (int i = 0; i <= n; i++)
            parent[i] = i;
        int mst_length=0;
        int used=0;
        while (!pq.isEmpty()) {
            Edge now_edge = pq.poll();
            if(find(now_edge.st)!=find(now_edge.end)){
                union(now_edge.st, now_edge.end);
                mst_length+=now_edge.w;
                used++;
            }
        }
        if(used==n-1)
            System.out.println(total_length-mst_length);
        else System.out.println(-1);
    }

    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 (a == parent[a])
            return a;
        else return parent[a] = find(parent[a]);
    }

    static int toLength(char ele) {
        if (ele >= 'a' && ele <= 'z') {
            return ele - 'a' + 1;
        } else {
            return ele - 'A' + 27;
        }
    }

    static class Edge implements Comparable<Edge> {
        int st;
        int end;
        int w;

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

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

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