-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPassing_013.java
More file actions
85 lines (69 loc) · 1.79 KB
/
Passing_013.java
File metadata and controls
85 lines (69 loc) · 1.79 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package java;
import java.util.*;
/**
* 013 - Passing (★5)
* ダイクストラ法
*/
public class Passing_013 {
private static int N;
private static int M;
private static List<Town>[] G;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.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 = sc.nextInt() - 1;
int b = sc.nextInt() - 1;
long c = sc.nextInt();
G[a].add(new Town(b, c));
G[b].add(new Town(a, c));
}
// 事前にコストを計算しておく
long[] oneToN = dijkstra(0);
long[] nToOne = dijkstra(N - 1);
for (int i = 0; i < N; i++) {
long ans = oneToN[i] + nToOne[i];
System.out.println(ans);
}
}
public static long[] dijkstra(int start) {
long[] dist = new long[N];
Arrays.fill(dist, Long.MAX_VALUE);
dist[start] = 0;
PriorityQueue<Town> log = new PriorityQueue<>();
log.add(new Town(start, 0));
while (!log.isEmpty()) {
// 現時点での最小コストとなる街
Town town = log.poll();
int from = town.pos;
if (dist[from] < town.cost) continue;
for (Town tc : G[from]) {
int to = tc.pos;
long cost = dist[from] + tc.cost;
if (dist[to] <= cost) continue;
dist[to] = cost;
log.add(new Town(to, dist[to]));
}
}
return dist;
}
/** 街 */
static class Town implements Comparable<Town> {
int pos;
long cost;
public Town(int pos, long cost) {
this.pos = pos;
this.cost = cost;
}
/** {@inheritDoc} */
@Override
public int compareTo(Town other) {
return Long.compare(cost, other.cost);
}
}
}