Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd24568

Browse files
committed
이예진: [PG] 118669 등산코스 정하기_241213
1 parent afebc6e commit fd24568

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

‎Programmers/Level3/YJ_118669.java‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import java.util.*;
2+
3+
public class YJ_118669 {
4+
class Node implements Comparable<Node> {
5+
int index;
6+
int distance;
7+
8+
public Node(int index, int distance) {
9+
this.index = index;
10+
this.distance = distance;
11+
}
12+
13+
@Override
14+
public int compareTo(Node o) {
15+
return this.distance - o.distance;
16+
}
17+
}
18+
19+
List<ArrayList<Node>> graph = new ArrayList<>();
20+
int[] table;
21+
Set<Integer> summitSet = new HashSet<>();
22+
public int[] solution(int n, int[][] paths, int[] gates, int[] summits) {
23+
//산봉우리 중복제거
24+
for(int mountain : summits){
25+
summitSet.add(mountain);
26+
}
27+
//최소 가중치 테이블 초기화
28+
table = new int[n+1];
29+
Arrays.fill(table, Integer.MAX_VALUE);
30+
//그래프 모델링
31+
for(int i=0; i<n+1; i++){
32+
graph.add(new ArrayList<>());
33+
}
34+
for (int[] path : paths) {
35+
int to = path[0];
36+
int from = path[1];
37+
int distance = path[2];
38+
graph.get(to).add(new Node(from, distance));
39+
graph.get(from).add(new Node(to, distance));
40+
}
41+
42+
dijkstra(gates);
43+
44+
int[] answer = {0,Integer.MAX_VALUE};
45+
for (int mountain : summits) {
46+
int intensity = answer[1];
47+
if (table[mountain] < intensity) { //intensity의 최솟값
48+
answer[0] = mountain;
49+
answer[1] = table[mountain];
50+
} else if (table[mountain] == intensity && mountain < answer[0]) {
51+
answer[0] = mountain; //최소 intensity 가 여러개일 경우 낮은 등산코스
52+
}
53+
}
54+
return answer;
55+
}
56+
57+
// 휴식 없이 이동해야 하는 시간 중 가장 긴 시간을 해당 등산코스의 intensity (가중치)
58+
// 출입구는 처음과 끝에 한 번씩, 산봉우리는 한 번만 포함 -> 출발 > 산봉우리 까지의 거리
59+
void dijkstra(int[] gates){
60+
PriorityQueue<Node> pq = new PriorityQueue<>();
61+
//출발지점으로 초기화
62+
for (int gate : gates) {
63+
pq.offer(new Node(gate, 0));
64+
table[gate] = 0;
65+
}
66+
67+
while(!pq.isEmpty()){
68+
Node node = pq.poll();
69+
int index = node.index;
70+
int distance = node.distance;
71+
72+
if(table[index] < distance){
73+
continue;
74+
}
75+
if(summitSet.contains(index)){ //현재 노드가 산봉우리라면 패스
76+
continue;
77+
}
78+
79+
for(Node next : graph.get(index)){
80+
int nextIndex = next.index;
81+
int cost = Math.max(table[index], next.distance);
82+
if(table[nextIndex] > cost){
83+
table[nextIndex] = cost;
84+
pq.offer(new Node(nextIndex, cost));
85+
}
86+
}
87+
}
88+
}
89+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /