๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 9694_๋ฌด์—‡์„ ์•„๋Š๋ƒ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ˆ„๊ตฌ๋ฅผ ์•„๋Š๋ƒ๊ฐ€ ๋ฌธ์ œ๋‹ค

๋ฟŒ์•ผ._. 2024. 1. 11. 11:08

Gold III

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

< ๋ฌด์—‡์„ ์•„๋Š๋ƒ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ˆ„๊ตฌ๋ฅผ ์•„๋Š๋ƒ๊ฐ€ ๋ฌธ์ œ๋‹ค >

 

๋ฌธ์ œ ํ’€์ด 

 

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

 

 

 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.PriorityQueue;
import java.util.StringTokenizer;

public class _9694_ { // ๋ฌด์—‡์„ ์•„๋Š๋ƒ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ˆ„๊ตฌ๋ฅผ ์•„๋Š๋ƒ๊ฐ€ ๋ฌธ์ œ๋‹ค
	static class Node implements Comparable<Node> {
		private int node;
		private int d;
		private ArrayList<Integer> list;

		public Node(int node, int d, ArrayList<Integer> list) {
			this.node = node;
			this.d = d;
			this.list = list;
		}

		@Override
		public int compareTo(Node o) {
			return this.d - o.d;
		}
	}

	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<>();
		Node answer[];

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

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

			answer = new Node[m];
			for (int j = 0; j < m; j++) {
				list.add(new ArrayList<>());
				ArrayList<Integer> temp = new ArrayList<>();
				temp.add(j);
				answer[j] = new Node(j, Integer.MAX_VALUE, temp);
			}

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

				int x = Integer.parseInt(st.nextToken());
				int y = Integer.parseInt(st.nextToken());
				int z = Integer.parseInt(st.nextToken());

				list.get(x).add(new int[] { y, z });
				list.get(y).add(new int[] { x, z });
			}

			PriorityQueue<Node> queue = new PriorityQueue<>();
			ArrayList<Integer> temp = new ArrayList<>();
			temp.add(0);
			answer[0].d = 0;
			queue.add(new Node(0, 0, temp));

			while (!queue.isEmpty()) {
				Node node = queue.poll();

				if (answer[node.node].d < node.d) {
					continue;
				}
				for (int j = 0; j < list.get(node.node).size(); j++) {
					int info[] = list.get(node.node).get(j);
					if (answer[info[0]].d > node.d + info[1]) {
						answer[info[0]].d = node.d + info[1];
						answer[info[0]].list.clear();
						ArrayList<Integer> route = new ArrayList<>();
						for (int k = 0; k < node.list.size(); k++) {
							answer[info[0]].list.add(node.list.get(k));
							route.add(node.list.get(k));
						}
						answer[info[0]].list.add(info[0]);
						route.add(info[0]);
						queue.add(new Node(info[0], node.d + info[1], route));
					}
				}
			}

			bw.write("Case #" + (i + 1) + ": ");

			if (answer[m - 1].d == Integer.MAX_VALUE) {
				bw.write("-1\n");
			} else {
				for (int j = 0; j < answer[m - 1].list.size(); j++) {
					bw.write(answer[m - 1].list.get(j) + " ");
				}
				bw.write("\n");
			}
			list.clear();
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
Node : ๋ฒˆํ˜ธ, ์นœ๋ฐ€๋„, ๋งŒ๋‚œ ์ˆœ์„œ
t : ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ˆ˜
list : ์ •์น˜์ธ๋“ค ๊ฐ„์˜ ์นœ๋ฐ€๋„ ๊ด€๊ณ„
answer : 0๋ฒˆ์—์„œ ๊ฐ ์ •์น˜์ธ๊นŒ์ง€ ์นœ๋ฐ€๋„
n, m : ๊ด€๊ณ„์˜ ๊ฐœ์ˆ˜, ์ •์น˜์ธ์˜ ์ˆ˜
x, y, z : ์ •์น˜์ธ x, ๊ทธ์˜ ์นœ๊ตฌ y, ๋‘ ์‚ฌ๋žŒ ๊ฐ„์˜ ์นœ๋ฐ€๋„
queue : Node๋ฅผ ๊ฐ€์ง„ ์šฐ์„ ์ˆœ์œ„ ํ (์นœ๋ฐ€๋„๊ฐ€ ๋‚ฎ์€ ๊ฒƒ์„ ์šฐ์„ ์ˆœ์œ„๋กœ ๊ฐ€์ง)

 

Node

์ •์น˜์ธ์˜ ๋ฒˆํ˜ธ, ์นœ๋ฐ€๋„, ๋งŒ๋‚œ ์ˆœ์„œ๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง. ์šฐ์„ ์ˆœ์œ„๋Š” ์นœ๋ฐ€๋„๊ฐ€ ๋‚ฎ์€ ๊ฒƒ์„ ์šฐ์„ ์œผ๋กœ ํ•จ

 

Main

ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ˆ˜๋งŒํผ 0๋ฒˆ์—์„œ m-1๋ฒˆ๊นŒ์ง€ ๋‚ฎ์€ ์นœ๋ฐ€๋„๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ๋งŒ๋‚œ ์ˆœ์„œ๋ฅผ ๊ตฌํ•œ๋‹ค.

์ •์น˜์ธ๋“ค ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ list์— ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์ €์žฅํ•œ๋‹ค. 

์šฐ์„ ์ˆœ์œ„ ํ์— 0๋ฒˆ์„ ๋„ฃ์–ด ์นœ๋ฐ€๋„๋ฅผ ๊ตฌํ•œ๋‹ค. ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ ๊ฐ’์„ ๊บผ๋‚ด ์ž์‹ ๊ณผ ์—ฐ๊ฒฐ๋œ ๋‹ค๋ฅธ ์ •์น˜์ธ๋“ค์„ ์‚ดํŽด๋ณด๋ฉฐ ์นœ๋ฐ€๋„์˜ ํ•ฉ์ด ํ˜„์žฌ ์ €์žฅ๋œ ์นœ๋ฐ€๋„๋ณด๋‹ค ์ž‘์€ ๊ฐ’์ด๋ผ๋ฉด ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๋ฉฐ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅํ•œ๋‹ค.

 

answer์˜ m-1๋ฒˆ์งธ ์นœ๋ฐ€๋„ ๊ฐ’์ด Integer.MAX_VALUE๋ผ๋ฉด ์ตœ๊ณ  ์˜์›์„ ๋งŒ๋‚  ์ˆ˜ ์—†๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ -1์„ ์ถœ๋ ฅํ•˜๊ณ  Integer.MAX_VALUE๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด answer์˜ m-1๋ฒˆ์งธ์— ์ €์žฅ๋œ list์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 10384_ํŒฌ๊ทธ๋žจ  (0) 2024.01.15
[Baekjoon] 1865_์›œํ™€  (0) 2024.01.12
[Baekjoon] 1719_ํƒ๋ฐฐ  (0) 2024.01.10
[Baekjoon] 11779_์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ 2  (0) 2024.01.09
[Baekjoon] 1238_ํŒŒํ‹ฐ  (1) 2024.01.08