-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ1753.java
More file actions
88 lines (75 loc) · 2.16 KB
/
BOJ1753.java
File metadata and controls
88 lines (75 loc) · 2.16 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
82
83
84
85
86
87
88
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 BOJ1753 {
static int N, M, R;
static boolean[] visited;
static int[] dist;
static ArrayList<ArrayList<Node>> list = new ArrayList<>();
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
StringBuilder sb = new StringBuilder();
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
R = Integer.parseInt(br.readLine());
visited = new boolean[N+1];
dist = new int[N+1];
for(int i = 0; i <= N; i++) {
list.add(new ArrayList<>());
}
for(int i = 1; i <= M; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int length = Integer.parseInt(st.nextToken());
list.get(from).add(new Node(to, length));
}
dijkstra();
for(int i = 1; i <= N; i++) {
if((Integer.MAX_VALUE - 1) != dist[i]) {
sb.append(dist[i]).append("\n");
}
else {
sb.append("INF").append("\n");
}
}
System.out.print(sb);
}
private static void dijkstra() {
Arrays.fill(dist, Integer.MAX_VALUE - 1);
Arrays.fill(visited, false);
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(R, 0));
dist[R] = 0;
while(!pq.isEmpty()) {
Node temp = pq.poll();
int no = temp.no;
if(visited[no]) continue;
visited[no] = true;
for(Node adj : list.get(no)) {
if(dist[adj.no] > dist[no] + adj.length) {
dist[adj.no] = dist[no] + adj.length;
pq.offer(new Node(adj.no, dist[adj.no]));
}
}
}
}
static class Node implements Comparable<Node> {
int no;
int length;
Node(int no, int length){
this.no = no;
this.length = length;
}
@Override
public int compareTo(Node o) {
return this.length - o.length;
}
}
}