-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshortestroutesi.java
More file actions
71 lines (67 loc) · 1.53 KB
/
shortestroutesi.java
File metadata and controls
71 lines (67 loc) · 1.53 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
import java.io.*;
import java.util.*;
public class shortestroutesi{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static StringTokenizer st = new StringTokenizer("");
static int N, M;
static ArrayList<Node>[] G;
static void dij(){
long d[] = new long[N];
Arrays.fill(d, 1000000000000000L);
d[0] = 0;
Queue<Node> q = new PriorityQueue<>();
q.add(new Node(0, 0));
while(!q.isEmpty()){
Node cur = q.poll();
if(cur.d != d[cur.v]) continue;
for(Node c : G[cur.v]){
long nd = cur.d + c.d;
if(nd < d[c.v]){
d[c.v] = nd;
q.add(new Node(c.v, nd));
}
}
}
for(long i : d){
pw.print(i + " ");
}
pw.println();
}
public static void main(String args[]) throws IOException{
N = nextInt();
M = nextInt();
G = new ArrayList[N];
for(int i = 0; i < N; i++){
G[i] = new ArrayList();
}
for(int i = 0; i < M; i++){
int a = nextInt(), b = nextInt(), c = nextInt();
a--; b--;
G[a].add(new Node(b, c));
}
dij();
br.close(); pw.close();
}
static String next() throws IOException{
while(!st.hasMoreTokens()){
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException{
return Integer.parseInt(next());
}
static class Node implements Comparable<Node>{
int v;
long d;
Node(int v, long d){
this.v = v; this.d = d;
}
public int compareTo(Node n){
if(d > n.d) return 1;
else if(d == n.d) return 0;
else return -1;
}
}
}