๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1238_ํŒŒํ‹ฐ

๋ฟŒ์•ผ._. 2024. 1. 8. 15:21

Gold III

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

< ํŒŒํ‹ฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

๋ฐ์ดํฌ์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์„œ ๋จผ์ € n๋ฒˆ์งธ ๋งˆ์„์—์„œ x๋ฒˆ์งธ ๋งˆ์„๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค. ๊ทธ ํ›„์— ๋‹ค์‹œ x๋ฒˆ์งธ ๋งˆ์„์—์„œ n๋ฒˆ์งธ ๋งˆ์„๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค.

 

 my solution (Java)

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

public class _1238_ { // ํŒŒํ‹ฐ

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

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

		ArrayList<ArrayList<int[]>> list = new ArrayList<>();
		for (int i = 0; i < n + 1; i++) {
			list.add(new ArrayList<>());
		}

		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 t = Integer.parseInt(st.nextToken());

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

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

			@Override
			public int compare(int[] o1, int[] o2) {
				return o1[1] - o2[1];
			}
		});

		int answer = 0;

		for (int i = 1; i <= n; i++) {
			int arr[] = new int[n + 1];
			int result[] = new int[n + 1];

			for (int j = 1; j < n + 1; j++) {
				arr[j] = Integer.MAX_VALUE;
				result[j] = Integer.MAX_VALUE;
			}

			if (i == x) {
				continue;
			}

			arr[i] = 0;
			queue.add(new int[] { i, 0 });

			while (!queue.isEmpty()) {
				int num[] = queue.poll();
				for (int j = 0; j < list.get(num[0]).size(); j++) {
					if (arr[list.get(num[0]).get(j)[0]] > num[1] + list.get(num[0]).get(j)[1]) {
						arr[list.get(num[0]).get(j)[0]] = num[1] + list.get(num[0]).get(j)[1];
						queue.add(new int[] { list.get(num[0]).get(j)[0], num[1] + list.get(num[0]).get(j)[1] });
					}
				}
			}

			result[x] = arr[x];
			queue.add(new int[] { x, result[x] });
			while (!queue.isEmpty()) {
				int num[] = queue.poll();
				for (int j = 0; j < list.get(num[0]).size(); j++) {
					if (result[list.get(num[0]).get(j)[0]] > num[1] + list.get(num[0]).get(j)[1]) {
						result[list.get(num[0]).get(j)[0]] = num[1] + list.get(num[0]).get(j)[1];
						queue.add(new int[] { list.get(num[0]).get(j)[0], num[1] + list.get(num[0]).get(j)[1] });
					}
				}
			}

			if (answer < result[i]) {
				answer = result[i];
			}
		}
		System.out.println(answer);
	}
}
๋ณ€์ˆ˜)
n, m, x : ํ•™์ƒ ์ˆ˜, ๋‹จ๋ฐฉํ–ฅ ๋„๋กœ ๊ฐœ์ˆ˜, ํŒŒํ‹ฐํ•˜๋Š” ๋งˆ์„
list : ๊ฐ ๋งˆ์„์— ์—ฐ๊ฒฐ๋œ ๋„๋กœ
start, end, t : ์‹œ์ž‘์ , ๋์ , ํ•„์š”ํ•œ ์†Œ์š” ์‹œ๊ฐ„
queue : ์šฐ์„ ์ˆœ์œ„ ํ [๋งˆ์„ ๋ฒˆํ˜ธ, ์‹œ๊ฐ„]
answer : ์˜ค๊ณ  ๊ฐ€๋Š”๋ฐ ๊ฐ€์žฅ ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๋Š” ํ•™์ƒ์˜ ์†Œ์š” ์‹œ๊ฐ„
arr : n๋ฒˆ์งธ ๋งˆ์„ -> x๋ฒˆ์งธ ๋งˆ์„ ์ตœ๋‹จ ์‹œ๊ฐ„
result : x๋ฒˆ์งธ ๋งˆ์„ -> n๋ฒˆ์งธ ๋งˆ์„ ์ตœ๋‹จ ์‹œ๊ฐ„

 

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

 

๋จผ์ € ๋ฐฐ์—ด์„ Integer.MAX_VALUE๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. x๋ฒˆ ๋งˆ์„์— ์žˆ๋Š” ํ•™์ƒ์„ ์ตœ๋‹จ ์‹œ๊ฐ„์„ ๊ตฌํ•  ํ•„์š” ์—†์œผ๋ฏ€๋กœ ๋„˜์–ด๊ฐ„๋‹ค. i๋ฒˆ์งธ ํ•™์ƒ์ด x๋ฒˆ ๋งˆ์„๋กœ ๊ฐ€๋Š”๋ฐ ์ตœ๋‹จ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค. ๊ทธ ํ›„ x๋ฒˆ ๋งˆ์„์—์„œ i๋ฒˆ ๋งˆ์„๋กœ ๋Œ์•„๊ฐ€๋Š” ์ตœ๋‹จ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค. i๋ฒˆ ๋งˆ์„๋กœ ๋Œ์•„์˜จ ๊ฐ’ ์ค‘์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ตฌํ•œ๋‹ค.