๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 10282_ํ•ดํ‚น

๋ฟŒ์•ผ._. 2023. 12. 25. 14:57

Gold IV

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

< ํ•ดํ‚น >

 

๋ฌธ์ œ ํ’€์ด 

 

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ปดํ“จํ„ฐ๊ฐ€ ๊ฐ์—ผ๋˜๋Š”๋ฐ ์ตœ์†Œ ์‹œ๊ฐ„์„ ๊ตฌํ•œ๋‹ค. 

 

 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 _10282_ { // ํ•ดํ‚น

	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;

		int t = Integer.parseInt(bf.readLine());

		ArrayList<ArrayList<int[]>> list = new ArrayList<>();

		for (int i = 0; i < t; i++) {
			st = new StringTokenizer(bf.readLine());

			int n = Integer.parseInt(st.nextToken());
			int d = Integer.parseInt(st.nextToken());
			int c = Integer.parseInt(st.nextToken());

			for (int j = 0; j <= n; j++) {
				list.add(new ArrayList<>());
			}

			for (int j = 0; j < d; j++) {
				st = new StringTokenizer(bf.readLine());

				int a = Integer.parseInt(st.nextToken());
				int b = Integer.parseInt(st.nextToken());
				int s = Integer.parseInt(st.nextToken());

				list.get(b).add(new int[] { a, s });
			}

			int dist[] = new int[n + 1];
			for (int j = 1; j <= n; j++) {
				dist[j] = Integer.MAX_VALUE;
			}
			dist[c] = 0;

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

				@Override
				public int compare(int[] o1, int[] o2) {
					if (o1[1] == o2[1]) {
						return o1[0] - o2[0];
					}
					return o1[1] - o2[1];
				}
			});
			queue.add(new int[] { c, 0 });

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

			int cnt = 0;
			int time = 0;
			for (int j = 1; j <= n; j++) {
				if (dist[j] != Integer.MAX_VALUE) {
					cnt += 1;
					if (time < dist[j]) {
						time = dist[j];
					}
				}
			}
			bw.write(cnt+" "+time+"\n");
			list.clear();
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
t : ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๊ฐœ์ˆ˜
list : ์ปดํ“จํ„ฐ ์˜์กด ๊ทธ๋ž˜ํ”„
n, d, c : ์ปดํ“จํ„ฐ ๊ฐœ์ˆ˜, ์˜์กด์„ฑ ๊ฐœ์ˆ˜, ํ•ดํ‚น๋‹นํ•œ ์ปดํ“จํ„ฐ์˜ ๋ฒˆํ˜ธ
a, b, s : a๊ฐ€ b๋ฅผ ์˜์กด, s์ดˆ
dist : ๊ฐ์—ผ๋˜๊ธฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
queue : ์šฐ์„ ์ˆœ์œ„ ํ
cnt, time : ๊ฐ์—ผ๋˜๋Š” ์ปดํ“จํ„ฐ ์ˆ˜, ๋งˆ์ง€๋ง‰ ์ปดํ“จํ„ฐ๊ฐ€ ๊ฐ์—ผ๋˜๊ธฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„

 

์ปดํ“จํ„ฐ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์˜์กด ๊ทธ๋ž˜ํ”„๋ฅผ ArrayList์— ์ €์žฅํ•œ๋‹ค. ์ปดํ“จํ„ฐ๊ฐ€ ๊ฐ์—ผ๋˜๊ธฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด Integer.MAX_VALUE๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ํ•ดํ‚น๋‹นํ•œ ์ปดํ“จํ„ฐ์˜ ์‹œ๊ฐ„์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ ํ›„ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅํ•œ๋‹ค. ์šฐ์„ ์ˆœ์œ„ ํ๋Š” ์‹œ๊ฐ„์ด ์ ์€ ๊ฒƒ์„ ์šฐ์„ ์ˆœ์œ„๋กœ ํ•œ๋‹ค. ๊ทธ ํ›„ ์˜์กด๊ด€๊ณ„๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋œ ์ปดํ“จํ„ฐ๋ฅผ ํ•ดํ‚นํ•˜๋Š”๋ฐ ์ตœ์†Œ ์‹œ๊ฐ„์œผ๋กœ ์—…๋ฐ์ดํŠธ ๊ฐ€๋Šฅํ•  ๋•Œ ํ์— ๋„ฃ๋Š”๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‹ต์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด dist ๋ฐฐ์—ด์„ ํ™•์ธํ•˜๋ฉฐ Integer.MAX_VALUE๊ฐ€ ์•„๋‹Œ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ์ปดํ“จํ„ฐ๊ฐ€ ๊ฐ์—ผ๋˜๊ธฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ•ด ์ถœ๋ ฅํ•œ๋‹ค.