Skip to content

Commit 951f138

Browse files
committed
[Graph] baekjoon-1753
1 parent c2f2cca commit 951f138

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
| :-: | :-: | :---------------------------------------------------- | :--- |
164164
| 01 | | [Baekjoon-11724 연결 요소의 개수](./src/Graph/P11724) | |
165165
| 02 | | [Baekjoon-1707 이분 그래프](./src/Graph/P1707) | |
166+
| 03 | | [Baekjoon-1753 최단경로](./src/Graph/P1753) | Dijkstra |
166167

167168
### Union-Find
168169

src/Graph/P1753/Main.java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package Graph.P1753;
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 V, E, K;
10+
static ArrayList<Node>[] graph;
11+
static int[] D;
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/Graph/P1753/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
18+
V = stoi(st.nextToken());
19+
E = stoi(st.nextToken());
20+
K = stoi(br.readLine());
21+
22+
graph = new ArrayList[V+1];
23+
for (int i = 1; i <= V; i++) graph[i] = new ArrayList<>();
24+
while (E-- > 0) {
25+
st = new StringTokenizer(br.readLine());
26+
int u = stoi(st.nextToken()), v = stoi(st.nextToken()), w = stoi(st.nextToken());
27+
graph[u].add(new Node(v, w));
28+
}
29+
30+
D = new int[V+1];
31+
Arrays.fill(D, INF);
32+
PriorityQueue<Node> pq = new PriorityQueue<>(((o1, o2) -> o1.dist - o2.dist));
33+
34+
pq.offer(new Node(K, 0));
35+
D[K] = 0;
36+
while (!pq.isEmpty()) {
37+
Node current = pq.poll();
38+
if (D[current.idx] < current.dist) continue;
39+
for (int i = 0; i < graph[current.idx].size(); i++) {
40+
Node next = graph[current.idx].get(i);
41+
if (D[next.idx] > D[current.idx] + next.dist) {
42+
D[next.idx] = D[current.idx] + next.dist;
43+
pq.offer(new Node(next.idx, D[next.idx]));
44+
}
45+
}
46+
}
47+
48+
for (int i = 1; i <= V; i++) {
49+
if (D[i] == INF) System.out.println("INF");
50+
else System.out.println(D[i]);
51+
}
52+
}
53+
54+
private static int stoi(String s) { return Integer.parseInt(s); }
55+
56+
static class Node {
57+
int idx;
58+
int dist;
59+
60+
public Node(int idx, int dist) {
61+
this.idx = idx;
62+
this.dist = dist;
63+
}
64+
}
65+
}

src/Graph/P1753/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## [baekjoon-1753] 최단경로
2+
3+
![image](https://user-images.githubusercontent.com/22045163/106621109-93515900-65b5-11eb-8fcf-ded496c53a11.png)
4+
5+
### 다익스트라 알고리즘 - Priority Queue를 이용한 개선 알고리즘
6+
7+
- 그래프 정보를 이차원 배열에 저장하면 **메모리 초과**
8+
- 단순 배열로 다음 방문 정점을 고르면 **시간 초과**

src/Graph/P1753/input.txt

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

0 commit comments

Comments
 (0)