๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฌธ์ œ

[์ž๋ฐ”] ๋ฐฑ์ค€ 1753 - ์ตœ๋‹จ๊ฒฝ๋กœ

by applemango2021 2020. 12. 6.

๋‹ค์ต์ŠคํŠธ๋ผ ๋•Œ๋ถ€ํ„ฐ์˜€๋˜๊ฐ€์š”..? ์ œ๊ฐ€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํฌ๊ธฐํ•œ ๊ฒŒ...

 

๊ทธ๋ž˜ํ”„์™€ ์—ฐ๊ฒฐ๋œ ๊ธฐ๋ฒ• ์ค‘์— ๊ฐ€์žฅ ๊ธฐ์ดˆ๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ฒ˜์Œ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ ํ’€ ๋•Œ๋Š” ์ดํ•ด ์•ˆ ๊ฐ€์„œ ๋ฏธ์น˜๋Š” ์ค„ ์•Œ์•˜๋‹ค. 

๋Œ€์ฒด ์–ด๋–ค ์‚ฌ๋žŒ์ด๊ธธ๋ž˜ ์ด๋Ÿฐ ์ƒ๊ฐ์„ ํ•˜๋Š” ๊ฑด์ง€๋„ ๋„ˆ๋ฌด ์‹ ๊ธฐํ–ˆ๊ณ .

๊ทผ๋ฐ ๊ฑฐ์˜ 3๋…„๋งŒ์— ๋‹ค์ต์ŠคํŠธ๋ผ ๋‹ค์‹œ ๋ณด๋Š”๋ฐ ์ง€๊ธˆ๋„ ์ดํ•ด๊ฐ€ ์•ˆ ๊ฐ„๋‹ค ํ•˜ํ•ณํ•˜ํ•˜

 

๊ฐœ๋… ๋˜์งš์–ด ๋ณด๋ ค๊ณ  ์•„๋ž˜ ๋ธ”๋กœ๊ทธ ์ฐธ๊ณ ํ–ˆ๋‹ค.

bumbums.tistory.com/4

 

์ž๋ฐ”๋กœ ๋งŒ๋“œ๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ (dijkstra) ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ทธ๋ž˜ํ”„์—์„œ ์ถœ๋ฐœ์ ์—์„œ ๋ชฉํ‘œ์ ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž…๋‹ˆ๋‹ค. ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ณ€์ˆ˜๋Š” ๋‘๊ฐœ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. int distance[] = new int[n+1]

bumbums.tistory.com

๋‹ค์ต์ŠคํŠธ๋ผ๋Š” ์‹œ์ž‘์ ์ด ์ •ํ•ด์ ธ์žˆ๊ณ , ํ•ด๋‹น ์‹œ์ž‘์ ์—์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋กœ ๋ชฉํ‘œ์ ์— ๋„๋‹ฌํ•˜๋Š” ๋ฌธ์ œ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ผ๋‹จ ์‹œ์ž‘์ ๊ณผ ์—ฐ๊ฒฐ๋œ Node๋“ค์„ ์ฐพ์•„์„œ ๊ทธ Node๋“ค๊ณผ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด์— ๋„ฃ๊ณ , 

๋‹ค์‹œ ๊ทธ Node์™€ ์—ฐ๊ฒฐ๋œ ๋‹ค๋ฅธ Node๋“ค์„ ์ฐพ์•„์„œ ๊ทธ Node๋“ค๊ณผ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์ฐพ๋Š” ํ–‰๋™์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

๋Œ€๋ถ€๋ถ„์˜ ๋ฌธ์ œ๋“ค์€ ๋…ธ๋“œ์™€ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ช‡์‹ญ๋งŒ๊ฐœ์”ฉ ์ค˜์„œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ํ‘ธ๋Š” ๊ฒŒ ๊ดœํžˆ ๋งˆ์Œ์ด ํŽธํ•˜๋‹ค. 

 

1. Node ํด๋ผ์Šค 

- Edge๋ผ๊ณ ๋„ ๋งŽ์ด ํ•œ๋‹ค. ์ฐจ์ด๋ฅผ ๋ชจ๋ฅด๊ฒ ๋‹ค.

 //์ •์ ์— ๊ด€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” Node Class 
	static class Node
	{
		int end;      //์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ
		int dist;    //๊ฑฐ๋ฆฌ 
		Node link;
		
		public Node(int end, int dist)
		{
			super();
			this.end = end;
			this.dist = dist;
		}
	}

