Skip to content

Commit 808bcfe

Browse files
committed
Update
1 parent 103f9e9 commit 808bcfe

File tree

1 file changed

+110
-13
lines changed

1 file changed

+110
-13
lines changed

9/1.java

Lines changed: 110 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,110 @@
1-
6 11
2-
1
3-
1 2 2
4-
1 3 5
5-
1 4 1
6-
2 3 3
7-
2 4 2
8-
3 2 3
9-
3 6 5
10-
4 3 3
11-
4 5 1
12-
5 3 1
13-
5 6 2
1+
import java.util.*;
2+
3+
class Node {
4+
5+
private int index;
6+
private int distance;
7+
8+
public Node(int index, int distance) {
9+
this.index = index;
10+
this.distance = distance;
11+
}
12+
13+
public int getIndex() {
14+
return this.index;
15+
}
16+
17+
public int getDistance() {
18+
return this.distance;
19+
}
20+
}
21+
22+
public class Main {
23+
24+
public static final int INF = (int) 1e9; // 무한을 의미하는 값으로 10억을 설정
25+
// 노드의 개수(N), 간선의 개수(M), 시작 노드 번호(Start)
26+
// 노드의 개수는 최대 100,000개라고 가정
27+
public static int n, m, start;
28+
// 각 노드에 연결되어 있는 노드에 대한 정보를 담는 배열
29+
public static ArrayList<ArrayList<Node>> graph = new ArrayList<ArrayList<Node>>();
30+
// 방문한 적이 있는지 체크하는 목적의 배열 만들기
31+
public static boolean[] visited = new boolean[100001];
32+
// 최단 거리 테이블 만들기
33+
public static int[] d = new int[100001];
34+
35+
// 방문하지 않은 노드 중에서, 가장 최단 거리가 짧은 노드의 번호를 반환
36+
public static int getSmallestNode() {
37+
int min_value = INF;
38+
int index = 0; // 가장 최단 거리가 짧은 노드(인덱스)
39+
for (int i = 1; i <= n; i++) {
40+
if (d[i] < min_value && !visited[i]) {
41+
min_value = d[i];
42+
index = i;
43+
}
44+
}
45+
return index;
46+
}
47+
48+
public static void dijkstra(int start) {
49+
// 시작 노드에 대해서 초기화
50+
d[start] = 0;
51+
visited[start] = true;
52+
for (int j = 0; j < graph.get(start).size(); j++) {
53+
d[graph.get(start).get(j).getIndex()] = graph.get(start).get(j).getDistance();
54+
}
55+
// 시작 노드를 제외한 전체 n - 1개의 노드에 대해 반복
56+
for (int i = 0; i < n - 1; i++) {
57+
// 현재 최단 거리가 가장 짧은 노드를 꺼내서, 방문 처리
58+
int now = getSmallestNode();
59+
visited[now] = true;
60+
// 현재 노드와 연결된 다른 노드를 확인
61+
for (int j = 0; j < graph.get(now).size(); j++) {
62+
int cost = d[now] + graph.get(now).get(j).getDistance();
63+
// 현재 노드를 거쳐서 다른 노드로 이동하는 거리가 더 짧은 경우
64+
if (cost < d[graph.get(now).get(j).getIndex()]) {
65+
d[graph.get(now).get(j).getIndex()] = cost;
66+
}
67+
}
68+
}
69+
}
70+
71+
public static void main(String[] args) {
72+
Scanner sc = new Scanner(System.in);
73+
74+
n = sc.nextInt();
75+
m = sc.nextInt();
76+
start = sc.nextInt();
77+
78+
// 그래프 초기화
79+
for (int i = 0; i <= n; i++) {
80+
graph.add(new ArrayList<Node>());
81+
}
82+
83+
// 모든 간선 정보를 입력받기
84+
for (int i = 0; i < m; i++) {
85+
int a = sc.nextInt();
86+
int b = sc.nextInt();
87+
int c = sc.nextInt();
88+
// a번 노드에서 b번 노드로 가는 비용이 c라는 의미
89+
graph.get(a).add(new Node(b, c));
90+
}
91+
92+
// 최단 거리 테이블을 모두 무한으로 초기화
93+
Arrays.fill(d, INF);
94+
95+
// 다익스트라 알고리즘을 수행
96+
dijkstra(start);
97+
98+
// 모든 노드로 가기 위한 최단 거리를 출력
99+
for (int i = 1; i <= n; i++) {
100+
// 도달할 수 없는 경우, 무한(INFINITY)이라고 출력
101+
if (d[i] == INF) {
102+
System.out.println("INFINITY");
103+
}
104+
// 도달할 수 있는 경우 거리를 출력
105+
else {
106+
System.out.println(d[i]);
107+
}
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)