-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
65 lines (53 loc) Β· 1.95 KB
/
Main.java
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
package Graph.P1753;
import java.io.*;
import java.util.*;
public class Main {
final static int INF = Integer.MAX_VALUE;
static int V, E, K;
static ArrayList<Node>[] graph;
static int[] D;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Graph/P1753/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
V = stoi(st.nextToken());
E = stoi(st.nextToken());
K = stoi(br.readLine());
graph = new ArrayList[V+1];
for (int i = 1; i <= V; i++) graph[i] = new ArrayList<>();
while (E-- > 0) {
st = new StringTokenizer(br.readLine());
int u = stoi(st.nextToken()), v = stoi(st.nextToken()), w = stoi(st.nextToken());
graph[u].add(new Node(v, w));
}
D = new int[V+1];
Arrays.fill(D, INF);
PriorityQueue<Node> pq = new PriorityQueue<>(((o1, o2) -> o1.dist - o2.dist));
pq.offer(new Node(K, 0));
D[K] = 0;
while (!pq.isEmpty()) {
Node current = pq.poll();
if (D[current.idx] < current.dist) continue;
for (int i = 0; i < graph[current.idx].size(); i++) {
Node next = graph[current.idx].get(i);
if (D[next.idx] > D[current.idx] + next.dist) {
D[next.idx] = D[current.idx] + next.dist;
pq.offer(new Node(next.idx, D[next.idx]));
}
}
}
for (int i = 1; i <= V; i++) {
if (D[i] == INF) System.out.println("INF");
else System.out.println(D[i]);
}
}
private static int stoi(String s) { return Integer.parseInt(s); }
static class Node {
int idx;
int dist;
public Node(int idx, int dist) {
this.idx = idx;
this.dist = dist;
}
}
}