https://www.acmicpc.net/problem/1916
์ด์ ๊ฐ์ ํ๊ทธ์ธ ๋ค์ต์คํธ๋ผ ๋ฌธ์ ๋ฅผ ํ๊ณ ๋ค์ต์คํธ๋ผ ๋ณต์ต๊ฒธ ํ์๋ค. ํ ๋์ฐฉ ์ง์ ์ ์ต์๋น์ฉ๋ง ๊ตฌํ๋ฉด ๋๋๊ฑฐ๋ผ ๋ ๊ฐ๋จํ๋ค. ์์ผ๋ก ์ง์ ๋ค ์น๋ฉด์ ์๊ณ ๋ฆฌ์ฆ์ด ๋จธ๋ฆฟ์์ ๋ค์ด๊ฐ๊ณ ์์ ์ต๊ฒ ํ๋ค.
์ฝ๋
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]));
}
}
}
}
}
๊ฒฐ๊ณผ
'Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 1931๋ฒ - ํ์์ค ๋ฐฐ์ (0) | 2022.08.05 |
---|---|
๋ฐฑ์ค 2563๋ฒ - ์์ข ์ด (0) | 2022.08.05 |
11404๋ฒ - ํ๋ก์ด๋ (0) | 2022.04.06 |
๋ฐฑ์ค 1780๋ฒ - ์ข ์ด์ ๊ฐ์ (0) | 2022.04.03 |
๋ฐฑ์ค 1072๋ฒ - ๊ฒ์ (0) | 2022.03.30 |