Skip to content

Commit ad4d94c

Browse files
committed
[Graph] baekjoon-11404
1 parent 0159629 commit ad4d94c

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

Diff for: β€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
| 02 | | [Baekjoon-1707 이뢄 κ·Έλž˜ν”„](./src/Graph/P1707) | |
166166
| 03 | | [Baekjoon-1753 μ΅œλ‹¨κ²½λ‘œ](./src/Graph/P1753) | Dijkstra |
167167
| 04 | | [Baekjoon-11657 νƒ€μž„λ¨Έμ‹ ](./src/Graph/P11657) | Bellman-Ford |
168+
| 05 | | [Baekjoon-11404 ν”Œλ‘œμ΄λ“œ](./src/Graph/P11404) | Floyd-Warshall |
168169

169170
### Union-Find
170171

Diff for: β€Žsrc/DP/MinAbsSum/Main.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package DP.MinAbsSum;
2+
3+
import java.util.Arrays;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
Solution sol = new Solution();
8+
System.out.println(sol.solution(new int[]{}));
9+
System.out.println(sol.solution(new int[]{1, 5, 2, -2}));
10+
System.out.println(sol.solution(new int[]{3, 3, 3, 4, 5})); // 0
11+
}
12+
}
13+
14+
class Solution {
15+
public int solution(int[] A) {
16+
if (A.length == 0) return 0;
17+
18+
int[] dp = new int[A.length];
19+
dp[0] = Math.min(A[0], (-1) * A[0]);
20+
for (int i = 1; i < A.length; i++) {
21+
if (Math.abs(dp[i-1] + A[i]) < Math.abs(dp[i-1] - A[i])) {
22+
dp[i] = dp[i-1] + A[i];
23+
} else {
24+
dp[i] = dp[i-1] - A[i];
25+
}
26+
}
27+
System.out.println(Arrays.toString(dp));
28+
return Math.abs(dp[A.length - 1]);
29+
}
30+
}

Diff for: β€Žsrc/Graph/P11404/Main.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package Graph.P11404;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int n, m;
9+
static int[][] D;
10+
11+
public static void main(String[] args) throws Exception {
12+
System.setIn(new FileInputStream("src/Graph/P11404/input.txt"));
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
15+
n = Integer.parseInt(br.readLine());
16+
m = Integer.parseInt(br.readLine());
17+
18+
D = new int[n+1][n+1];
19+
while (m-- > 0) {
20+
StringTokenizer st = new StringTokenizer(br.readLine());
21+
int a = Integer.parseInt(st.nextToken());
22+
int b = Integer.parseInt(st.nextToken());
23+
int c = Integer.parseInt(st.nextToken());
24+
if (D[a][b] > 0) D[a][b] = Math.min(D[a][b], c);
25+
else D[a][b] = c;
26+
}
27+
28+
for (int k = 1; k <= n; k++) {
29+
for (int i = 1; i <= n; i++) {
30+
for (int j = 1; j <= n; j++) {
31+
if (i == j || D[i][k] == 0 || D[k][j] == 0) continue;
32+
if (D[i][j] == 0) D[i][j] = D[i][k] + D[k][j];
33+
else D[i][j] = Math.min(D[i][j], D[i][k] + D[k][j]);
34+
}
35+
}
36+
}
37+
38+
for (int i = 1; i <= n; i++) {
39+
for (int j = 1; j <= n; j++) {
40+
System.out.print(D[i][j] + " ");
41+
}
42+
System.out.println();
43+
}
44+
}
45+
}

Diff for: β€Žsrc/Graph/P11404/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## [baekjoon-11404] ν”Œλ‘œμ΄λ“œ
2+
3+
![image](https://user-images.githubusercontent.com/22045163/106630419-2b077500-65bf-11eb-9dec-b3b94edb092d.png)
4+
5+
![image](https://user-images.githubusercontent.com/22045163/106630455-335fb000-65bf-11eb-9201-e489d997fef3.png)
6+
7+
ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ„ μ΄μš©ν•˜λŠ”λ°, λ¬Έμ œμ—μ„œ "μ‹œμž‘ λ„μ‹œμ™€ 도착 λ„μ‹œλ₯Ό μ—°κ²°ν•˜λŠ” 노선은 ν•˜λ‚˜κ°€ 아닐 수 μžˆλ‹€." λΌλŠ” 쑰건을 ν™•μΈν•˜κ³  이 뢀뢄을 κ³ λ €ν•΄μ•Ό ν•œλ‹€.

Diff for: β€Žsrc/Graph/P11404/input.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
5
2+
14
3+
1 2 2
4+
1 3 3
5+
1 4 1
6+
1 5 10
7+
2 4 2
8+
3 4 1
9+
3 5 1
10+
4 5 3
11+
3 5 10
12+
3 1 8
13+
1 4 2
14+
5 1 7
15+
3 4 2
16+
5 2 4

0 commit comments

Comments
Β (0)