-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain2.java
68 lines (56 loc) Β· 1.89 KB
/
Main2.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
66
67
68
package Graph.P1753;
import java.io.*;
import java.util.*;
public class Main2 {
final static int INF = Integer.MAX_VALUE;
static int V, E, start;
static int[] D;
static LinkedList<Edge>[] graph;
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 = Integer.parseInt(st.nextToken());
E = Integer.parseInt(st.nextToken());
start = Integer.parseInt(br.readLine());
graph = new LinkedList[V+1];
for (int i = 1; i <= V; i++) graph[i] = new LinkedList<>();
while(E-- > 0) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
int w = Integer.parseInt(st.nextToken());
graph[u].add(new Edge(v, w));
}
D = new int[V+1];
Arrays.fill(D, INF);
dijkstra();
for (int i = 1; i <= V; i++) {
if (D[i] == INF) System.out.println("INF");
else System.out.println(D[i]);
}
}
static void dijkstra() {
D[start] = 0;
PriorityQueue<Edge> pq = new PriorityQueue<>(((o1, o2) -> o1.w - o2.w));
pq.add(new Edge(start, 0));
while (!pq.isEmpty()) {
Edge e = pq.poll();
if (D[e.v] < e.w) continue;
for (Edge to : graph[e.v]) {
int dist = e.w + to.w;
if (D[to.v] > dist) {
D[to.v] = dist;
pq.add(new Edge(to.v, dist));
}
}
}
}
static class Edge {
int v, w;
public Edge(int v, int w) {
this.v = v;
this.w = w;
}
}
}