2. Vertex ํด๋ผ์Šค 

 //PriorityQueue ์‚ฌ์šฉ์‹œ, ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ง€์ •ํ•ด์ฃผ๊ธฐ ์œ„ํ•œ ํด๋ผ์Šค
	//compareTo ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•˜์—ฌ, ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์€ Vertex๊ฐ€ ์šฐ์„ ์ˆœ์œ„๋ฅผ ์ฐจ์ง€ํ•˜๋„๋ก ํ•œ๋‹ค 
	static class Vertex implements Comparable<Vertex>
	{
		int end;
		int dist;
		
		public Vertex(int end, int dist)
		{
			super();
			this.end = end;
			this.dist = dist;
		}
		
		@Override
		public int compareTo(Vertex o) {
			return this.dist-o.dist;
		}
	}

3. main ๋ฉ”์†Œ๋“œ 

์˜ค ๋‚˜๋ฆ„ ์ž˜ ์ •๋ฆฌํ•ด๋†จ์—ˆ๋Š”๋ฐ...?

 	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] str = br.readLine().trim().split(" ");
		StringBuilder sb = new StringBuilder();
		int V = Integer.parseInt(str[0]); //์ •์ (Node)
		int E = Integer.parseInt(str[1]); 
		int K = Integer.parseInt(br.readLine().trim());
		
		Node[] node = new Node[V+1];
		Node newNode;
		int start, end, weight;
		
		//1. ๊ทธ๋ž˜ํ”„ ์ƒ์„ฑ 
		for(int e=0;e<E;e++) {
			str = br.readLine().trim().split(" ");
			
			start = Integer.parseInt(str[0]);
			end = Integer.parseInt(str[1]);
			weight = Integer.parseInt(str[2]);
			
			//์ƒˆ๋กœ์šด ์ •์  ์ƒ์„ฑ
			newNode = new Node(end, weight); 
			//node[start]์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋˜ ๊ธฐ์กด ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋ฅผ link์— ๋‹ด์Œ
			newNode.link = node[start];      
			//node[start]์— ์ƒˆ๋กœ ๋งŒ๋“  newNode์˜ ์ฃผ์†Œ ๋‹ด์Œ 
			node[start] = newNode;
		}
		
		//2. ๊ฑฐ๋ฆฌ ๋ฐฐ์—ด, ๋ฐฉ๋ฌธ ์ฒดํฌ๋ฐฐ์—ด 
		//dist๋ฐฐ์—ด : ์‹œ์ž‘์ (K)์—์„œ ๊ฐ ๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด 
		//์šฐ์„ ์€ MAX_VALUE๋กœ ์ฑ„์›Œ์ค€ ๋’ค์— ๋” ์งง์€ ๊ฑฐ๋ฆฌ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค
		int[] dist = new int[V+1];
		Arrays.fill(dist, Integer.MAX_VALUE);
		boolean[] visited = new boolean[V+1];
		
		//3. ์‹œ์ž‘์  ์ฒดํฌ 
		dist[K] = 0;
		
		PriorityQueue<Vertex> que = new PriorityQueue<Vertex>();
		que.offer(new Vertex(K,0));
		
		Vertex vertex;
		Node currNode;
		int pos, min;
		
		while(!que.isEmpty()) {
			//2-2-c)์— ์ด์–ด์„œ : ์ด๋•Œ ์šฐ์„ ์ˆœ์œ„ ํ์ด๊ธฐ ๋•Œ๋ฌธ์—, Vertex(2,2)๊ฐ€ ๋จผ์ € ๋ฝ‘ํžŒ๋‹ค. 
			vertex = que.poll();
			
			//ํ˜„์žฌ์˜ ์œ„์น˜ 
			pos = vertex.start; 
			//ํ˜„์žฌ ์‹œ์ž‘์ ์—์„œ pos๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ์ค‘ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค
			min = vertex.weight;
			
			if(visited[pos]) continue;
			visited[pos] = true;
			
			//1) ์‹œ์ž‘์ (=1์ด๋ผ๊ณ  ๊ฐ€์ •)์„ ๊ธฐ์ค€์œผ๋กœ ์ƒ๊ฐํ•ด๋ณด๋ฉด
			//currNode์— node[1]์— ๋ถ™์–ด์žˆ๋Š” ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ๋‹ด์€ ๊ฒƒ
			currNode = node[pos];
			
			//2-1-a) ๋…ธ๋“œ 1์—๋Š” ๋…ธ๋“œ 2์™€ ๋…ธ๋“œ 3์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”๋ฐ, ๋‚˜์ค‘์— ์ž…๋ ฅ๋œ ๋…ธ๋“œ๊ฐ€ 
			//๋จผ์ € ์ž…๋ ฅ๋œ ๋…ธ๋“œ์˜ ์•ž์— ๋ผ์–ด๋“ค๊ฒŒ ๊ตฌํ˜„ํ–ˆ์œผ๋ฏ€๋กœ ์ผ๋‹จ currNode์—๋Š”
			//๋…ธ๋“œ 3์˜ ์ฃผ์†Œ๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋‹ค. 
			//2-3-a) ๋…ธ๋“œ 2์˜ ๋‹ค์Œ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๊ฐ€ ์—†์–ด currNode๋Š” Null์ด ๋œ๋‹ค. 
			while(currNode!=null) {
				//2-1-b) next์—๋Š” 3์˜ ๊ฐ’์ด ์ž…๋ ฅ๋˜์–ด ์žˆ๊ณ , nextWeight์—๋Š” ๊ฑฐ๋ฆฌ 3
				//2-2-a) ์ด์ œ next์—๋Š” 2์˜ ๊ฐ’์ด ์ž…๋ ฅ๋˜์–ด ์žˆ๊ณ , nextWeight์—๋Š” ๊ฑฐ๋ฆฌ 2
				int next = currNode.connNode;
				int nextWeight = currNode.weight;
				//2-1-c) ํ•œ๋ฒˆ๋„ ๋…ธ๋“œ 3์„ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๊ณ , dist[3]์—๋Š” ํ˜„์žฌ MAX_VALUE๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค
				//2-2-b) ํ•œ๋ฒˆ๋„ ๋…ธ๋“œ 2๋ฅผ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์—†๊ณ , dist[2]์—๋Š” ํ˜„์žฌ MAX_VALUE๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค 
				if(!visited[next] && min+nextWeight < dist[next] ) {
					dist[next] = min+nextWeight;
					//2-1-d) ์ด๋ ‡๊ฒŒ ๋˜๋ฉด, que์—๋Š” Vertex(3,3)์ด ๋“ค์–ด๊ฐ„๋‹ค
					//2-2-c) ์ด๋ ‡๊ฒŒ ๋˜๋ฉด, que์—๋Š” Vertex(2,2)๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.
					//์ด๋•Œ ์ค‘์š”ํ•œ ์ ์€, que๊ฐ€ ์šฐ์„ ์ˆœ์œ„ ํ์ด๋ฉฐ, Vertex ํด๋ž˜์Šค์—์„œ 
					//weight๊ฐ€ ์ ์€ ๊ฐ’์ด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๊ฐ€์ง€๋„๋ก ๊ตฌํ˜„ํ–ˆ์œผ๋ฏ€๋กœ ์ˆœ์„œ ์ƒ์œผ๋กœ๋Š” 
					//Vertex(3,3)์„ ๋จผ์ € ๋„ฃ์–ด๋„, ๋‹ค์Œ ์ˆœ์„œ์—์„œ que.poll()์„ ํ•  ๋•Œ์—๋Š”
					//Vertex(2,2)๊ฐ€ ๋จผ์ € ๋‚˜์˜จ๋‹ค. 
					que.offer(new Vertex(next,dist[next]));
				}
				
				//2-1-e) ๋…ธ๋“œ 3์—๋Š” ๋…ธ๋“œ2์˜ ์ฃผ์†Œ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค.์ด์ œ currNode์— ๋…ธ๋“œ2๊ฐ€ ๋‹ด๊ฒจ์žˆ๋„๋ก ํ•œ๋‹ค. 
				//2-2-d) ๋…ธ๋“œ 2 ๋‹ค์Œ์—๋Š” ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ currNode = null์ด ๋œ๋‹ค
				currNode = currNode.link;
			}
		}
		for(int i=1;i<=V;i++) {
			if(dist[i]==Integer.MAX_VALUE) sb.append("INF\n");
			else sb.append(dist[i]+"\n");
		}
		System.out.println(sb.toString().trim());
	}

