Skip to content

Commit a54d8c5

Browse files
authored
T : o
1 parent ff98cae commit a54d8c5

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static List<Pair>[] adj;
6+
static int v;
7+
static int e;
8+
static int k;
9+
10+
static class Pair implements Comparable<Pair>{
11+
int cost,nxt;
12+
13+
public Pair(int cost,int nxt){
14+
this.cost = cost;
15+
this.nxt = nxt;
16+
}
17+
18+
@Override
19+
public int compareTo(Pair o) {
20+
return this.cost - o.cost;
21+
}
22+
}
23+
24+
static void solutions(){
25+
var pq = new PriorityQueue<Pair>();
26+
var d = new int[v+10];
27+
Arrays.fill(d,0x3fffffff);
28+
for (Pair p : adj[k]) {
29+
if(d[p.nxt] > p.cost){
30+
pq.add(p);
31+
d[p.nxt] = p.cost;
32+
}
33+
}
34+
d[k] = 0;
35+
while(!pq.isEmpty()){
36+
var cur = pq.poll();
37+
if(d[cur.nxt] != cur.cost) continue;
38+
for(var nxt : adj[cur.nxt]){
39+
if(cur.cost + nxt.cost < d[nxt.nxt]){
40+
pq.add(new Pair(cur.cost + nxt.cost , nxt.nxt));
41+
d[nxt.nxt] = cur.cost + nxt.cost;
42+
}
43+
}
44+
}
45+
46+
for(int i=1;i<=v;i++){
47+
if(d[i] == 0x3fffffff) System.out.println("INF");
48+
else System.out.println(d[i]);
49+
}
50+
}
51+
52+
53+
static void input() throws IOException {
54+
try(var br = new BufferedReader(new InputStreamReader(System.in))){
55+
var st = new StringTokenizer(br.readLine());
56+
v = Integer.parseInt(st.nextToken());
57+
e = Integer.parseInt(st.nextToken());
58+
st = new StringTokenizer(br.readLine());
59+
k = Integer.parseInt(st.nextToken());
60+
adj = new ArrayList[v+10];
61+
for(int i=1;i<=v;i++) adj[i] = new ArrayList<>();
62+
for(int i=0;i<e;i++) {
63+
st = new StringTokenizer(br.readLine());
64+
int u = Integer.parseInt(st.nextToken());
65+
int v = Integer.parseInt(st.nextToken());
66+
int w = Integer.parseInt(st.nextToken());
67+
adj[u].add(new Pair(w,v));
68+
}
69+
}
70+
}
71+
72+
public static void main(String[] args) throws IOException {
73+
input();
74+
solutions();
75+
}
76+
}

0 commit comments

Comments
 (0)