-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAprilfoolA.java
More file actions
57 lines (50 loc) · 1.47 KB
/
Copy pathAprilfoolA.java
File metadata and controls
57 lines (50 loc) · 1.47 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
import java.io.*;
import java.util.*;
public class AprilfoolA {
static class Edge {
int to;
int weight;
Edge(int to, int weight) {
this.to=to;
this.weight=weight;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
if (n == -1) return;
int m=sc.nextInt();
ArrayList<Edge>[] adj=new ArrayList[n + 1];
for (int i=1;i <= n;i++) {
adj[i]=new ArrayList<>();
}
for (int i=0;i < m;i++) {
int u=sc.nextInt();
int v=sc.nextInt();
int w=sc.nextInt();
adj[u].add(new Edge(v, w));
adj[v].add(new Edge(u, w));
}
int[] dist=new int[n + 1];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[1]=0;
for (int u=1;u <= n;u++) {
if (dist[u] == Integer.MAX_VALUE) continue;
for (Edge edge : adj[u]) {
int v=edge.to;
int w=edge.weight;
if (dist[u] + w < dist[v]) {
dist[v]=dist[u] + w;
}
}
}
StringBuilder out=new StringBuilder();
for (int i=2;i <= n;i++) {
if (dist[i] == Integer.MAX_VALUE)
out.append("-1\n");
else
out.append(dist[i]).append("\n");
}
System.out.print(out);
}
}