Repository navigation
Expand file tree
/
Copy path1939.java
More file actions
108 lines (84 loc) · 2.41 KB
/
Copy path1939.java
File metadata and controls
108 lines (84 loc) · 2.41 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
// Copyright@2023 Jihoon Lucas Kim <jihoon.lucas.kim@gmail.com>
// 중량제한
// https://www.acmicpc.net/problem/1939
// 힌트
// 1. BFS를 활용하여 지정한 중량으로 공장이 이어질 수 있는지 찾는다.
// 2. 여기서 중량의 범위가 방대하므로 binary search를 통해 값을 찾아낸다.
// 이때, 복잡도는 O(nlogc)이다.
import java.io.*;
import java.util.*;
class Node {
int to;
int weight;
public Node(int to, int weight) {
this.to = to;
this.weight = weight;
}
}
public class Main {
static int N, M;
static int loc1, loc2, maxWeight;
static HashMap<Integer, ArrayList<Node>> adj;
static boolean[] check = new boolean[100001];
public static void main(String[] args) throws IOException, NumberFormatException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
adj = new HashMap<>();
maxWeight = 0;
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int n1 = Integer.parseInt(st.nextToken());
int n2 = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
addNodeToMap(n1, n2, weight);
addNodeToMap(n2, n1, weight);
maxWeight = Math.max(weight, maxWeight);
}
st = new StringTokenizer(br.readLine());
loc1 = Integer.parseInt(st.nextToken());
loc2 = Integer.parseInt(st.nextToken());
int answer = bs();
System.out.println(answer);
}
static void addNodeToMap(int from, int to, int weight) {
if (adj.containsKey(from)) {
adj.get(from).add(new Node(to, weight));
} else {
adj.put(from, new ArrayList<Node>() {{add(new Node(to, weight));}});
}
}
static int bs() {
int start = 1;
int end = maxWeight;
int mid = -1;
while (start <= end) {
mid = (start + end) / 2;
if (bfs(mid)) {
start = mid + 1;
} else {
end = mid - 1;
}
Arrays.fill(check, false);
}
return end;
}
static boolean bfs(int val) {
Queue<Integer> q = new LinkedList<>();
q.add(loc1);
check[loc1] = true;
while(!q.isEmpty()) {
int from = q.poll();
for (Node v : adj.get(from)) {
int nextNode = v.to;
int cost = v.weight;
if (!check[nextNode] && cost >= val) {
check[nextNode] = true;
q.add(nextNode);
}
}
}
return check[loc2];
}
}