Skip to content

Commit be13703

Browse files
committed
[Graph] baekjoon-1854
1 parent 056a2f7 commit be13703

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

โ€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
| 03 | | [Baekjoon-1753 ์ตœ๋‹จ๊ฒฝ๋กœ](./src/Graph/P1753) | Dijkstra |
180180
| 04 | | [Baekjoon-11657 ํƒ€์ž„๋จธ์‹ ](./src/Graph/P11657) | Bellman-Ford |
181181
| 05 | | [Baekjoon-11404 ํ”Œ๋กœ์ด๋“œ](./src/Graph/P11404) | Floyd-Warshall |
182+
| 06 | | [Baekjoon-1854 K๋ฒˆ์งธ ์ตœ๋‹จ๊ฒฝ๋กœ ์ฐพ๊ธฐ](./src/Graph/P1854) | Dijkstra |
182183

183184
### Union-Find
184185

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

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package Graph.P1854;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int n, m, k;
9+
static ArrayList<Node>[] graph;
10+
static PriorityQueue<Integer>[] D;
11+
12+
public static void main(String[] args) throws Exception {
13+
System.setIn(new FileInputStream("src/Graph/P1854/input.txt"));
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
n = stoi(st.nextToken());
18+
m = stoi(st.nextToken());
19+
k = stoi(st.nextToken());
20+
21+
graph = new ArrayList[n+1];
22+
D = new PriorityQueue[n+1];
23+
for (int i = 1; i <= n; i++) {
24+
graph[i] = new ArrayList<>();
25+
D[i] = new PriorityQueue<>(((o1, o2) -> o2 - o1));
26+
}
27+
28+
while (m-- > 0) {
29+
st = new StringTokenizer(br.readLine());
30+
int a = stoi(st.nextToken()), b = stoi(st.nextToken()), c = stoi(st.nextToken());
31+
graph[a].add(new Node(b, c));
32+
}
33+
34+
PriorityQueue<Node> pq = new PriorityQueue<>(((o1, o2) -> o1.dist - o2.dist));
35+
pq.offer(new Node(1, 0));
36+
D[1].offer(0);
37+
38+
while (!pq.isEmpty()) {
39+
Node cur = pq.poll();
40+
for (Node next : graph[cur.idx]) {
41+
if (D[next.idx].size() < k) {
42+
D[next.idx].offer(cur.dist + next.dist);
43+
pq.offer(new Node(next.idx, cur.dist + next.dist));
44+
} else {
45+
if (D[next.idx].peek() > cur.dist + next.dist) {
46+
D[next.idx].poll();
47+
D[next.idx].offer(cur.dist + next.dist);
48+
pq.offer(new Node(next.idx, cur.dist + next.dist));
49+
}
50+
}
51+
}
52+
}
53+
54+
for (int i = 1; i <= n; i++) {
55+
if (D[i].size() < k) System.out.println(-1);
56+
else System.out.println(D[i].peek());
57+
}
58+
}
59+
60+
static class Node {
61+
int idx, dist;
62+
63+
public Node(int idx, int dist) {
64+
this.idx = idx;
65+
this.dist = dist;
66+
}
67+
}
68+
69+
static int stoi(String s) { return Integer.parseInt(s); }
70+
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## [baekjoon-1854] K๋ฒˆ์งธ ์ตœ๋‹จ๊ฒฝ๋กœ ์ฐพ๊ธฐ
2+
3+
![image](https://user-images.githubusercontent.com/22045163/106920032-068dd300-674e-11eb-81d0-e3f362577bcc.png)
4+
5+
### ํ’€์ด ๋กœ์ง
6+
7+
๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ˜„์žฌ ๊ณ„์‚ฐ๋œ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ํฐ ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด ๋ฌด์‹œํ•ด๋ฒ„๋ฆฐ๋‹ค.
8+
์ด ๋ฌธ์ œ๋Š” ๊ทธ ๊ฐ’์„ ๋ฌด์‹œํ•˜์ง€ ์•Š๊ณ  top `K` ๋ฅผ ๋ฝ‘๋Š” ๊ฒƒ์ด๋‹ค. ์–ด๋–ป๊ฒŒ ์ด๋Ÿฐ ์ƒ๊ฐ์„ ํ–ˆ์„๊นŒ.
9+
์ฐธ ์žฌ๋ฐŒ๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ๐Ÿง˜๐Ÿผโ€โ™‚๏ธ
10+
11+
![image](https://user-images.githubusercontent.com/22045163/106920894-d266e200-674e-11eb-9e49-c85bd39be790.png)

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

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

0 commit comments

Comments
ย (0)