๊ธฐ๋ก๋ฐฉ

BOJ_11404 : ํ”Œ๋กœ์ด๋“œ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_11404 : ํ”Œ๋กœ์ด๋“œ

Soom_1n 2024. 1. 31. 16:05

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

 

11404๋ฒˆ: ํ”Œ๋กœ์ด๋“œ

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ n์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ m์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ m+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ ๋ฒ„์Šค์˜ ์ถœ๋ฐœ ๋„์‹œ์˜ ๋ฒˆํ˜ธ๊ฐ€

www.acmicpc.net



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

  • ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ธฐ์ดˆ ๋ฌธ์ œ์ด๋‹ค.

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

  • ๋‹จ๋ฐฉํ–ฅ ๊ฐ€์ค‘์น˜ ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„์—์„œ ๋ชจ๋“  ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•œ๋‹ค.

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

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(br.readLine());
        int m = Integer.parseInt(br.readLine());

        int[][] cost = new int[n + 1][n + 1];

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i != j)
                    cost[i][j] = Integer.MAX_VALUE;
            }
        }

        for (int i = 0; i < m; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            int c = Integer.parseInt(st.nextToken());
            cost[a][b] = Math.min(cost[a][b], c);
        }

        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    if (cost[i][k] != Integer.MAX_VALUE && cost[k][j] != Integer.MAX_VALUE)
                        cost[i][j] = Math.min(cost[i][j], cost[i][k] + cost[k][j]);
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (cost[i][j] == Integer.MAX_VALUE)
                    sb.append(0).append(' ');
                else
                    sb.append(cost[i][j]).append(' ');
            }
            sb.append('\n');
        }

        bw.write(sb.toString());
        bw.flush();
    }
}

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

  • n x n ํ–‰๋ ฌ์„ ๋งŒ๋“ค์–ด ๊ฐ€์ค‘์น˜๋ฅผ ์ €์žฅํ•œ๋‹ค.
    • ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ฒฐ์ด๋ฉฐ ์ž๊ธฐ ์ž์‹ ์€ 0, ์—ฐ๊ฒฐ์ด ์•ˆ๋˜์–ด ์žˆ์œผ๋ฉด Infinity๋กœ ์ €์žฅํ•œ๋‹ค.
  • k๋ฅผ ๊ฒฝ์œ ์ง€๋กœํ•˜๋Š” i -> j ์—ฐ๊ฒฐ์„ ๋ชจ๋‘ ํ™•์ธํ•ด ์ตœ์†Œ๊ฐ’์„ ๊ฐฑ์‹ ํ•œ๋‹ค.
  • ๋ชจ๋“  i -> j ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ๋ฌธ์ œ๋Š” ๋งŽ์ด ํ’€์ด๋ณด์ง€ ์•Š์•˜๋Š”๋ฐ, ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์•Œ๊ณ ๋‚˜๋ฉด ๊ฐ„๋‹จํžˆ ํ’€์ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
    • ํ•ต์‹ฌ์€ k๋ฅผ ๊ฒฝ์œ ์ง€๋กœ ๋‘๊ณ , ์ตœ์†Œ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ์ ํ™”์‹์ด์—ˆ๋‹ค.
  • ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ฒฐ์„ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ๋กœ ์ฐฉ๊ฐํ•ด 1ํšŒ ํ‹€๋ ธ๋‹ค.

 

 

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_1300 : K๋ฒˆ์งธ ์ˆ˜  (0) 2024.02.05
BOJ_2343 : ๊ธฐํƒ€ ๋ ˆ์Šจ  (0) 2024.02.05
BOJ_1517 : ๋ฒ„๋ธ” ์†ŒํŠธ  (0) 2024.01.26
BOJ_2638 : ์น˜์ฆˆ  (0) 2024.01.25
BOJ_5639 : ์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ  (0) 2023.12.15