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 66a4c96

Browse files
백제완: [BOJ] 12784 인하니카 공화국_250304
1 parent ba49487 commit 66a4c96

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

‎BOJ/10001-15000번/JW_12784.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.ArrayList;
2+
3+
public class JW_12784 {
4+
5+
static final int INF = Integer.MAX_VALUE;
6+
7+
static ArrayList<ArrayList<int[]>> graph;
8+
static boolean[] visited;
9+
10+
public static void main(String[] args) throws Exception {
11+
int t = read();
12+
StringBuilder sb = new StringBuilder();
13+
while (t-- > 0) {
14+
int n = read(), m = read();
15+
if(n == 1) {
16+
sb.append(0).append("\n");
17+
continue;
18+
}
19+
graph = new ArrayList<>();
20+
for (int i = 0; i < n + 1; i++)
21+
graph.add(new ArrayList<>());
22+
visited = new boolean[n + 1];
23+
int u, v, d;
24+
for (int i = 0; i < m; i++) {
25+
u = read();
26+
v = read();
27+
d = read();
28+
graph.get(u).add(new int[] { v, d });
29+
graph.get(v).add(new int[] { u, d });
30+
}
31+
sb.append(recursive(new int[] { 1, INF })).append("\n");
32+
}
33+
System.out.println(sb);
34+
}
35+
36+
private static int recursive(int[] u) {
37+
visited[u[0]] = true;
38+
int minD = 0;
39+
// 자식이 가지는 모든 값
40+
for (int[] v : graph.get(u[0]))
41+
if (!visited[v[0]])
42+
minD += recursive(v);
43+
// 현재 섬으로 오는 가중치와 자식으로 가는 간선 가중치의 합을 비교
44+
return Math.min(minD == 0 ? INF : minD, u[1]); // 갱신되지 않았을 경우 INF 반환
45+
}
46+
47+
private static int read() throws Exception {
48+
int c, n = System.in.read() & 15;
49+
while ((c = System.in.read()) >= 48)
50+
n = (n << 3) + (n << 1) + (c & 15);
51+
if (c == 13)
52+
System.in.read();
53+
return n;
54+
}
55+
}

0 commit comments

Comments
(0)

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