-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.java
More file actions
183 lines (148 loc) ยท 4.39 KB
/
Dijkstra.java
File metadata and controls
183 lines (148 loc) ยท 4.39 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package basic.algorithm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/*
ํน์ ๋
ธ๋์์ ๋ค๋ฅธ ๋ชจ๋ ๋
ธ๋๊น์ง์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ์๊ณ ๋ฆฌ์ฆ.
์์ ๊ฐ์ค์น๊ฐ ์์ ๊ฒฝ์ฐ์๋ ์ธ ์ ์์.
ํด๋น ๋ฌธ์ ์์ ๋ฐฉํฅ ์๋ ๊ทธ๋ํ๋ฅผ ๊ธฐ์ค.
*/
public class Dijkstra {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringBuilder sb = new StringBuilder();
static int[] distance;
static Map<Integer, List<Node>> graph = new HashMap<>();
public static void main(String[] args) throws IOException {
//Node์ ๊ฐ์
int N = Integer.parseInt(br.readLine());
//๊ฐ์ ์ ๊ฐ์
int E = Integer.parseInt(br.readLine());
//0 ์ ์ฌ์ฉํ์ง ์๊ณ 1 ~ N๋ฅผ ์ฌ์ฉํ๋ฉฐ ํน์ Node ๋ถํฐ ๋ค๋ฅธ Node๋ค ๊น์ง์ ๊ฑฐ๋ฆฌ.
init(N);
input(E);
//์
๋ ฅ ํน์ ๋
ธ๋
dijkstra(Integer.parseInt(br.readLine()));
printDistance();
}
//๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ
private static void dijkstra(int base){
//์ฐ์ ์์ ํ๋ฅผ ์ด์ฉ.
PriorityQueue<Node> queue = new PriorityQueue<>(
Comparator.comparingInt(node -> node.cost)
);
//์์์ ์ Queue์ ์ถ๊ฐํ ์ฑ๋ก ์์
queue.add(new Node(base, 0));
//์ค์ ํฌ์ธํธ
distance[base] = 0;
//Queue๋ฅผ ๋นผ๊ณ ์์
while(!queue.isEmpty()){
//๊ฐ์ฅ ์ต๊ทผ์ ๋
ธ๋๋ฅผ poll
Node now = queue.poll();
//๋ง์ฝ ์ถ๊ฐํ๋ ค๋ ๊ฐ์ด ๊ธฐ์กด ์ต์ ๊ฐ๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๋ค๋ฉด
//๊ฐ์ ๊ณผ์ ์ด ๋ฐ๋ณต๋ ํ
๋ ์ ์ธ
//์ด๋ now.cost์ distance[now.to] ๊ฐ ๊ฐ์ ๊ฒฝ์ฐ๋ ์ต์๊ฐ์ ์ต์ด๋ก ์ง์ด ๋ฃ์ ๋๋ง ํด๋น
if(now.cost > distance[now.to]) continue;
//๊ฐ์ ๊ฐฑ์ ์ด ์ด๋ฃจ์ด์ ธ์ผํ๋ค๋ฉด ํด๋น Node์ ์ด์ด์ง ๋ชจ๋ Node๋ค์ ํ์
for(Node next : graph.get(now.to)){
//๋ค์์ผ๋ก ์ด์ด์ง๋ Cost๋ฅผ ๊ตฌํจ.
int nextDistance = now.cost + next.cost;
//์ด์ด์ง๋ cost๊ฐ ์ต์ distance๋ณด๋ค ์์ ๊ฒฝ์ฐ
if(nextDistance < distance[next.to]){
//ํด๋น ๊ฐ์ Queue์ ์ถ๊ฐ.
distance[next.to] = nextDistance;
queue.add(new Node(next.to, nextDistance));
}
}
}
}
//์ด๊ธฐํ ๊ณผ์
private static void init(int N) {
distance = new int[N +1];
//๊ฑฐ๋ฆฌ์ ์ด๊ธฐ๊ฐ์ ์ต๋๊ฐ์ผ๋ก ์ง์ .
Arrays.fill(distance,Integer.MAX_VALUE);
//Graph ์ด๊ธฐํ
for(int i = 1; i<= N; i++){
graph.put(i, new ArrayList<>());
}
}
//์
๋ ฅ ๊ณผ์
private static void input(int E) throws IOException {
StringTokenizer st;
//๊ทธ๋ํ ์
๋ ฅ
for(int i = 0; i< E; i++){
//์
๋ ฅ์ ํํ๋ (from) (to) (cost)
st = new StringTokenizer(br.readLine());
//์์ ๋
ธ๋
int from = Integer.parseInt(st.nextToken());
//๋์ฐฉ ๋
ธ๋
int to = Integer.parseInt(st.nextToken());
//๋น์ฉ
int cost = Integer.parseInt(st.nextToken());
//์ ๋ฐฉํฅ์ด๊ธฐ ๋๋ฌธ์ ๋ ๋ชจ๋์ ์
๋ ฅ
graph.get(from).add(new Node(to, cost));
graph.get(to).add(new Node(from, cost));
}
}
//๊ฒฐ๊ณผ ์ถ๋ ฅ ๊ณผ์
private static void printDistance() {
int count = 0 ;
for(int i : distance){
if(count==0){
count++;
continue;
}
if(i == Integer.MAX_VALUE)
sb.append(String.format("%3s", "INF")).append(" ");
else
sb.append(String.format("%3d", i)).append(" ");
}
System.out.print(sb);
}
static class Node{
int to;
int cost;
public Node(int to, int cost) {
this.to = to;
this.cost = cost;
}
}
}
//Test Case 1
/*
์
๋ ฅ
5
6
1 2 2
1 3 3
2 3 2
2 4 4
3 4 1
4 5 1
1
0 2 3 4 5
*/
//Test Case 2
/*
์
๋ ฅ
4
4
1 2 1
2 3 2
3 4 3
1 4 10
1
์ถ๋ ฅ
0 1 3 6
*/
//Tast Case 3
/*
์
๋ ฅ
3
2
1 2 5
2 3 2
2
์ถ๋ ฅ
7 0 2
*/