Skip to content

Commit 29405e0

Browse files
authored
[20250710] BOJ / G2 / 궁금한 민호 / 이종환
2 parents 4dd81c3 + 1d90c50 commit 29405e0

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

0224LJH/202507/.gitkeep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.*;
6+
7+
public class Main {
8+
static int cityCnt, ans;
9+
static int[][] distance;
10+
11+
public static void main(String[] args) throws IOException {
12+
init();
13+
process();
14+
print();
15+
}
16+
17+
private static void init() throws IOException {
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
cityCnt = Integer.parseInt(br.readLine());
20+
distance = new int[cityCnt][cityCnt];
21+
ans = 0;
22+
23+
for (int i = 0; i < cityCnt; i++){
24+
StringTokenizer st = new StringTokenizer(br.readLine());
25+
for (int j = 0; j < cityCnt; j++){
26+
distance[i][j] = Integer.parseInt(st.nextToken());
27+
}
28+
}
29+
}
30+
31+
private static void process() {
32+
for (int i = 0; i < cityCnt; i++){
33+
for (int j = i; j < cityCnt; j++){
34+
if ( i == j) continue;
35+
if (distance[i][j] != distance[j][i]){
36+
// i->j와 j->i는 같아야함
37+
ans = -1;
38+
return;
39+
}
40+
41+
boolean isDirectlyConnected = true;
42+
43+
for (int k = 0; k < cityCnt; k++){
44+
if ( i == k || j ==k) continue;
45+
int tempDis = distance[i][k] + distance[k][j];
46+
47+
if (tempDis < distance[i][j]){
48+
//지금 명시된 길이가 두개의 길을 이은것 보다 짧음 -> 불가능
49+
ans =-1;
50+
return;
51+
} else if ( tempDis== distance[i][j]) {
52+
isDirectlyConnected = false;
53+
break;
54+
}
55+
}
56+
57+
if (isDirectlyConnected) {
58+
ans += distance[i][j];
59+
}
60+
}
61+
}
62+
}
63+
64+
65+
66+
private static void print() {
67+
System.out.print(ans);
68+
}
69+
}
70+
```

0 commit comments

Comments
 (0)