-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263#include<iostream>#include<vector>#include<queue>#include<algorithm>#include<cstring>using namespace std;int n, m;int sx, fx;vector<pair<int, int> > v[10001];bool d[10001];bool bfs(int value) {queue<int> q;q.push(sx);d[sx] = true;while (!q.empty()) {int x = q.front();q.pop();if (x == fx) return true;for (int i = 0; i < v[x].size(); i++) {int next = v[x][i].first;int nv = v[x][i].second;if (!d[next] && value <= nv) {d[next] = true;q.push(next);}}}return false;}int main() {ios_base::sync_with_stdio(0);cin.tie(0);cin >> n >> m;int maxx = 0;for (int i = 0; i < m; i++) {int a, b, c;cin >> a >> b >> c;v[a].push_back({ b,c });v[b].push_back({ a,c });maxx = max(maxx, c);}cin >> sx >> fx;int L = 0; int R = maxx;while (L <= R) {int mid = (L + R) / 2;if (bfs(mid)) {L = mid + 1;}else {R = mid - 1;}for (int i = 1; i <= n; i++)d[i] = false;}cout << R << '\n';return 0;}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
BFS를 통한 확인 -> 이분탐색하기문제
쉬운문제는 아니다 여러번 틀렸었고
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980#include<iostream>#include<vector>#include<queue>#include<utility>using namespace std;vector<pair<int, long>> v[10001];bool d[10001];long long dist[10001];int n, m;int sx, ex, ans;void bfs(int x) {queue<pair<int, int> > q;d[x] = true;for (int i = 0; i < v[x].size(); i++) {q.push({ v[x][i].first, v[x][i].second });d[v[x][i].first] = true;dist[v[x][i].first] = v[x][i].second;}while (!q.empty()) {x = q.front().first; int val = q.front().second;q.pop();if (x == ex) {if (val > ans) {ans = val;continue;//cout << val << '\n';}}if (val <= ans || dist[x] <= ans)continue;for (int i = 0; i < v[x].size(); i++) {//if (v[x][i].second < val) continue;if (d[v[x][i].first] == false) {d[v[x][i].first] = true;if (v[x][i].second < val) {q.push({ v[x][i].first , v[x][i].second });dist[v[x][i].first] = v[x][i].second;}else {q.push({ v[x][i].first, val });dist[v[x][i].first] = val;}}else {if (dist[v[x][i].first] < val && v[x][i].second > dist[v[x][i].first]) {if (v[x][i].second > val) {q.push({ v[x][i].first, val });dist[v[x][i].first] = val;}else {q.push({ v[x][i].first, v[x][i].second });dist[v[x][i].first] = v[x][i].second;}}}}}}int main() {ios_base::sync_with_stdio(0);cin.tie(0);cin >> n >> m;for (int i = 0; i < m; i++) {int a, b; long long c;cin >> a >> b >> c;v[a].push_back({ b,c });v[b].push_back({ a,c });}cin >> sx >> ex;bfs(sx);cout << ans << '\n';return 0;}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter다음같이 순수 BFS로 짜게되면 경우의수가 너무많아진다. 돌았던곳을 여러번 돌아야하는것은 효율적이지 못하고
이를 최대한 제거하면서 할수있는게, 출발 노드로부터 반개이상의 노드의 값들을 확인하면서 진행하는
이분탐색이 해당문제풀이에 알맞다
'백준알고리즘' 카테고리의 다른 글
연구소 (0) 2019.10.04 가스관 (0) 2019.10.03 맥주 마시면서 걸어가기 (0) 2019.10.01 (4991) 로봇 청소기 (0) 2019.10.01 택배 (0) 2019.09.29