Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 0224LJH/202507/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

70 changes: 70 additions & 0 deletions 0224LJH/202507/10 BOJ 궁금한 민호.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
static int cityCnt, ans;
static int[][] distance;

public static void main(String[] args) throws IOException {
init();
process();
print();
}

private static void init() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
cityCnt = Integer.parseInt(br.readLine());
distance = new int[cityCnt][cityCnt];
ans = 0;

for (int i = 0; i < cityCnt; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
for (int j = 0; j < cityCnt; j++){
distance[i][j] = Integer.parseInt(st.nextToken());
}
}
}

private static void process() {
for (int i = 0; i < cityCnt; i++){
for (int j = i; j < cityCnt; j++){
if ( i == j) continue;
if (distance[i][j] != distance[j][i]){
// i->j와 j->i는 같아야함
ans = -1;
return;
}

boolean isDirectlyConnected = true;

for (int k = 0; k < cityCnt; k++){
if ( i == k || j ==k) continue;
int tempDis = distance[i][k] + distance[k][j];

if (tempDis < distance[i][j]){
//지금 명시된 길이가 두개의 길을 이은것 보다 짧음 -> 불가능
ans =-1;
return;
} else if ( tempDis== distance[i][j]) {
isDirectlyConnected = false;
break;
}
}

if (isDirectlyConnected) {
ans += distance[i][j];
}
}
}
}



private static void print() {
System.out.print(ans);
}
}
```