제출 코드
- 사용 알고리즘 :
다익스트라
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
package gold;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class G1753_shortestPath {
static class Node{
public int to;
public int weight;
public Node(int to, int weight){
this.to = to;
this.weight = weight;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] line = br.readLine().split(" ");
int V = Integer.parseInt(line[0]);
int E = Integer.parseInt(line[1]);
int start = Integer.parseInt(br.readLine());
List<Node>[] list = new LinkedList[V+1];
for(int i=1; i<=V; i++) list[i] = new LinkedList<>();
for(int i=0; i<E; i++) {
line = br.readLine().split(" ");
int u = Integer.parseInt(line[0]);
int v = Integer.parseInt(line[1]);
int w = Integer.parseInt(line[2]);
list[u].add(new Node(v, w));
}
int[] distance = new int[V+1];
boolean[] visit = new boolean[V+1];
Arrays.fill(distance, Integer.MAX_VALUE);
distance[start] = 0;
int current = 0;
for(int v=0; v<V; v++) {
int min = Integer.MAX_VALUE;
for(int i=1; i<=V; i++) {
if(!visit[i] && distance[i]<min) {
min = distance[i];
current = i;
}
}
visit[current] = true;
for(Node no : list[current]) {
if(!visit[no.to] && min+no.weight<distance[no.to]) {
distance[no.to] = min + no.weight;
}
}
}
for(int v=1; v<=V; v++) {
System.out.println(distance[v]==Integer.MAX_VALUE ? "INF" : distance[v]);
}
}
}