Skip to content

Commit 5c2437e

Browse files
authored
Merge pull request #729 from AlgorithmWithGod/zinnnn37
[20250824] BOJ / G4 / 두 단계 최단 경로 1 / 김민진
2 parents e710217 + a0e8706 commit 5c2437e

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class BJ_23793_두_단계_최단_경로_1 {
6+
7+
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static StringTokenizer st;
10+
11+
private static int N;
12+
private static int M;
13+
private static int X;
14+
private static int Y;
15+
private static int Z;
16+
17+
private static int[] dist;
18+
private static Queue<Node> pq;
19+
private static List<Node>[] graph;
20+
21+
private static class Node implements Comparable<Node> {
22+
int to;
23+
int weigh;
24+
25+
Node(int to, int weigh) {
26+
this.to = to;
27+
this.weigh = weigh;
28+
}
29+
30+
@Override
31+
public int compareTo(Node o) {
32+
return Integer.compare(this.weigh, o.weigh);
33+
}
34+
}
35+
36+
public static void main(String[] args) throws IOException {
37+
init();
38+
sol();
39+
}
40+
41+
private static void init() throws IOException {
42+
st = new StringTokenizer(br.readLine());
43+
N = Integer.parseInt(st.nextToken());
44+
M = Integer.parseInt(st.nextToken());
45+
46+
graph = new List[N + 1];
47+
dist = new int[N + 1];
48+
for (int i = 1; i <= N; i++) {
49+
graph[i] = new ArrayList<>();
50+
}
51+
52+
pq = new PriorityQueue<>();
53+
for (int i = 0; i < M; i++) {
54+
st = new StringTokenizer(br.readLine());
55+
56+
int from = Integer.parseInt(st.nextToken());
57+
int to = Integer.parseInt(st.nextToken());
58+
int weight = Integer.parseInt(st.nextToken());
59+
60+
graph[from].add(new Node(to, weight));
61+
}
62+
63+
st = new StringTokenizer(br.readLine());
64+
X = Integer.parseInt(st.nextToken());
65+
Y = Integer.parseInt(st.nextToken());
66+
Z = Integer.parseInt(st.nextToken());
67+
}
68+
69+
private static void sol() throws IOException {
70+
// 경유
71+
int a = dijkstra(X, Y, false);
72+
int b = dijkstra(Y, Z, false);
73+
if (a == -1 || b == -1) {
74+
bw.write("-1 ");
75+
} else {
76+
bw.write((a + b) + " ");
77+
}
78+
79+
// 경유 X
80+
bw.write(dijkstra(X, Z, true) + "");
81+
bw.flush();
82+
bw.close();
83+
br.close();
84+
}
85+
86+
private static int dijkstra(int from, int to, boolean flag) {
87+
clear();
88+
pq.offer(new Node(from, 0));
89+
dist[from] = 0;
90+
91+
while (!pq.isEmpty()) {
92+
Node cur = pq.poll();
93+
94+
if (cur.to == to) {
95+
return cur.weigh;
96+
}
97+
98+
if (dist[cur.to] < cur.weigh) {
99+
continue;
100+
}
101+
102+
for (Node n : graph[cur.to]) {
103+
// Y를 경유하지 않아야하는데 경유함
104+
if (flag && n.to == Y) {
105+
continue;
106+
}
107+
108+
int nextWeight = n.weigh + dist[cur.to];
109+
if (nextWeight < dist[n.to]) {
110+
dist[n.to] = nextWeight;
111+
pq.offer(new Node(n.to, nextWeight));
112+
}
113+
}
114+
}
115+
return dist[to] != Integer.MAX_VALUE ? dist[to] : -1;
116+
}
117+
118+
private static void clear() {
119+
pq.clear();
120+
Arrays.fill(dist, Integer.MAX_VALUE);
121+
}
122+
123+
}
124+
```

0 commit comments

Comments
 (0)