๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ๊ฐ๋
๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ด๋?
ํ๋์ ์ ์ ์์ ๋ชจ๋ ์ ์ ์ผ๋ก์ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ณ์ฐํ๋ ๊ฒ์ ๋ชฉํ๋ก ํ๋ ์๊ณ ๋ฆฌ์ฆ
Single source shortest paths problem
๋ก์ง
- ๋ชจ๋ ์ ์ ๋ค์ ์ฒ์์ ๋ฌดํ๋ ๊ฐ์ ๊ฐ๋๋ค.
- ์ ์ A์์ ์ ์ B๊ฐ ์ด์ด์ง๋ฉด ์ ์ A๊น์ง์ ์ต๋จ๊ฑฐ๋ฆฌ + A์์ B๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ๋ค.
- ๋ง์ฝ B๊ฐ ๊ฐ์ง๊ณ ์๋ ๊ฐ(์ฆ, A์์ B๋ก ์ด์ด์ง๋ ๋ค๋ฅธ ๊ฒฝ๋ก์ ์ํ ๊ฐ)๊ณผ 2๋ฒ์์ ๊ณ์ฐํ ๊ฐ๊ณผ ๋น๊ตํ์ฌ ํ์๊ฐ ๋ ์๋ค๋ฉด ๊ฐฑ์ ํ๋ค.
- ๊ฐ์ฅ ์์ ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ์ ์ ๋ถํฐ ์ด์ด์ ๋ฐ๋ณตํ๋ค. (๊ฐ์ฅ ์์ ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ์ ์ ๋ถํฐ ์์ํ๋ ์ด์ ๋ ์์ง ์์ ๊ฐ๋ถํฐ ์์๋๋ฉด ์ธ๋ชจ์๋ ์ฐ์ฐ์ด ์งํ๋๊ธฐ ๋๋ฌธ์ด๋ฉฐ, ์ฌ๋ฐ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์ป์ด๋ผ ์ ์๋ค.)
์์ ๊ฐ์ผ๋ก ๊ฐฑ์ ์ด ๋์ด์ผ ํ๋๋ฐ, ํฐ ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ์ ์ ๋ถํฐ ์์๋๋ฉด ์๋ฏธ๊ฐ ์์๊น? ์์.
๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ๊ตฌํ
๊ตฌํ ์ ์ฃผ์์
- ์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ฅ ์ต์๊ฐ์ ๊ฐ์ง ์ ์ ์ ์ป์ด๋ด๋๋ก ํ๋ค.
- JAVA์ Priority Queue๋ poll(), add()์ ๊ฐ์ด ์ถ๊ฐ,์ญ์ ์ฐ์ฐ์ด ์ผ์ด๋ ๋ ์ ๋ ฌ์ ์ํํ๋ค.
- ์ฐ์ ์์ ํ๋ฅผ ๊ฐฑ์ ํ ๋, ๊ฐ์ด ๋ฐ๋ ๋ฐ์ดํฐ๋ง ๋ค์ ์ฝ์ ํ๋๋ก ํ๋ค.
- ๋ง์ฝ ์ธ์ ๋ฐฐ์ด๋ก ๊ตฌํ์ ํ๊ฒ ๋์์ ๋ ๊ณต๊ฐ ๋ณต์ก๋๋ O(V^2)์ด๋ฉฐ, ์ธ์ ๋ฆฌ์คํธ๋ก ๊ตฌํ์ ํ๊ฒ ๋๋ฉด O(VE) ์ด๋ค.
๊ตฌํ
- ๋ฐฑ์ค 1753๋ฒ ์ต๋จ ๊ฒฝ๋ก ๋ฌธ์ https://www.acmicpc.net/problem/1753
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
public class ShortestPath {
static Node[] nodes;
static List<List<int[]>> edge_weights;
static boolean[] isChecked;
static public final int INF = 1000000000;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int vertexCnt,edgeCnt;
String[] line = br.readLine().split(" ");
vertexCnt = Integer.parseInt(line[0]);
edgeCnt = Integer.parseInt(line[1]);
edge_weights = new ArrayList<>();
nodes = new Node[vertexCnt+1];
isChecked = new boolean[vertexCnt+1];
// Node๋ฅผ ๋ด๊ณ ์๋ list๋ฅผ ์ด๊ธฐํ ํ๋ค.
// ์ธ์ ๋ฆฌ์คํธ๋ฅผ ์ด๊ธฐํ ํ๋ค.
edge_weights.add(new ArrayList<>());
for(int cnt=1; cnt<=vertexCnt; cnt++) {
nodes[cnt] = new Node(cnt);
edge_weights.add(new ArrayList<>());
}
int startVertex = Integer.parseInt(br.readLine());
nodes[startVertex].shortest_distance = 0;
// ๊ฐ์ ๊ฐ์ค์น ๋ฆฌ์คํธ๋ฅผ ์ด๊ธฐํ ํ๋ค.
for(int cnt=0; cnt<edgeCnt; cnt++) {
line = br.readLine().split(" ");
int start = Integer.parseInt(line[0]);
int[] end_weight = new int[2];
end_weight[0] = Integer.parseInt(line[1]);
end_weight[1] = Integer.parseInt(line[2]);
edge_weights.get(start).add(end_weight);
}
// ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ณ์ฐํ๋ค.
dijkstra(startVertex, vertexCnt);
printNodes();
line = null;
nodes = null;
edge_weights = null;
}
private static void dijkstra(int startVertex, int vertexCnt) {
PriorityQueue<Node> pqueue = new PriorityQueue<>();
//๋ชจ๋ ๋
ธ๋๋ค์ ์ฐ์ ์์ ํ์ ๋ฃ๋๋ค.
for(int cnt=1; cnt<=vertexCnt; cnt++) {
pqueue.add(nodes[cnt]);
}
//๊ฐ์ฅ ๊ฒฝ๋ก ๊ฐ์ค์น๊ฐ ์์ ๋
ธ๋๋ฅผ ์์ ๋
ธ๋๋ก ์ ํํ๋ค.
//๋ง์ฝ ๋
ธ๋๊ฐ ์ฐ๊ฒฐ๋์ด ์์ผ๋ฉด ํด๋น ๋
ธ๋์ shortest path๋ฅผ ์ต์ ํ ํ๋ค.
//๋ชจ๋ ๋
ธ๋๊ฐ ์์ ๋
ธ๋๋ก ์ ํ๋์์ผ๋ฉด ์ข
๋ฃํ๋ค.
while(!pqueue.isEmpty()) {
Node start = pqueue.poll();
isChecked[start.num] = true;
for(int[] list : edge_weights.get(start.num)) {
int before = nodes[list[0]].shortest_distance;
nodes[list[0]].shortest_distance =
Math.min(nodes[list[0]].shortest_distance, start.shortest_distance + list[1]);
if(nodes[list[0]].shortest_distance != before) {
pqueue.add(nodes[list[0]]);
}
}
}
pqueue = null;
}
private static void printNodes() {
for(int idx=1; idx<nodes.length; idx++) {
if(nodes[idx].shortest_distance == INF) {
System.out.println("INF");
}
else
System.out.println(nodes[idx].shortest_distance);
}
System.out.println();
}
static class Node implements Comparable<Node> {
int num;
int shortest_distance;
public Node(int num) {
this.num = num;
this.shortest_distance = INF;
}
@Override
public int compareTo(Node target) {
if (this.shortest_distance < target.shortest_distance) {
return -1;
} else if (this.shortest_distance > target.shortest_distance) {
return 1;
}
return 0;
}
@Override
public String toString() {
return "[" + num + "," + shortest_distance + "]";
}
}
}
|
- ํ๋ก๊ทธ๋๋จธ์ค ๋ฐฐ๋ฌ https://programmers.co.kr/learn/courses/30/lessons/12978
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ๋ฐฐ๋ฌ
5 [[1,2,1],[2,3,3],[5,2,2],[1,4,2],[5,3,1],[5,4,2]] 3 4 6 [[1,2,1],[1,3,2],[2,3,2],[3,4,3],[3,5,2],[3,5,3],[5,6,1]] 4 4
programmers.co.kr