forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.java
106 lines (90 loc) Β· 3.65 KB
/
5.java
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
import java.util.*;
class Node implements Comparable<Node> {
private int index;
private int distance;
public Node(int index, int distance) {
this.index = index;
this.distance = distance;
}
public int getIndex() {
return this.index;
}
public int getDistance() {
return this.distance;
}
// 거리(λΉμ©)κ° μ§§μ κ²μ΄ λμ μ°μ μμλ₯Ό κ°μ§λλ‘ μ€μ
@Override
public int compareTo(Node other) {
if (this.distance < other.distance) {
return -1;
}
return 1;
}
}
public class Main {
public static final int INF = (int) 1e9; // 무νμ μλ―Ένλ κ°μΌλ‘ 10μ΅μ μ€μ
// λ
Έλμ κ°μ(N), κ°μ μ κ°μ(M), μμ λ
Έλ λ²νΈ(Start)
public static int n, m, start;
// κ° λ
Έλμ μ°κ²°λμ΄ μλ λ
Έλμ λν μ 보λ₯Ό λ΄λ λ°°μ΄
public static ArrayList<ArrayList<Node>> graph = new ArrayList<ArrayList<Node>>();
// μ΅λ¨ 거리 ν
μ΄λΈ λ§λ€κΈ°
public static int[] d = new int[30001];
public static void dijkstra(int start) {
PriorityQueue<Node> pq = new PriorityQueue<>();
// μμ λ
Έλλ‘ κ°κΈ° μν μ΅λ¨ κ²½λ‘λ 0μΌλ‘ μ€μ νμ¬, νμ μ½μ
pq.offer(new Node(start, 0));
d[start] = 0;
while(!pq.isEmpty()) { // νκ° λΉμ΄μμ§ μλ€λ©΄
// κ°μ₯ μ΅λ¨ κ±°λ¦¬κ° μ§§μ λ
Έλμ λν μ 보 κΊΌλ΄κΈ°
Node node = pq.poll();
int dist = node.getDistance(); // νμ¬ λ
ΈλκΉμ§μ λΉμ©
int now = node.getIndex(); // νμ¬ λ
Έλ
// νμ¬ λ
Έλκ° μ΄λ―Έ μ²λ¦¬λ μ μ΄ μλ λ
ΈλλΌλ©΄ 무μ
if (d[now] < dist) continue;
// νμ¬ λ
Έλμ μ°κ²°λ λ€λ₯Έ μΈμ ν λ
Έλλ€μ νμΈ
for (int i = 0; i < graph.get(now).size(); i++) {
int cost = d[now] + graph.get(now).get(i).getDistance();
// νμ¬ λ
Έλλ₯Ό κ±°μ³μ, λ€λ₯Έ λ
Έλλ‘ μ΄λνλ κ±°λ¦¬κ° λ 짧μ κ²½μ°
if (cost < d[graph.get(now).get(i).getIndex()]) {
d[graph.get(now).get(i).getIndex()] = cost;
pq.offer(new Node(graph.get(now).get(i).getIndex(), cost));
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
start = sc.nextInt();
// κ·Έλν μ΄κΈ°ν
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<Node>());
}
// λͺ¨λ κ°μ μ 보λ₯Ό μ
λ ₯λ°κΈ°
for (int i = 0; i < m; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
// Xλ² λ
Έλμμ Yλ² λ
Έλλ‘ κ°λ λΉμ©μ΄ ZλΌλ μλ―Έ
graph.get(x).add(new Node(y, z));
}
// μ΅λ¨ 거리 ν
μ΄λΈμ λͺ¨λ 무νμΌλ‘ μ΄κΈ°ν
Arrays.fill(d, INF);
// λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ μν
dijkstra(start);
// λλ¬ν μ μλ λ
Έλμ κ°μ
int count = 0;
// λλ¬ν μ μλ λ
Έλ μ€μμ, κ°μ₯ λ©λ¦¬ μλ λ
Έλμμ μ΅λ¨ 거리
int maxDistance = 0;
for (int i = 1; i <= n; i++) {
// λλ¬ν μ μλ λ
ΈλμΈ κ²½μ°
if (d[i] != INF) {
count += 1;
maxDistance = Math.max(maxDistance, d[i]);
}
}
// μμ λ
Έλλ μ μΈν΄μΌ νλ―λ‘ count - 1μ μΆλ ₯
System.out.println((count - 1) + " " + maxDistance);
}
}