-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstra_05719_almostShortestWay.java
130 lines (107 loc) · 3.56 KB
/
dijkstra_05719_almostShortestWay.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package dijkstra;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.*;
/**
* 1. 문제 링크: https://www.acmicpc.net/problem/5719
*/
public class dijkstra_05719_almostShortestWay {
static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static int N, M, S, D;
static int[] dist;
static boolean[][] visit;
static List<Node>[] adj;
static List<Integer>[] parent;
public static void main(String[] args) throws Exception {
dijkstra_05719_almostShortestWay main = new dijkstra_05719_almostShortestWay();
while (true) {
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
if (N == 0 && M == 0) {
break;
}
main.solve();
}
bw.close();
br.close();
}
private void solve() throws Exception {
initialize();
dijkstra(S);
backtracking(S, D);
dijkstra(S);
if (dist[D] == Integer.MAX_VALUE) {
bw.write("-1\n");
} else {
bw.write(dist[D] + "\n");
}
}
private void dijkstra(int start) {
Arrays.fill(dist, Integer.MAX_VALUE);
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.add(new Node(start, 0));
dist[start] = 0;
while (!pq.isEmpty()) {
Node now = pq.poll();
if (dist[now.n] < now.cost) {
continue;
}
for (Node next : adj[now.n]) {
if (visit[now.n][next.n] || dist[next.n] < dist[now.n] + next.cost) {
continue;
}
if (dist[next.n] == dist[now.n] + next.cost) {
parent[next.n].add(now.n);
} else {
dist[next.n] = dist[now.n] + next.cost;
parent[next.n].clear();
parent[next.n].add(now.n);
pq.add(new Node(next.n, dist[next.n]));
}
}
}
}
private void backtracking(int start, int node) {
if (node == start) {
return;
}
for (Integer parent : parent[node]) {
if (!visit[parent][node]) {
visit[parent][node] = true;
backtracking(start, parent);
}
}
}
private void initialize() throws Exception {
StringTokenizer st = new StringTokenizer(br.readLine());
S = Integer.parseInt(st.nextToken());
D = Integer.parseInt(st.nextToken());
dist = new int[N];
adj = new ArrayList[N];
parent = new ArrayList[N];
visit = new boolean[N][N];
for (int i = 0; i < N; i++) {
adj[i] = new ArrayList<>();
parent[i] = new ArrayList<>();
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
adj[Integer.parseInt(st.nextToken())].add(new Node(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())));
}
}
static class Node implements Comparable<Node> {
int n, cost;
public Node(int n, int cost) {
this.n = n;
this.cost = cost;
}
@Override
public int compareTo(Node o) {
return cost - o.cost;
}
}
}