-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1916.java
More file actions
81 lines (67 loc) · 2.12 KB
/
BOJ1916.java
File metadata and controls
81 lines (67 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package ¹éÁØ;
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.StringTokenizer;
public class BOJ1916 {
static boolean[] visited;
static int N, M;
static int[] dist;
static ArrayList<ArrayList<Node>> graph = new ArrayList<>();
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine()); //µµ½ÃÀÇ ¼ö
M = Integer.parseInt(br.readLine()); //¹ö½ºÀÇ ¼ö
StringTokenizer st;
visited = new boolean[N+1];
dist = new int[N+1];
for(int i = 0; i <= N; i++) {
graph.add(new ArrayList<>());
}
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 money = Integer.parseInt(st.nextToken());
graph.get(from).add(new Node(to, money));
}
st = new StringTokenizer(br.readLine());
int startPoint = Integer.parseInt(st.nextToken());
int endPoint = Integer.parseInt(st.nextToken());
Arrays.fill(dist, Integer.MAX_VALUE - 1);
dijkstra(startPoint, endPoint);
System.out.print(dist[endPoint]);
}
private static void dijkstra(int start, int end) {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(start, 0));
dist[start] = 0;
while(!pq.isEmpty()) {
Node node = pq.poll();
int nodePoint = node.end;
int c = node.cost;
if(visited[nodePoint]) continue;
visited[nodePoint] = true;
for(Node temp : graph.get(nodePoint)) {
if(dist[nodePoint] + temp.cost < dist[temp.end]) {
dist[temp.end] = dist[nodePoint] + temp.cost;
pq.offer(new Node(temp.end, dist[temp.end]));
}
}
}
}
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 this.cost - o.cost;
}
}
}