반응형

https://www.acmicpc.net/problem/1800

 

1800번: 인터넷 설치

첫 번째 줄에 N(1 ≤ N ≤ 1,000), 케이블선의 개수 P(1 ≤ P ≤ 10,000), 공짜로 제공하는 케이블선의 개수 K(0 ≤ K < N)이 주어진다. 다음 P개의 줄에는 케이블이 연결하는 두 컴퓨터 번호와 그 가격이 차

www.acmicpc.net

 

이분탐색과 다익스트라를 이용하여 풀었습니다.

mid를 최소 비용으로 할 때, 1번부터 N번까지 도달할 수 있는지를 검사해줍니다.

도달할 수 있는지의 여부를 구하기 위해 다익스트라를 수행합니다.

dist[i]는 i 지점에 도달하기 위해 필요한 mid 비용을 넘는 케이블 선의 최소 개수입니다.

dist[n]이 k개 이하라면, 1번부터 N번까지 도달할 수 있습니다.

 

#include <iostream>
#include <vector>
#include <queue>
#define INF 2147483647
using namespace std;

vector<pair<int, int>> graph[1001];

int n, p, k, a, b, c;

bool dijkstra(int price) {
	vector<int> dist(n + 1, INF);
	dist[1] = 0;
	priority_queue<pair<int, int>> pq;
	pq.push({ 0, 1 });
	while (!pq.empty()) {
		int cost = -pq.top().first;
		int node = pq.top().second;
		pq.pop();
		if (dist[node] < cost) continue;
		for (int i = 0; i < graph[node].size(); i++) {
			int nextNode = graph[node][i].first;
			int nextCost = cost + (graph[node][i].second > price ? 1 : 0);
			if (dist[nextNode] > nextCost) {
				dist[nextNode] = nextCost;
				pq.push({-nextCost, nextNode});
			}
		}
	}
	return dist[n] <= k;
}

int main() {
	scanf("%d %d %d", &n, &p, &k);
	while (p--) {
		scanf("%d %d %d", &a, &b, &c);
		graph[a].push_back({ b, c });
		graph[b].push_back({ a, c });
	}
	int l = 0, r = 1e7;
	while (l <= r) {
		int mid = (l + r) / 2;
		if (dijkstra(mid)) r = mid - 1;
		else l = mid + 1;
	}
	printf("%d", r != 1e7 ? r + 1 : -1);
}
반응형

'Algorithm' 카테고리의 다른 글

프로그래머스 : 점프와 순간 이동  (0) 2021.11.15
프로그래머스 : 구명보트  (0) 2021.11.15
백준 1937 : 욕심쟁이 판다  (0) 2021.11.15
백준 10216 : Count Circle Groups  (0) 2021.11.15
백준 14923 : 미로 탈출  (0) 2021.11.15

+ Recent posts