๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1719_ํƒ๋ฐฐ

๋ฟŒ์•ผ._. 2024. 1. 10. 17:48

Gold III

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1719)

< ํƒ๋ฐฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

๋ฐ์ดํฌ์ŠคํŠธ๋ผ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class _1719_ { // ํƒ๋ฐฐ

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		int dist[][] = new int[n + 1][n + 1];
		int answer[][] = new int[n + 1][n + 1];
		ArrayList<ArrayList<int[]>> list = new ArrayList<>();

		for (int i = 0; i < n + 1; i++) {
			list.add(new ArrayList<>());
			if (i == 0) {
				continue;
			}
			for (int j = 0; j < n + 1; j++) {
				if (j == 0) {
					continue;
				}
				dist[i][j] = Integer.MAX_VALUE;
			}
		}

		for (int i = 0; i < m; i++) {
			st = new StringTokenizer(bf.readLine());
			int start = Integer.parseInt(st.nextToken());
			int end = Integer.parseInt(st.nextToken());
			int d = Integer.parseInt(st.nextToken());

			list.get(start).add(new int[] { end, d });
			list.get(end).add(new int[] { start, d });
		}

		PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {

			@Override
			public int compare(int[] o1, int[] o2) {
				return o1[1] - o2[1];
			}
		});
		for (int i = 1; i <= n; i++) {
			queue.add(new int[] { i, 0 });
			while (!queue.isEmpty()) {
				int info[] = queue.poll();
				if (info[1] > dist[i][info[0]]) {
					continue;
				}
				for (int j = 0; j < list.get(info[0]).size(); j++) {
					int temp[] = list.get(info[0]).get(j);
					if (temp[0] == i)
						continue;
					if (info[1] + temp[1] < dist[i][temp[0]]) {
						dist[i][temp[0]] = info[1] + temp[1];
						queue.add(new int[] { temp[0], info[1] + temp[1] });
						if (answer[i][info[0]] == 0)
							answer[i][temp[0]] = temp[0];
						else
							answer[i][temp[0]] = answer[i][info[0]];
					}
				}
			}
		}
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (i == j)
					bw.write("- ");
				else
					bw.write(answer[i][j] + " ");
			}
			bw.write("\n");
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
n, m : ์ง‘ํ•˜์žฅ์˜ ๊ฐœ์ˆ˜, ์ง‘ํ•˜์žฅ๊ฐ„ ๊ฒฝ๋กœ์˜ ๊ฐœ์ˆ˜
dist : ์ตœ๋‹จ ์‹œ๊ฐ„
answer : ๊ฒฝ๋กœํ‘œ
list : ๋‘ ์ง‘ํ•˜์žฅ ๊ด€๊ณ„
start, end, d : ๋‘ ์ง‘ํ•˜์žฅ์˜ ๋ฒˆํ˜ธ, ๊ทธ ์‚ฌ์ด๋ฅผ ์˜ค๊ฐ€๋Š”๋ฐ ํ•„์š”ํ•œ ์‹œ๊ฐ„
queue : ์šฐ์„ ์ˆœ์œ„ ํ [์ง‘ํ•˜์žฅ ๋ฒˆํ˜ธ, ์‹œ๊ฐ„]

 

์ง‘ํ•˜์žฅ์˜ ๊ฐœ์ˆ˜์™€ ์ง‘ํ•˜์žฅ๊ฐ„ ๊ฒฝ๋กœ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์ง‘ํ•˜์žฅ๊ฐ„ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ArrayList์— ์ €์žฅํ•œ๋‹ค. ์šฐ์„ ์ˆœ์œ„ ํ์—๋Š” [์ง‘ํ•˜์žฅ ๋ฒˆํ˜ธ, ์‹œ๊ฐ„]์„ ์ €์žฅํ•˜๋ฉฐ ์‹œ๊ฐ„์ด ์งง์€ ๊ฒƒ์„ ์šฐ์„ ์ˆœ์œ„๋กœ ๋‘”๋‹ค. ๋ชจ๋“  ์ง‘ํ•˜์žฅ์—์„œ ๋‹ค๋ฅธ ์ง‘ํ•˜์žฅ์œผ๋กœ ๊ฐˆ ๋•Œ ๊ฐ€์žฅ ๋จผ์ € ๊ฑฐ์ณ์•ผ ํ•˜๋Š” ์ง‘ํ•˜์žฅ์„ ๊ตฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ์ „์ฒด ๊ณ„์‚ฐํ•œ๋‹ค.

 

์ฒ˜์Œ์—๋Š” ์‹œ์ž‘ ์ง‘ํ•˜์žฅ์„ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅํ•œ๋‹ค. ๊ทธ ํ›„ ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. ํ์—์„œ ๊ฐ’์„ ํ•˜๋‚˜ ๊บผ๋‚ด์™€ ๊ทธ ์‹œ๊ฐ„์ด ํ˜„์žฌ ์ €์žฅ๋œ ์‹œ๊ฐ„๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ณ„์‚ฐํ•  ํ•„์š” ์—†์œผ๋ฏ€๋กœ ๋‹ค์Œ์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค. ๋งŒ์•ฝ ์‹œ๊ฐ„์ด ์ ๋‹ค๋ฉด ๊ทธ ์ง‘ํ•˜์žฅ๊ณผ ์—ฐ๊ฒฐ๋œ ์ง‘ํ•˜์žฅ์„ ํƒ์ƒ‰ํ•œ๋‹ค. ์—ฐ๊ฒฐ๋œ ์ง‘ํ•˜์žฅ์œผ๋กœ ์ด๋™ํ–ˆ์„ ๋•Œ ์ตœ๋‹จ์‹œ๊ฐ„์œผ๋กœ ์—…๋ฐ์ดํŠธ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด ์—…๋ฐ์ดํŠธ ํ›„ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅํ•œ๋‹ค. ๋˜ํ•œ, ๊ฐ€์žฅ ๋จผ์ € ๊ฑฐ์ณ์•ผ ํ•˜๋Š” ์ง‘ํ•˜์žฅ์„ ๊ตฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ answer ๋ฐฐ์—ด์—๋„ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. ์ด๋•Œ, ๋งŒ์•ฝ answer ๊ฐ’์ด 0์ด๋ผ๋ฉด ์•„์ง ๋‹ค๋ฅธ ์ง‘ํ•˜์žฅ์„ ๊ฑฐ์น˜์ง€ ์•Š์€ ๊ฒƒ์ด๋ฏ€๋กœ ์ž๊ธฐ ์ž์‹ ์„ ๋„ฃ๊ณ , ๋‹ค๋ฅธ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ๊ทธ ์ง‘ํ•˜์žฅ์„ ๊ฑฐ์นœ ํ›„์— ๊ฑฐ์ณ์•ผ ํ•˜๋ฏ€๋กœ ๊ทธ ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.

 

์ตœ์ข… answer ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.