forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path4.java
More file actions
114 lines (96 loc) Β· 3.96 KB
/
4.java
File metadata and controls
114 lines (96 loc) Β· 3.96 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
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)
public static int n, m;
// μμ λ
Έλλ₯Ό 1λ² νκ°μΌλ‘ μ€μ
public static int start = 1;
// κ° λ
Έλμ μ°κ²°λμ΄ μλ λ
Έλμ λν μ 보λ₯Ό λ΄λ 리μ€νΈλ₯Ό λ§λ€κΈ°
public static ArrayList<ArrayList<Node>> graph = new ArrayList<ArrayList<Node>>();
// μ΅λ¨ 거리 ν
μ΄λΈ λ§λ€κΈ°
public static int[] d = new int[20001];
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();
// κ·Έλν μ΄κΈ°ν
for (int i = 0; i <= n; i++) {
graph.add(new ArrayList<Node>());
}
// λͺ¨λ κ°μ μ 보λ₯Ό μ
λ ₯λ°κΈ°
for (int i = 0; i < m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
// aλ² λ
Έλμ bλ² λ
Έλμ μ΄λ λΉμ©μ΄ 1μ΄λΌλ μλ―Έ(μλ°©ν₯)
graph.get(a).add(new Node(b, 1));
graph.get(b).add(new Node(a, 1));
}
// μ΅λ¨ 거리 ν
μ΄λΈμ λͺ¨λ 무νμΌλ‘ μ΄κΈ°ν
Arrays.fill(d, INF);
// λ€μ΅μ€νΈλΌ μκ³ λ¦¬μ¦μ μν
dijkstra(start);
// κ°μ₯ μ΅λ¨ κ±°λ¦¬κ° λ¨Ό λ
Έλ λ²νΈ(λλΉμ΄κ° μ¨μ νκ°μ λ²νΈ)
int maxNode = 0;
// λλ¬ν μ μλ λ
Έλ μ€μμ, κ°μ₯ μ΅λ¨ κ±°λ¦¬κ° λ¨Ό λ
Έλμμ μ΅λ¨ 거리
int maxDistance = 0;
// κ°μ₯ μ΅λ¨ κ±°λ¦¬κ° λ¨Ό λ
Έλμμ μ΅λ¨ 거리μ λμΌν μ΅λ¨ 거리λ₯Ό κ°μ§λ λ
Έλλ€μ 리μ€νΈ
ArrayList<Integer> result = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
if (maxDistance < d[i]) {
maxNode = i;
maxDistance = d[i];
result.clear();
result.add(maxNode);
}
else if (maxDistance == d[i]) {
result.add(i);
}
}
System.out.println(maxNode + " " + maxDistance + " " + result.size());
}
}