๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

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

๋ฟŒ์•ผ._. 2023. 11. 10. 14:32

Gold V

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

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

 

๋ฌธ์ œ ํ’€์ด 

 

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ตœ์†Œ ๋น„์šฉ์„ ์ฐพ๋Š”๋‹ค.

 

 

 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 _1916_ { // ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ
	static ArrayList<ArrayList<City>> list;
	static boolean visited[];
	static int result[];

	static class City {
		private int num;
		private int money;

		public City(int num, int money) {
			this.num = num;
			this.money = money;
		}
	}

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

		int n = Integer.parseInt(bf.readLine());
		int m = Integer.parseInt(bf.readLine());

		list = new ArrayList<>();
		visited = new boolean[n + 1];
		result = new int[n + 1];

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

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

		bfs(start, end);

		System.out.println(result[end]);
	}

	private static void bfs(int start, int end) {
		PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return o1[1] - o2[1];
			}
		});
		queue.add(new int[] { start, 0 });
		visited[start] = true;

		while (!queue.isEmpty()) {
			int temp[] = queue.poll();
			if (result[temp[0]] < temp[1])
				continue;
			for (int i = 0; i < list.get(temp[0]).size(); i++) {
				City city = list.get(temp[0]).get(i);
				if (!visited[city.num] || result[city.num] > temp[1] + city.money) {
					visited[city.num] = true;
					result[city.num] = temp[1] + city.money;
					queue.add(new int[] { city.num, result[city.num] });
				}
			}
		}
	}
}

 

Main

๋ณ€์ˆ˜)
City : ๋„์‹œ ๋ฒˆํ˜ธ, ๋น„์šฉ
n : ๋„์‹œ ๊ฐœ์ˆ˜
m : ๋ฒ„์Šค ๊ฐœ์ˆ˜
list : ๋„์‹œ ์—ฐ๊ฒฐ๋œ ๋ฒ„์Šค
visited : ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
result : ์ตœ์†Œ ๋น„์šฉ
start, end, money : ์‹œ์ž‘ ๋„์‹œ, ๋„์ฐฉ ๋„์‹œ, ๋น„์šฉ
queue : ์šฐ์„ ์ˆœ์œ„ ํ <๋„์‹œ ๋ฒˆํ˜ธ, ๋น„์šฉ>

 

๋„์‹œ ๊ฐœ์ˆ˜(n), ๋ฒ„์Šค ๊ฐœ์ˆ˜(m)๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋ฒ„์Šค ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ํ›„ list์— ๋„์‹œ ๋ฒˆํ˜ธ์™€ ๋น„์šฉ์„ ์ €์žฅํ•ด ๋‘”๋‹ค. ์ถœ๋ฐœ์ ์˜ ๋ฒˆํ˜ธ์™€ ๋„์ฐฉ์ ์˜ ๋„์‹œ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ํ›„ bfs ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถœ๋ฐœ ๋„์‹œ ๋ฒˆํ˜ธ์™€ ๋น„์šฉ์„ ์ €์žฅํ•˜๋ฉฐ ๋ฐฉ๋ฌธ ํ‘œ์‹œํ•œ๋‹ค. queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉฐ ํ˜„์žฌ ๋น„์šฉ์ด ํ˜„์žฌ ๋„์‹œ๊นŒ์ง€์˜ ๋น„์šฉ๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ณ„์‚ฐํ•  ํ•„์š” ์—†์œผ๋ฏ€๋กœ ๋„˜์–ด๊ฐ„๋‹ค. ํ˜„์žฌ ๋น„์šฉ์ด ํ˜„์žฌ ๋„์‹œ ๋น„์šฉ๊ณผ ๊ฐ™๊ฑฐ๋‚˜ ์ž‘๋‹ค๋ฉด ๋„์‹œ์™€ ์—ฐ๊ฒฐ๋œ ๋ฒ„์Šค๋ฅผ ์ˆœํ™˜ํ•œ๋‹ค. ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ ์ตœ์†Œ ๋น„์šฉ์ด๋ผ๋ฉด ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•ด ์ค€ ํ›„ queue์— ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค.

 

์ตœ์†Œ ๋น„์šฉ์„ ์ถœ๋ ฅํ•œ๋‹ค.