※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 1800번 문제인 인터넷 설치이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/1800
문제를 다른 시각으로 해석해보자. 1번 컴퓨터와
두 컴퓨터가 이어질 수 있는지를 생각해야 한다는 점을 잊지 말자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
int N, E, K;
vector<pair<int, int>> G[1001];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
int dist[1001];
bool dijkstra(int mid) {
memset(dist, 0x3f, sizeof(dist));
dist[1] = 0;
pq.push(make_pair(0, 1));
while (!pq.empty()) {
int cur = pq.top().second, d = pq.top().first; pq.pop();
if (dist[cur] < d) continue;
for (auto &p:G[cur]) {
int nxt = p.first, dd = d;
if (p.second > mid) dd++;
if (dd < dist[nxt]) dist[nxt] = dd, pq.push(make_pair(dd, nxt));
}
}
return (dist[N] <= K);
}
int L, R = 1000001;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> E >> K;
while (E--) {
int x, y, w; cin >> x >> y >> w;
G[x].emplace_back(make_pair(y, w));
G[y].emplace_back(make_pair(x, w));
}
while (L < R) {
int mid = (L + R) / 2;
if (dijkstra(mid)) R = mid;
else L = mid + 1;
}
if (L == 1000001) cout << -1;
else cout << L;
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 32953 // C++] 회상 (0) | 2024.12.24 |
---|---|
[BOJ 11643 // C++] The Magical 3 (0) | 2024.12.23 |
[BOJ 14476 // C++] 최대공약수 하나 빼기 (0) | 2024.12.19 |
[BOJ 1451 // C++] 직사각형으로 나누기 (1) | 2024.12.18 |
[BOJ 32963 // C++] 맛있는 사과 (0) | 2024.12.17 |