Skip to content

Commit 3f8b1c9

Browse files
authored
Merge pull request #734 from AlgorithmWithGod/khj20006
[20250824] BOJ / P5 / 특수 능력 / 권혁준
2 parents 7e81dbe + a87c182 commit 3f8b1c9

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static final int INF = (int)1e9 + 7;
57+
58+
static int N, M, C;
59+
static int[][] min, max;
60+
61+
public static void main(String[] args) throws Exception {
62+
63+
io = new IOController();
64+
65+
init();
66+
solve();
67+
68+
io.close();
69+
70+
}
71+
72+
static void init() throws Exception {
73+
74+
N = io.nextInt();
75+
M = io.nextInt();
76+
C = io.nextInt();
77+
min = new int[N+1][N+1];
78+
max = new int[N+1][N+1];
79+
for(int i=1;i<=N;i++) {
80+
Arrays.fill(min[i], INF);
81+
Arrays.fill(max[i], -INF);
82+
}
83+
for(int i=0;i<M;i++) {
84+
int a = io.nextInt();
85+
int b = io.nextInt();
86+
int c = io.nextInt();
87+
min[a][b] = Math.min(min[a][b], c);
88+
max[a][b] = Math.max(max[a][b], c);
89+
}
90+
91+
}
92+
93+
static void solve() throws Exception {
94+
95+
int[] dist = new int[N+1];
96+
Arrays.fill(dist, INF);
97+
dist[1] = 0;
98+
PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> a[0]-b[0]);
99+
pq.offer(new int[]{0,1,0});
100+
while(!pq.isEmpty()) {
101+
int[] cur = pq.poll();
102+
int d = cur[0], n = cur[1], t = cur[2];
103+
if(d > dist[n]) continue;
104+
for(int i=1;i<=N;i++) if(min[n][i] != INF) {
105+
if(t<C && dist[i] > d - max[n][i]) {
106+
dist[i] = d - max[n][i];
107+
pq.offer(new int[]{dist[i], i, t+1});
108+
}
109+
if(dist[i] > d + min[n][i]) {
110+
dist[i] = d + min[n][i];
111+
pq.offer(new int[]{dist[i], i, t});
112+
}
113+
}
114+
}
115+
io.write(dist[N] + "\n");
116+
117+
}
118+
119+
}
120+
```

0 commit comments

Comments
 (0)