-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstra.java
More file actions
71 lines (57 loc) · 1.83 KB
/
Copy pathDijkstra.java
File metadata and controls
71 lines (57 loc) · 1.83 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
package algorithm.module;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Dijkstra {
public void init(){
Scanner sc = new Scanner(System.in);
int v = sc.nextInt();
int e = sc.nextInt();
int start = sc.nextInt();
// 그래프 연결관계 만들고
List<List<Node>> graph = new ArrayList<>();
for (int i = 0; i < v+1; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < e; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int cost = sc.nextInt();
graph.get(a).add(new Node(b, cost));
}
// 방문여부, 거리 배열 만들기
boolean[] visited = new boolean[v+1];
int[] dist = new int[v+1];
for (int i = 0; i < v + 1; i++) {
dist[i] = Integer.MAX_VALUE;
}
dist[start] = 0;
// 최소 비용 노드 선택
for (int i = 0; i < v; i++) {
int nodeValue = Integer.MAX_VALUE;
int nodeIdx = 0;
for (int j = 0; j < v + 1; j++) {
if(!visited[j] && dist[j]<nodeValue){
nodeValue = dist[j];
nodeIdx = j;
}
}
visited[nodeIdx] = true;
for (int j = 0; j < graph.get(nodeIdx).size(); j++) {
Node adjNode = graph.get(nodeIdx).get(j);
if(dist[adjNode.idx] > dist[nodeIdx] + adjNode.cost){
dist[adjNode.idx] = dist[nodeIdx] + adjNode.cost;
}
}
}
// 거리값 갱신
}
}
class Node{
int idx;
int cost;
Node (int idx, int cost){
this.idx = idx;
this.cost = cost;
}
}