๋‚ด๊ฐ€ ์ฒ˜์Œ ๋‹ค์ต์ŠคํŠธ๋ผ๋ฅผ ๋ฐฐ์šธ ๋•Œ๋„ ๊ทธ๋žฌ๊ณ , ์ง€๊ธˆ๋„ ๊ฐ„ํ˜น ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ถ€๋ถ„ ์ค‘ ํ•˜๋‚˜๋Š” visited ๋ฐฐ์—ด๋กœ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๋Š” ์ด์œ ์˜€๋‹ค. ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๋ฉด ๋” ์งง์€ ๊ฑฐ๋ฆฌ๋ฅผ ๋†“์น˜๋Š” ๊ฒŒ ์•„๋‹Œ๊ฐ€ ์‹ถ์—ˆ๋‹ค. 

์˜ˆ๋ฅผ ๋“ค์–ด, ๋…ธ๋“œ 1๊ณผ ๋…ธ๋“œ 5๊ฐ€ ์•„๋ž˜์ฒ˜๋Ÿผ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๊ณ  ํ•˜์ž.

๋…ธ๋“œ 1 → ๋…ธ๋“œ 5๊นŒ์ง€ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ : 5

๋…ธ๋“œ 5 → ๋…ธ๋“œ 1๊นŒ์ง€ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ : 2

 

๋งŒ์•ฝ ์‹œ์ž‘์ ์ด ๋…ธ๋“œ 1์ด๋ผ์„œ ์ผ๋‹จ visited[1] = true ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์—ˆ์„ ๊ฑฐ๊ณ , que์—๋Š” Vertex(5,5)๊ฐ€ ๋“ค์–ด๊ฐˆ ํ…๋ฐ

que.poll()ํ•ด์„œ Vertex(5,5)๋ฅผ ๋ฝ‘์•˜์„ ๋•Œ, ์ด๋ฏธ ๋…ธ๋“œ 1์€ visited[1]=true ์ฒ˜๋ฆฌ ๋˜์–ด ์žˆ์–ด์„œ, ํ›จ์”ฌ ๊ฑฐ๋ฆฌ๊ฐ€ ์งง์€ 2๊ฐ€ ๊ณ ๋ ค๋˜์ง€ ์•Š๋Š”๋‹ค. 

 

๊ทผ๋ฐ ๋‚ด๊ฐ€ ์• ์ดˆ์— ๋‹ค์ต์ŠคํŠธ๋ผ์˜ ์ „์ œ๋ฅผ ๋ชจ๋ฅด๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋“  ์˜๋ฌธ์ด์—ˆ๋‹ค. 

๋‹ค์ต์ŠคํŠธ๋ผ๋Š” ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„๋ผ๋Š” ์ „์ œ๋ฅผ ๊น ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ธฐ๋ฒ•์ด๋‹ค. ์ฆ‰, ๋…ธ๋“œ 1→๋…ธ๋“œ 5์™€ ๋…ธ๋“œ 5→๋…ธ๋“œ 1์€ ์—„์—ฐํžˆ ๋‹ค๋ฅธ ์ผ€์ด์Šค๋‹ค. ๋…ธ๋“œ 5์—์„œ ๋…ธ๋“œ 1๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•ด์„œ ํ•ญ์ƒ ๋…ธ๋“œ 1์—์„œ ๋…ธ๋“œ 5๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฑด ์•„๋‹ˆ๋‹ค. 

์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ๋…ธ๋“œ 1์—์„œ ๋…ธ๋“œ 5๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€, ๋…ธ๋“œ 1์—์„œ ๋…ธ๋“œ 5๋กœ ๋ฐ”๋กœ ๊ฐ€๋Š” ๊ฒƒ ๋ฟ์ด๋‹ค. 

 

์ •๋ฆฌํ•˜๋ฉด, ๋…ธ๋“œ A, B, C๊ฐ€ ์žˆ๊ณ , A→B๋กœ ๊ฐ€๋Š” ๊ฐ„์„  1, A→C๋กœ ๊ฐ€๋Š” ๊ฐ„์„ , B→C๋กœ ๊ฐ€๋Š” ๊ฐ„์„  3๊ฐœ๊ฐ€ ์žˆ์„ ๋•Œ, ๋…ธ๋“œ A์—์„œ ๋…ธ๋“œ C๊นŒ์ง€ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐพ๋Š”๋‹ค.

- ๋…ธ๋“œ A์—์„œ ๋…ธ๋“œ C๊นŒ์ง€์˜ ํ˜„์žฌ ์ตœ๋‹จ๊ฑฐ๋ฆฌ(dist[]๋ฐฐ์—ด์˜ ๊ฐ’)

- ๋…ธ๋“œ A์—์„œ ๋…ธ๋“œ B๊นŒ์ง€ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฑฐ๋ฆฌ + ๋…ธ๋“œ B์—์„œ ๋…ธ๋“œ C๊นŒ์ง€ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ

์œ„ ๋‘ ๊ฐœ์˜ ๊ฑฐ๋ฆฌ ์ค‘ ๊ฐ€์žฅ ์งง์€ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ 

๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๊ณ  ํ•˜์ž.

A→B๋กœ ๊ฐ€๋Š” ๊ฐ„์„  : 1

A→C๋กœ ๊ฐ€๋Š” ๊ฐ„์„  : 10

B→C๋กœ ๊ฐ€๋Š” ๊ฐ„์„  : 2

 

์‹œ์ž‘ ๋…ธ๋“œ์ธ A์—์„œ ์‹œ์ž‘ํ•  ๋•Œ, dist[B] = 1, dist[C] = 10์œผ๋กœ ๊ฐฑ์‹ ๋  ๊ฒƒ์ด๊ณ , ์ด์ œ ๋…ธ๋“œ B์™€ ๋…ธ๋“œ C๋ฅผ ์ฐจ๋ก€๋กœ ๋“ค๋ฅผํ…๋ฐ

๋…ธ๋“œ B์—์„œ ๋…ธ๋“œ C๊นŒ์ง€ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ = 2์ด๋ฏ€๋กœ dist[C] = dist[B]+2 = 1+2 = 3์œผ๋กœ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐฑ์‹ ๋œ๋‹ค.