Skip to content

Commit dbdeb3a

Browse files
beberichetony9402
andauthored
[ADD] baekjoon 17396 java solution (#87)
* [ADD] baekjoon 17396 java solution * Refactoring --------- Co-authored-by: Minsang Kim <[email protected]>
1 parent 51654bb commit dbdeb3a

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

solutions/baekjoon/17396/Main.java

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Authored by: beberiche
2+
// Co-authored by: -
3+
// Link: http://boj.kr/f6965f3f15f04d6db896babd4720c86e
4+
5+
import java.util.*;
6+
import java.io.*;
7+
8+
public class Main {
9+
public static void main(String[] args) {
10+
FastReader rd = new FastReader();
11+
int N = rd.nextInt();
12+
int M = rd.nextInt();
13+
14+
int[] a = new int[N];
15+
for (int i = 0; i < N; i++) {
16+
a[i] = rd.nextInt();
17+
}
18+
19+
List<Node> list[] = new ArrayList[N];
20+
for (int i = 0; i < N; i++) {
21+
list[i] = new ArrayList<>();
22+
}
23+
24+
for (int i = 0; i < M; i++) {
25+
int n1 = rd.nextInt();
26+
int n2 = rd.nextInt();
27+
int dist = rd.nextInt();
28+
29+
boolean check1 = check(n1, a);
30+
boolean check2 = check(n2, a);
31+
32+
if (check1 && check2) {
33+
list[n1].add(new Node(n2, dist));
34+
list[n2].add(new Node(n1, dist));
35+
}
36+
}
37+
38+
long[] dist = new long[N];
39+
long INF = (long) 3e11 + 4;
40+
Arrays.fill(dist, INF);
41+
dist[0] = 0;
42+
43+
PriorityQueue<Node> pq = new PriorityQueue<>();
44+
pq.add(new Node(0, 0));
45+
while (!pq.isEmpty()) {
46+
Node curr = pq.poll();
47+
48+
if (dist[curr.id] < curr.dist) continue;
49+
50+
for (Node next : list[curr.id]) {
51+
if (dist[next.id] > curr.dist + next.dist) {
52+
dist[next.id] = curr.dist + next.dist;
53+
pq.add(new Node(next.id, dist[next.id]));
54+
}
55+
}
56+
}
57+
58+
// System.out.println(-1);
59+
long ans = dist[N - 1] == INF ? -1 : dist[N - 1];
60+
System.out.println(ans);
61+
}
62+
63+
private static boolean check(int node, int[] a) {
64+
return node == a.length - 1 || a[node] == 0;
65+
}
66+
67+
private static class Node implements Comparable<Node> {
68+
int id;
69+
long dist;
70+
71+
72+
Node(int id, long dist) {
73+
this.id = id;
74+
this.dist = dist;
75+
}
76+
77+
@Override
78+
public int compareTo(Node o) {
79+
return Long.compare(this.dist, o.dist);
80+
}
81+
}
82+
83+
static class FastReader {
84+
BufferedReader br;
85+
StringTokenizer st;
86+
87+
public FastReader() {
88+
br = new BufferedReader(new InputStreamReader(System.in));
89+
}
90+
91+
String next() {
92+
while (st == null || !st.hasMoreElements()) {
93+
try {
94+
st = new StringTokenizer(br.readLine());
95+
} catch (IOException e) {
96+
e.printStackTrace();
97+
}
98+
}
99+
return st.nextToken();
100+
}
101+
102+
int nextInt() {
103+
return Integer.parseInt(next());
104+
}
105+
106+
long nextLong() {
107+
return Long.parseLong(next());
108+
}
109+
110+
double nextDouble() {
111+
return Double.parseDouble(next());
112+
}
113+
114+
String nextLine() {
115+
String str = "";
116+
try {
117+
str = br.readLine();
118+
} catch (IOException e) {
119+
e.printStackTrace();
120+
}
121+
return str;
122+
}
123+
}
124+
}
125+
126+
/* Solution Description
127+
128+
1. 다익스트라 응용 문제. 일반적인 다익스트라 치고 간선의 수, 비용의 최대치가 꽤 큰 편이다.
129+
이를 최적화 할 수 있는 식이 필요한 문제이다.
130+
131+
2. 결과적으로 유섭이가 백도어에 성공하기 위해서는, 넥서스 외에 시야가 보이는 거점을 지나쳐서는 안된다.
132+
따라서, 그래프를 형성할 때, 주어지는 입력값의 거점이 **넥서스가 아님에도, 상대의 시야에 보이는 거점** 이라면 그래프 연결에 제외하도록 했다.
133+
134+
3. 더불어, `dist[]` 내에 저장된 임의의 노드 `i` 에 대한 최단 비용보다,
135+
탐색 시의 `dist` 비용이 더 크다면 다음 탐색을 하는 방법으로 탐색 시간을 줄였다.
136+
137+
*/

0 commit comments

Comments
 (0)