Skip to content

Commit fe59ed9

Browse files
authored
Merge pull request #879 from AlgorithmWithGod/khj20006
[20250912] BOJ / P5 / 밤편지 / 권혁준
2 parents c674bbd + 77bb7ad commit fe59ed9

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
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())
24+
nextLine();
25+
return st.nextToken();
26+
}
27+
28+
int nextInt() throws Exception {
29+
return Integer.parseInt(nextToken());
30+
}
31+
32+
long nextLong() throws Exception {
33+
return Long.parseLong(nextToken());
34+
}
35+
36+
double nextDouble() throws Exception {
37+
return Double.parseDouble(nextToken());
38+
}
39+
40+
void close() throws Exception {
41+
bw.flush();
42+
bw.close();
43+
}
44+
45+
void write(String content) throws Exception {
46+
bw.write(content);
47+
}
48+
49+
}
50+
51+
public class Main {
52+
53+
static IOController io;
54+
55+
//
56+
57+
static final int INF = 715827881;
58+
59+
static int N, M;
60+
static int[][] cost, dist, val, queries;
61+
62+
public static void main(String[] args) throws Exception {
63+
64+
io = new IOController();
65+
66+
N = io.nextInt();
67+
M = io.nextInt();
68+
cost = new int[N+1][N+1];
69+
dist = new int[N+1][N+1];
70+
val = new int[N+1][N+1];
71+
for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) {
72+
cost[i][j] = io.nextInt();
73+
if(cost[i][j] == 0 && i != j) cost[i][j] = INF;
74+
if(i != j) {
75+
dist[i][j] = INF;
76+
val[i][j] = INF;
77+
}
78+
}
79+
80+
queries = new int[M][];
81+
for(int i=0;i<M;i++) queries[i] = new int[]{io.nextInt(),io.nextInt(),io.nextInt(),i};
82+
Arrays.sort(queries, (a,b) -> a[0]-b[0]);
83+
84+
int limit = 1;
85+
int[] ans = new int[M];
86+
for(int[] query : queries) {
87+
int c = query[0], s = query[1], e = query[2], i = query[3];
88+
while(limit < c) {
89+
for(int x=limit;x<=N;x++) for(int y=x;y<=N;y++) for(int k=1;k<limit;k++) {
90+
val[x][y] = Math.min(val[x][y], cost[x][limit] + dist[limit][k] + cost[k][y]);
91+
val[x][y] = Math.min(val[x][y], cost[x][k] + dist[k][limit] + cost[limit][y]);
92+
}
93+
for(int k=1;k<=limit;k++) {
94+
dist[limit][k] = Math.min(dist[limit][k], cost[limit][k]);
95+
dist[k][limit] = Math.min(dist[k][limit], cost[k][limit]);
96+
}
97+
for(int j=1;j<=limit;j++) for(int k=1;k<=limit;k++) dist[j][k] = Math.min(dist[j][k], dist[j][limit] + dist[limit][k]);
98+
limit++;
99+
}
100+
if(s == e) ans[i] = 0;
101+
else {
102+
if(s > e) {
103+
int t = s;
104+
s = e;
105+
e = t;
106+
}
107+
if(s < c && e < c) ans[i] = dist[s][e] == INF ? -1 : dist[s][e];
108+
else if(s < c) {
109+
int res = INF;
110+
for(int k=1;k<c;k++) res = Math.min(res, dist[s][k] + cost[k][e]);
111+
ans[i] = res == INF ? -1 : res;
112+
}
113+
else {
114+
if(val[s][e] == INF) ans[i] = cost[s][e] == INF ? -1 : cost[s][e];
115+
else ans[i] = val[s][e] == INF ? -1 : val[s][e];
116+
}
117+
}
118+
}
119+
120+
for(int i=0;i<M;i++) io.write(ans[i] + "\n");
121+
122+
io.close();
123+
124+
}
125+
126+
}
127+
```

0 commit comments

Comments
 (0)