-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path1939.cpp
More file actions
86 lines (79 loc) · 1.73 KB
/
Copy path1939.cpp
File metadata and controls
86 lines (79 loc) · 1.73 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
// Copyright@2023 Jihoon Lucas Kim <jihoon.lucas.kim@gmail.com>
// 중량제한
// https://www.acmicpc.net/problem/1939
// 힌트
// 1. BFS를 활용하여 지정한 중량으로 공장이 이어질 수 있는지 찾는다.
// 2. 여기서 중량의 범위가 방대하므로 binary search를 통해 값을 찾아낸다.
// 이때, 복잡도는 O(nlogc)이다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
int n, m, a, b, c;
int loc1, loc2, weight_max = 0;
int answer = 1;
struct from
{
int to;
int weight;
};
vector<from> v[100001];
vector<bool> check(100001);
void bfs(int mid)
{
queue<int> q;
check[loc1] = true;
q.push(loc1);
while (!q.empty())
{
int node = q.front();
q.pop();
for (int i = 0; i < v[node].size(); i++)
{
from next = v[node][i];
int n_node = next.to;
int n_cost = next.weight;
if (!check[n_node] && n_cost >= mid)
{
check[n_node] = true;
q.push(n_node);
}
}
}
}
void bs()
{
int start = 1;
int end = weight_max;
int mid = -1;
while (start <= end)
{
mid = (start + end) / 2;
check = vector<bool>(100001, false);
bfs(mid);
if (check[loc2])
{
answer = max(answer, mid);
start = mid + 1;
}
else
{
end = mid - 1;
}
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < m; i++)
{
cin >> a >> b >> c;
v[a].push_back({b, c});
v[b].push_back({a, c});
weight_max = max(weight_max, c);
}
cin >> loc1 >> loc2;
bs();
cout << answer << endl;
}