Skip to content

Commit 4f3f27e

Browse files
committed
[Graph] baekjoon-11657
1 parent 951f138 commit 4f3f27e

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

โ€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
| 01 | | [Baekjoon-11724 ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜](./src/Graph/P11724) | |
165165
| 02 | | [Baekjoon-1707 ์ด๋ถ„ ๊ทธ๋ž˜ํ”„](./src/Graph/P1707) | |
166166
| 03 | | [Baekjoon-1753 ์ตœ๋‹จ๊ฒฝ๋กœ](./src/Graph/P1753) | Dijkstra |
167+
| 04 | | [Baekjoon-11657 ํƒ€์ž„๋จธ์‹ ](./src/Graph/P11657) | Bellman-Ford |
167168

168169
### Union-Find
169170

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

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

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

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## [baekjoon-11657] ํƒ€์ž„๋จธ์‹ 
2+
3+
![image](https://user-images.githubusercontent.com/22045163/106625700-4328c580-65ba-11eb-95b4-9fd5a2761cc7.png)
4+
5+
### ์ถœ๋ ฅ ์ดˆ๊ณผ ์ด์Šˆ
6+
7+
> [11657๋ฒˆ - ํƒ€์ž„๋จธ์‹  > ์ถœ๋ ฅ์ดˆ๊ณผ ๋ฌธ์ œ ํ•ด๊ฒฐ์ด ๋˜์—ˆ๋Š”๋ฐ ์ด์œ ๋ฅผ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค](https://www.acmicpc.net/board/view/55270)
8+
9+
![image](https://user-images.githubusercontent.com/22045163/106625810-62275780-65ba-11eb-9ece-2a882f589424.png)
10+
11+
์Œ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ํ—ˆ์šฉ๋˜๊ธฐ ๋•Œ๋ฌธ์— underflow ์ด์Šˆ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค. ์•ž์œผ๋กœ ์ด๋Ÿฐ ๋ถ€๋ถ„๋„ ์‹ ๊ฒฝ ์จ์„œ ๊ตฌํ˜„ํ•ด์•ผ๊ฒ ๋‹ค.
12+
13+
![image](https://user-images.githubusercontent.com/22045163/106625767-550a6880-65ba-11eb-8091-48feded9e2b6.png)

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

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

0 commit comments

Comments
ย (0)