๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 11779_์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ 2

๋ฟŒ์•ผ._. 2024. 1. 9. 17:07

Gold III

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

< ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ 2 >

 

๋ฌธ์ œ ํ’€์ด 

 

๋ฐ์ดํฌ์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์„œ ์ตœ์†Œ ๋น„์šฉ์„ ๊ตฌํ•œ๋‹ค.

 

 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 _11779_ { // ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ 2
	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 n = Integer.parseInt(bf.readLine());
		int m = Integer.parseInt(bf.readLine());

		ArrayList<ArrayList<int[]>> list = new ArrayList<>();
		Node result[] = new Node[n + 1];
		for (int i = 0; i <= n; i++) {
			list.add(new ArrayList<>());
			result[i] = new Node(i, Integer.MAX_VALUE, 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 cost = Integer.parseInt(st.nextToken());
			list.get(start).add(new int[] { end, cost });
		}

		st = new StringTokenizer(bf.readLine());
		int start = Integer.parseInt(st.nextToken());
		int end = Integer.parseInt(st.nextToken());

		PriorityQueue<Node> queue = new PriorityQueue<>();
		ArrayList<Integer> temp = new ArrayList<>();
		temp.add(start);
		queue.add(new Node(start, 0, temp));

		while (!queue.isEmpty()) {
			Node node = queue.poll();
			if(node.d > result[node.node].d) {
				continue;
			}
			for (int i = 0; i < list.get(node.node).size(); i++) {
				int info[] = list.get(node.node).get(i);
				if (node.d + info[1] < result[info[0]].d) {
					ArrayList<Integer> route = new ArrayList<>();
					result[info[0]].d = node.d + info[1];
					result[info[0]].list.clear();
					for (int j = 0; j < node.list.size(); j++) {
						route.add(node.list.get(j));
						result[info[0]].list.add(node.list.get(j));
					}
					result[info[0]].list.add(info[0]);
					route.add(info[0]);

					queue.add(new Node(info[0], node.d + info[1], route));
				}
			}
		}

		bw.write(result[end].d + "\n");
		bw.write(result[end].list.size() + "\n");
		for (int i = 0; i < result[end].list.size(); i++) {
			bw.write(result[end].list.get(i) + " ");
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
Node : ๋„์‹œ ๋ฒˆํ˜ธ, ๋น„์šฉ, ๊ฒฝ๋กœ
n, m : ๋„์‹œ์˜ ๊ฐœ์ˆ˜, ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜
list : ๋ฒ„์Šค์˜ ์ •๋ณด
result : ์ถœ๋ฐœ ๋„์‹œ์—์„œ ๊ฐ ๋„์‹œ๋กœ ๊ฐ€๋Š” ์ตœ์†Œ ๋น„์šฉ
start, end cost : ์ถœ๋ฐœ ๋„์‹œ ๋ฒˆํ˜ธ, ๋„์ฐฉ์ง€์˜ ๋„์‹œ ๋ฒˆํ˜ธ, ๋ฒ„์Šค ๋น„์šฉ
start, end : ์ถœ๋ฐœ์ ์˜ ๋„์‹œ ๋ฒˆํ˜ธ, ๋„์ฐฉ์ ์˜ ๋„์‹œ ๋ฒˆํ˜ธ
queue : ์šฐ์„ ์ˆœ์œ„ ํ 
temp : ์ถœ๋ฐœ์ ๋ถ€ํ„ฐ ๊ฒฝ๋กœ ์ €์žฅํ•˜๋Š” ArrayList

 

Node

๋„์‹œ ๋ฒˆํ˜ธ, ๋ฒ„์Šค ๋น„์šฉ, ๊ฒฝ๋กœ๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง„๋‹ค. ์šฐ์„ ์ˆœ์œ„๋Š” ๋น„์šฉ์ด ์ž‘์€ ๊ฒƒ์„ ์šฐ์„ ์œผ๋กœ ํ•œ๋‹ค.

 

Main

๋„์‹œ์˜ ๊ฐœ์ˆ˜์™€ ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋ฒ„์Šค์˜ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ArrayList์— ์ €์žฅํ•œ๋‹ค. 

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

 

์ตœ์ข… ์ถœ๋ฐœ ๋„์‹œ์—์„œ ๋„์ฐฉ ๋„์‹œ๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๋“œ๋Š” ์ตœ์†Œ ๋น„์šฉ๊ณผ ๊ฒฝ๋กœ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋„์‹œ์˜ ๊ฐœ์ˆ˜, ๋ฐฉ๋ฌธํ•˜๋Š” ๋„์‹œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค.