[Algorithm] ๐Ÿฉณ๋ฐฑ์ค€ 1753 ์ตœ๋‹จ ๊ฒฝ๋กœ

HaJingJingยท2021๋…„ 5์›” 24์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
50/119

0. ๋ฌธ์ œ

๋ฐฉํ–ฅ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์ง€๋ฉด ์ฃผ์–ด์ง„ ์‹œ์ž‘์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ์œผ๋กœ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋ชจ๋“  ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 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. ์•„์ด๋””์–ด

๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜
โ†’ ์ธ์ ‘ํ–‰๋ ฌ๋กœ ๊ตฌํ˜„ ์‹œ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ

๐Ÿ’ก ์—ฐ๊ฒฐ๋œ ์ ๋“ค์„ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•จ
๐Ÿ’ก ํ˜„์žฌ ์ ์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์— ์šฐ์„ ์ˆœ์œ„ ํ ์‚ฌ์šฉ
๐Ÿ’ก ๊ธฐ์กด์— ์žˆ๋Š” ๋น„์šฉ๊ณผ ์ ์„ ๊ฒฝ์œ ํ•ด์„œ ๊ฐ€๋Š” ๋น„์šฉ์„ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ๊ฒƒ์„ ๊ณ ๋ฆ„

2. ํ•ต์‹ฌ ํ’€์ด

1) ์—ฐ๊ฒฐ๋œ ์ ๋“ค์„ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•จ

List<Node>[] edge = new ArrayList[v+1];
		
for(int i=1; i<v+1; i++)
	edge[i] = new ArrayList<>();
		
		
for(int i=0; i<e; i++) {
	s = br.readLine().split(" ");
	int start = Integer.parseInt(s[0]);
	int end = Integer.parseInt(s[1]);
	int cost = Integer.parseInt(s[2]);
	edge[start].add(new Node(end,cost));
}

2) ํ˜„์žฌ ์ ์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ์— ์šฐ์„ ์ˆœ์œ„ ํ ์‚ฌ์šฉ

PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(k, 0));

...
while(!pq.isEmpty()){
...
}

3) ๊ธฐ์กด์— ์žˆ๋Š” ๋น„์šฉ๊ณผ ์ ์„ ๊ฒฝ์œ ํ•ด์„œ ๊ฐ€๋Š” ๋น„์šฉ์„ ๋น„๊ตํ•˜์—ฌ ๋” ์ž‘์€ ๊ฒƒ์„ ๊ณ ๋ฆ„

for(Node node : edge[cur]) {
	int next = node.end;
	int cost = node.cost;
				
	if(dist[next] > dist[cur]+cost) {
		dist[next] = dist[cur]+cost;
		pq.add(new Node(next, dist[next]));
	}
}

3. ์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
public class Graph_3 {
	static int INF = 10000000;
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().split(" ");
		
		int v = Integer.parseInt(s[0]);
		int e = Integer.parseInt(s[1]);
		int k = Integer.parseInt(br.readLine());
		
		List<Node>[] edge = new ArrayList[v+1];
		int[] dist = new int[v+1];
		
		Arrays.fill(dist, INF);
		
		for(int i=1; i<v+1; i++)
			edge[i] = new ArrayList<>();
		
		
		for(int i=0; i<e; i++) {
			s = br.readLine().split(" ");
			int start = Integer.parseInt(s[0]);
			int end = Integer.parseInt(s[1]);
			int cost = Integer.parseInt(s[2]);
			edge[start].add(new Node(end,cost));
		}
		
		PriorityQueue<Node> pq = new PriorityQueue<>();
		boolean[] visited = new boolean[v+1];
		pq.add(new Node(k, 0));
		dist[k] = 0;
		
		while(!pq.isEmpty()) {
			Node curNode = pq.poll();
			int cur = curNode.end;
			
			if(visited[cur])
				continue;
			visited[cur] = true;
			
			for(Node node : edge[cur]) {
				int next = node.end;
				int cost = node.cost;
				
				if(dist[next] > dist[cur]+cost) {
					dist[next] = dist[cur]+cost;
					pq.add(new Node(next, dist[next]));
				}
			}
		}
		
		for(int i=1; i<v+1; i++) {
			if(dist[i] != INF)
				System.out.println(dist[i]);
			else
				System.out.println("INF");
		}
	}
	
	static class Node implements Comparable<Node>{
		int end, cost;
		Node(int end, int cost){
			this.end = end;
			this.cost = cost;
		}
		
		@Override
	    public int compareTo(Node o) {
	        return cost - o.cost;
	    }
	}

}

4. ๊ฒฐ๊ณผ

์„ฑ๊ณตโœจ
์ธ์ ‘ํ–‰๋ ฌ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค๊ฐ€ ์ธ์ ‘๋ฆฌ์ŠคํŠธ, ์šฐ์„ ์ˆœ์œ„ ํ๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •์—์„œ ๋งŽ์ด ํž˜๋“ค์—ˆ๋‹ค๐Ÿ˜ฑ

profile
๐ŸŒฑ์ดˆ๋ณด ๊ฐœ๋ฐœ์ž๐ŸŒฑ

0๊ฐœ์˜ ๋Œ“๊ธ€