01-09 04:37
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๋‹ค์ต์ŠคํŠธ๋ผ 1753 :: ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๋‹ค์ต์ŠคํŠธ๋ผ 1753 :: ์ตœ๋‹จ ๊ฒฝ๋กœ

miinsun 2022. 6. 9. 15:10

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ฃผ์–ด์ง„ ์‹œ์ž‘์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๋‹จ, ๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 10 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

  • ์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ V์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ E๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ V ≤ 20,000, 1 ≤ E ≤ 300,000)
  • ๋ชจ๋“  ์ •์ ์—๋Š” 1๋ถ€ํ„ฐ V๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค.
  • ๋‘˜์งธ ์ค„์—๋Š” ์‹œ์ž‘ ์ •์ ์˜ ๋ฒˆํ˜ธ K(1 ≤ K ≤ V)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
  • ์…‹์งธ ์ค„๋ถ€ํ„ฐ E๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ๊ฐ„์„ ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์„ธ ๊ฐœ์˜ ์ •์ˆ˜ (u, v, w)๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค.
    • ์ด๋Š” u์—์„œ v๋กœ ๊ฐ€๋Š” ๊ฐ€์ค‘์น˜ w์ธ ๊ฐ„์„ ์ด ์กด์žฌํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.
    • u์™€ v๋Š” ์„œ๋กœ ๋‹ค๋ฅด๋ฉฐ w๋Š” 10 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ด๋‹ค.
    • ์„œ๋กœ ๋‹ค๋ฅธ ๋‘ ์ •์  ์‚ฌ์ด์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ„์„ ์ด ์กด์žฌํ•  ์ˆ˜๋„ ์žˆ์Œ์— ์œ ์˜ํ•œ๋‹ค.

 

์ถœ๋ ฅ

  • ์ฒซ์งธ ์ค„๋ถ€ํ„ฐ V๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ, i๋ฒˆ์งธ ์ค„์— i๋ฒˆ ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ์˜ ๊ฒฝ๋กœ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • ์‹œ์ž‘์  ์ž์‹ ์€ 0์œผ๋กœ ์ถœ๋ ฅํ•˜๊ณ , ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์—๋Š” INF๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

5 6
1
5 1 1
1 2 2
1 3 3
2 3 4
2 4 5
3 4 6

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

0
2
3
7
INF

 

โ€‹

๐Ÿ’ป  Main.java

  • ๊ทธ๋ž˜ํ”„๋ฅผ 2์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„ํ•ด์ค€๋‹ค
  • ๊ฐฑ์‹  ๋œ ๋…ธ๋“œ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉ
    • ์ด๋•Œ ์šฐ์„  ์ˆœ์œ„ ํ๋ฅผ ํ•ญ์ƒ ์ตœ๋‹จ ๊ฒฝ๋กœ ์ˆœ์œผ๋กœ ์ •๋ ฌ์ด ๋ณด์žฅ๋œ๋‹ค
  • ์ตœ๋‹จ ๊ฒฝ๋กœ๋Š” ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด int ๋ฐฐ์—ด result๋ฅผ ์ƒ์„ฑํ•ด์คฌ๋‹ค
  •  
/* ๋ฐฑ์ค€ ๋‹ค์ต์ŠคํŠธ๋ผ - 1753 :: ์ตœ๋‹จ๊ฒฝ๋กœ */
import java.util.*;
import java.io.*;

class Node implements Comparable<Node>{
	int index;
	int distacne;

	Node(int index, int distacne) {
		this.index = index;
		this.distacne = distacne;
	}
	
	@Override
	public int compareTo(Node o) {
	return Integer.compare(this.distacne, o.distacne);
	}
}

public class Main {
	static final int INF = 9999999;
	static List<List<Node>> graph = new ArrayList<>();
	static int[] result;	
		
	public static void main(String[] args) throws IOException{
		Scanner sc = new Scanner (System.in);
		int v = sc.nextInt();
		int e = sc.nextInt();
		int k = sc.nextInt();
		
		for(int i = 0; i <= v; i++) {
			graph.add(new ArrayList<>());
		}
		
		result = new int[v + 1];
		Arrays.fill(result, INF);
		
		// ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์ž…๋ ฅ ๊ฐ’์— ๋”ฐ๋ผ ๊ทธ๋ž˜ํ”„ ์ดˆ๊ธฐํ™”
		for (int i = 0; i < e; i++) {
			graph.get(sc.nextInt()).add(new Node(sc.nextInt(), sc.nextInt()));
		}
		
		// ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜ํ–‰
		dijkstra(k);
		
		// ๋ฌธ์ œ์—์„œ ์ œ์‹œํ•œ ์กฐ๊ฑด์— ๋งž๊ฒŒ ์ถœ๋ ฅ
		for (int i = 1; i < result.length; i++) {
			if(result[i] == INF) {
				System.out.println("INF");	
			}else {
				System.out.println(result[i]);
			}
		}
        sc.close();
	}
	
	static void dijkstra(int start) {
		// ์ตœ๋‹จ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐฑ์‹  ๋œ ๋…ธ๋“œ๋“ค์„ ๋‹ด์„ ์šฐ์„ ์ˆœ์œ„ ํ ์ƒ์„ฑ
		PriorityQueue<Node> pq =  new PriorityQueue<>();
		result[start] = 0;
		
		pq.offer(new Node(start, result[start]));
		
		while(!pq.isEmpty()) {
			Node node = pq.poll();
			int nodeIndex = node.index;
			int distance = node.distacne;
			
			// ์ค‘๋ณต ๋ฐฉ๋ฌธ ๊ฒ€์‚ฌ
			if(distance > result[nodeIndex]) {
				continue;
			}
			
			for (Node linkedNode : graph.get(nodeIndex)) {
				// ์ƒˆ๋กœ ์ฐพ์€ ๊ฐ’์ด ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฉด, ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ๊ฐฑ์‹ ํ•ด์ฃผ๊ธฐ
				if(distance + linkedNode.distacne < result[linkedNode.index]) {
					result[linkedNode.index] = distance + linkedNode.distacne;
					pq.offer(new Node(linkedNode.index, result[linkedNode.index]));
				}
			}
		}
	}
}
Comments