Skip to content

Commit 39d33f8

Browse files
committed
[Graph] baekjoon-21278
1 parent 59970bb commit 39d33f8

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

โ€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@
289289
| 06 | โญ๏ธ | [Baekjoon-5719 ๊ฑฐ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ](./src/Graph/P5719) | Dijkstra |
290290
| 07 | | [Baekjoon-9205 ๋งฅ์ฃผ ๋งˆ์‹œ๋ฉด์„œ ๊ฑธ์–ด๊ฐ€๊ธฐ](./src/Graph/P9205) | Floyd-Warshall |
291291
| 08 | | [SWEA-1263 ์‚ฌ๋žŒ ๋„คํŠธ์›Œํฌ2](./src/Graph/swea1263) | Floyd-Warshall |
292+
| 09 | | [Baekjoon-21278 ํ˜ธ์„์ด ๋‘ ๋งˆ๋ฆฌ ์น˜ํ‚จ](./src/Graph/P21278) | Floyd-Warshall |
292293

293294
## Math
294295

โ€Žsrc/Graph/P21278/Main.java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package Graph.P21278;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileInputStream;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class Main {
9+
10+
final static int INF = 201;
11+
static int N, M;
12+
static int[][] D;
13+
14+
public static void main(String[] args) throws Exception {
15+
System.setIn(new FileInputStream("src/Graph/P21278/input.txt"));
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
19+
N = Integer.parseInt(st.nextToken());
20+
M = Integer.parseInt(st.nextToken());
21+
22+
D = new int[N+1][N+1];
23+
for (int i = 1; i <= N; i++) {
24+
for (int j = 1; j <= N; j++) {
25+
if (i != j) D[i][j] = INF;
26+
}
27+
}
28+
29+
while (M-- > 0) {
30+
st = new StringTokenizer(br.readLine());
31+
int A = Integer.parseInt(st.nextToken());
32+
int B = Integer.parseInt(st.nextToken());
33+
D[A][B] = 2;
34+
D[B][A] = 2;
35+
}
36+
37+
for (int k = 1; k <= N; k++) {
38+
for (int i = 1; i <= N; i++) {
39+
for (int j = 1; j <= N; j++) {
40+
D[i][j] = Math.min(D[i][j], D[i][k] + D[k][j]);
41+
}
42+
}
43+
}
44+
45+
int I = 0, J = 0, min = Integer.MAX_VALUE;
46+
for (int i = 1; i < N; i++) {
47+
for (int j = i+1; j <= N; j++) {
48+
int sum = 0;
49+
for (int k = 1; k <= N; k++) {
50+
sum += Math.min(D[i][k], D[j][k]);
51+
}
52+
if (sum < min) {
53+
I = i;
54+
J = j;
55+
min = sum;
56+
}
57+
}
58+
}
59+
60+
System.out.println(I + " " + J + " " + min);
61+
}
62+
}

โ€Žsrc/Graph/P21278/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [baekjoon-21278] ํ˜ธ์„์ด ๋‘ ๋งˆ๋ฆฌ ์น˜ํ‚จ
2+
3+
![image](https://user-images.githubusercontent.com/22045163/113171455-7027ea80-9282-11eb-8622-e7fe4113fdd9.png)
4+
5+
![image](https://user-images.githubusercontent.com/22045163/113171495-7d44d980-9282-11eb-9bbc-a6597393e937.png)

โ€Žsrc/Graph/P21278/input.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
5 4
2+
1 3
3+
4 2
4+
2 5
5+
3 2

0 commit comments

Comments
ย (0)