Algorithm/๋ฐฑ์ค€

๋ฐฑ์ค€ 1916๋ฒˆ - ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ

giraffe_ 2022. 4. 8. 11:15

https://www.acmicpc.net/problem/1916

 

1916๋ฒˆ: ์ตœ์†Œ๋น„์šฉ ๊ตฌํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— ๋„์‹œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 1,000)์ด ์ฃผ์–ด์ง€๊ณ  ๋‘˜์งธ ์ค„์—๋Š” ๋ฒ„์Šค์˜ ๊ฐœ์ˆ˜ M(1 ≤ M ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์…‹์งธ ์ค„๋ถ€ํ„ฐ M+2์ค„๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฒ„์Šค์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋จผ์ € ์ฒ˜์Œ์—๋Š” ๊ทธ

www.acmicpc.net

 

 

 

 

 

 

์–ด์ œ ๊ฐ™์€ ํƒœ๊ทธ์ธ ๋‹ค์ต์ŠคํŠธ๋ผ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๋‹ค์ต์ŠคํŠธ๋ผ ๋ณต์Šต๊ฒธ ํ’€์—ˆ๋‹ค. ํ•œ ๋„์ฐฉ ์ง€์ ์˜ ์ตœ์†Œ๋น„์šฉ๋งŒ ๊ตฌํ•˜๋ฉด ๋˜๋Š”๊ฑฐ๋ผ ๋” ๊ฐ„๋‹จํ–ˆ๋‹ค. ์†์œผ๋กœ ์ง์ ‘ ๋‹ค ์น˜๋ฉด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋จธ๋ฆฟ์†์— ๋“ค์–ด๊ฐ€๊ณ  ์†์— ์ต๊ฒŒ ํ–ˆ๋‹ค.

 

 

 

 

 

์ฝ”๋“œ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;

public class GettingMinimalCost {
	public static final int INF = 987654321;
	public static ArrayList<Node>[] list;
	public static int[] dist;
	public static boolean[] visited;
	
	static class Node implements Comparable<Node>{
		int to;
		int cost;
		public Node(int to, int cost) {
			super();
			this.to = to;
			this.cost = cost;
		}
		
		@Override
		public int compareTo(Node o) {
			return Integer.compare(this.cost, o.cost);
		}
	}
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(br.readLine());
		int M = Integer.parseInt(br.readLine());
		
		list = new ArrayList[N + 1];
		for(int i = 1; i < N + 1; i++) {
			list[i] = new ArrayList<>();
		}
		
		dist = new int[N + 1];
		visited = new boolean[N + 1];
		
		StringTokenizer st;
		for(int i = 0; i < M; i++) {
			st = new StringTokenizer(br.readLine());
			int from = Integer.parseInt(st.nextToken());
			int to = Integer.parseInt(st.nextToken());
			int cost = Integer.parseInt(st.nextToken());
			
			list[from].add(new Node(to, cost));
		}
		
		st = new StringTokenizer(br.readLine());
		int A = Integer.parseInt(st.nextToken());
		int B = Integer.parseInt(st.nextToken());
		
		dijkstra(A);
		
		System.out.println(dist[B]);
	}

	public static void dijkstra(int start) {
		Queue<Node> q = new PriorityQueue<>();
		Arrays.fill(dist, INF);
		
		q.add(new Node(start, 0));
		dist[start] = 0;
		
		while(!q.isEmpty()) {
			Node node = q.poll();
			int to = node.to;
			
			if(visited[to])	continue;
			visited[to] = true;
			
			for(Node next : list[to]) {
				if(dist[next.to] > dist[to] + next.cost) {
					dist[next.to] = dist[to] + next.cost;
					q.add(new Node(next.to, dist[next.to]));
				}
			}
		}
	}

}

 

 

 

 

 

๊ฒฐ๊ณผ