Skip to content

Commit 29c0c9b

Browse files
committed
BOJ_1939 : 중량제한
1 parent c476cbc commit 29c0c9b

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

week5/BOJ_1939(중량제한).java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static int N;
7+
public static List<List<int[]>> graph;
8+
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
N = Integer.parseInt(st.nextToken());
13+
int M = Integer.parseInt(st.nextToken());
14+
graph = new ArrayList<>();
15+
for (int i = 0; i <= N; i++) {
16+
graph.add(new ArrayList<>());
17+
}
18+
int max = 0;
19+
int min = Integer.MAX_VALUE;
20+
for (int i = 0; i < M; i++) {
21+
st = new StringTokenizer(br.readLine());
22+
int A = Integer.parseInt(st.nextToken());
23+
int B = Integer.parseInt(st.nextToken());
24+
int C = Integer.parseInt(st.nextToken());
25+
graph.get(A).add(new int[]{B, C});
26+
graph.get(B).add(new int[]{A, C});
27+
max = Math.max(max, C);
28+
min = Math.min(min, C);
29+
}
30+
st = new StringTokenizer(br.readLine());
31+
int start = Integer.parseInt(st.nextToken());
32+
int end = Integer.parseInt(st.nextToken());
33+
34+
int left = min;
35+
int right = max;
36+
int answer = left;
37+
while (left <= right) {
38+
int mid = (left + right) / 2;
39+
if (!checkPathWeight(start, end, mid)) {
40+
right = mid - 1;
41+
} else {
42+
answer = Math.max(answer, mid);
43+
left = mid + 1;
44+
}
45+
}
46+
System.out.println(answer);
47+
}
48+
49+
public static boolean checkPathWeight(int start, int end, int weight) {
50+
boolean[] visited = new boolean[N + 1];
51+
Queue<Integer> q = new LinkedList<>();
52+
q.offer(start);
53+
while (!q.isEmpty()) {
54+
int v = q.poll();
55+
if (v == end) {
56+
return true;
57+
}
58+
List<int[]> list = graph.get(v);
59+
for (int i = 0; i < list.size(); i++) {
60+
int[] edge = list.get(i);
61+
int vertex = edge[0];
62+
if (edge[1] > 0 && edge[1] >= weight && !visited[vertex]) {
63+
visited[vertex] = true;
64+
q.offer(vertex);
65+
}
66+
}
67+
}
68+
return false;
69+
}
70+
}

0 commit comments

Comments
 (0)