반응형
https://www.acmicpc.net/problem/1939
1939번: 중량제한
첫째 줄에 N, M(1 ≤ M ≤ 100,000)이 주어진다. 다음 M개의 줄에는 다리에 대한 정보를 나타내는 세 정수 A, B(1 ≤ A, B ≤ N), C(1 ≤ C ≤ 1,000,000,000)가 주어진다. 이는 A번 섬과 B번 섬 사이에 중량제한이
www.acmicpc.net
#include <bits/stdc++.h>
using namespace std;
int n, m;
int startIsland, endIsland;
vector<pair<int, int> > adj[10001];
bool BFS(int st, int en, int weight){
bool vis[10001];
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(st); vis[st] = 1;
while(!Q.empty()){
int cur = Q.front(); Q.pop();
if(cur == en){
return true;
}
for(auto nx : adj[cur]){
if(!vis[nx.first]&&nx.second >= weight){
vis[nx.first] = 1; Q.push(nx.first);
}
}
}
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=0; i<m; i++){
int a,b,c;
cin >> a >> b >> c;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
cin >> startIsland >> endIsland;
//이분탐색
int st = 1, en = 1000000000;
while(st <= en){
int mid = (st+en)/2;
if(BFS(startIsland, endIsland, mid)){
st = mid+1;
}
else{
en = mid-1;
}
}
cout << en << '\n';
return 0;
}
반응형
'Algorithm > problem' 카테고리의 다른 글
백준 6236번 : 용돈 관리 - 이분탐색(매개변수 탐색) C++ (0) | 2022.07.03 |
---|---|
백준 2343: 기타레슨 - 이분탐색(Parameter Searche) C++ (0) | 2022.07.03 |
백준 15990번 : 1,2,3 더하기 5 - DP (0) | 2022.07.03 |
백준 14226번 : 이모티콘 - BFS (0) | 2022.07.02 |
[백준] 1963번 : 소수경로 - 에라토스테네스의 체, BFS (0) | 2022.07.02 |