Skip to content

Commit 0af075e

Browse files
authored
[20260122] BOJ / P5 / 거의 최단 경로 / 김민진
1 parent c1f4024 commit 0af075e

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class BJ_5719_거의_최단_경로 {
6+
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static final StringBuilder sb = new StringBuilder();
10+
private static StringTokenizer st;
11+
12+
private static int N, M, S, D, shortest;
13+
private static int[] dist, distFor, distBack;
14+
private static List<Node>[] graphFor, graphBack;
15+
private static Queue<Node> pq;
16+
17+
private static class Node implements Comparable<Node> {
18+
int to;
19+
int cost;
20+
21+
public Node(int to, int cost) {
22+
this.to = to;
23+
this.cost = cost;
24+
}
25+
26+
@Override
27+
public int compareTo(Node o) {
28+
return Integer.compare(this.cost, o.cost);
29+
}
30+
31+
}
32+
33+
public static void main(String[] args) throws IOException {
34+
while (init()) {
35+
sol();
36+
}
37+
bw.write(sb.toString());
38+
bw.flush();
39+
bw.close();
40+
br.close();
41+
}
42+
43+
private static boolean init() throws IOException {
44+
st = new StringTokenizer(br.readLine());
45+
N = Integer.parseInt(st.nextToken());
46+
M = Integer.parseInt(st.nextToken());
47+
48+
if (N == 0 && M == 0) return false;
49+
50+
st = new StringTokenizer(br.readLine());
51+
S = Integer.parseInt(st.nextToken());
52+
D = Integer.parseInt(st.nextToken());
53+
54+
graphFor = new List[N];
55+
graphBack = new List[N];
56+
for (int i = 0; i < N; i++) {
57+
graphFor[i] = new ArrayList<>();
58+
graphBack[i] = new ArrayList<>();
59+
}
60+
61+
dist = new int[N];
62+
distFor = new int[N];
63+
distBack = new int[N];
64+
Arrays.fill(dist, 987654321);
65+
Arrays.fill(distFor, 987654321);
66+
Arrays.fill(distBack, 987654321);
67+
68+
for (int i = 0; i < M; i++) {
69+
st = new StringTokenizer(br.readLine());
70+
int a = Integer.parseInt(st.nextToken());
71+
int b = Integer.parseInt(st.nextToken());
72+
int c = Integer.parseInt(st.nextToken());
73+
74+
graphFor[a].add(new Node(b, c));
75+
graphBack[b].add(new Node(a, c));
76+
}
77+
78+
pq = new PriorityQueue<>();
79+
return true;
80+
}
81+
82+
private static void sol() {
83+
dijkstra(S, D, distFor, graphFor);
84+
dijkstra(D, S, distBack, graphBack);
85+
shortest = distFor[D];
86+
87+
findRoute();
88+
}
89+
90+
private static void dijkstra(int start, int end, int[] dist, List<Node>[] graph) {
91+
pq.clear();
92+
pq.add(new Node(start, 0));
93+
dist[start] = 0;
94+
95+
while (!pq.isEmpty()) {
96+
Node cur = pq.poll();
97+
98+
if (dist[cur.to] < cur.cost) continue;
99+
100+
if (cur.to == end) break;
101+
102+
for (Node next : graph[cur.to]) {
103+
int nextCost = dist[cur.to] + next.cost;
104+
105+
if (nextCost < dist[next.to]) {
106+
dist[next.to] = nextCost;
107+
pq.add(new Node(next.to, nextCost));
108+
}
109+
}
110+
}
111+
}
112+
113+
private static void findRoute() {
114+
pq.clear();
115+
pq.offer(new Node(S, 0));
116+
dist[S] = 0;
117+
118+
while (!pq.isEmpty()) {
119+
Node cur = pq.poll();
120+
121+
if (dist[cur.to] < cur.cost) continue;
122+
123+
if (cur.to == D) {
124+
sb.append(dist[cur.to]).append("\n");
125+
return;
126+
}
127+
128+
for (Node next : graphFor[cur.to]) {
129+
if (distFor[cur.to] + next.cost + distBack[next.to] == shortest) continue;
130+
131+
int nextCost = dist[cur.to] + next.cost;
132+
133+
if (nextCost < dist[next.to]) {
134+
dist[next.to] = nextCost;
135+
pq.offer(new Node(next.to, nextCost));
136+
}
137+
}
138+
}
139+
sb.append("-1\n");
140+
}
141+
142+
}
143+
```

0 commit comments

Comments
 (0)