※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 16211번 문제인 백채원이다.
문제는 아래 링크를 확인하자.

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

 

16211번: 백채원

첫째 줄에는 우리나라에 있는 지점의 수 N과 도로의 수 M, 백채원의 추종자의 수 K가 차례대로 주어진다. (1 ≤ N ≤ 200,000, 1 ≤ M ≤ 500,000, 1 ≤ K ≤ N-1) 둘째 줄부터 M개의 줄에 차례대로 M개의 각

www.acmicpc.net

문제에서 찾는 지점들은 "1번지점에서의 거리가 K개의 지점에서부터의 거리보다 짧은 지점"들과 같음을 관찰하자.

 

따라서 1번 지점으로부터 각 지점까지의 거리와 K개의 지점으로부터 각 지점까지의 거리를 미리 구해두고, 두 값의 크기를 노드 번호 오름차순으로 살펴보는 것으로 문제를 해결할 수 있다. 거리의 경우 전자는 1번 지점을 source로 하는 dijkstra, 후자는 K개의 지점을 source로 하는 multi-source dijkstra를 이용해 계산해낼 수 있다. 

 

아래는 제출한 소스코드이다.

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int N, M, K;
vector<pair<int, int>> G[200001];
int dist1[200001], dist2[200001];

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;

void dijkstra1() {
	while (!pq.empty()) {
		int d = pq.top().first, cur = pq.top().second; pq.pop();
		if (dist1[cur] < d) continue;
		for (auto& p : G[cur]) {
			int nxt = p.first, w = p.second;
			if (dist1[nxt] == 0 || dist1[nxt] > d + w) {
				dist1[nxt] = d + w;
				pq.push(make_pair(d + w, nxt));
			}
		}
	}
}

void dijkstra2() {
	while (!pq.empty()) {
		int d = pq.top().first, cur = pq.top().second; pq.pop();
		if (dist2[cur] < d) continue;
		for (auto& p : G[cur]) {
			int nxt = p.first, w = p.second;
			if (dist2[nxt] == 0 || dist2[nxt] > d + w) {
				dist2[nxt] = d + w;
				pq.push(make_pair(d + w, nxt));
			}
		}
	}
}

bool chk;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> N >> M >> K;
	while (M--) {
		int x, y, w; cin >> x >> y >> w;
		G[x].emplace_back(make_pair(y, w));
		G[y].emplace_back(make_pair(x, w));
	}
	
	dist1[1] = 1;
	pq.push(make_pair(1, 1));
	dijkstra1();

	while (K--) {
		int x; cin >> x;
		dist2[x] = 1;
		pq.push(make_pair(1, x));
	}
	dijkstra2();
	
	for (int i = 2; i <= N; i++) {
		if (dist1[i] < dist2[i]) cout << i << ' ', chk = 1;
	}

	if (!chk) cout << 0;
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 24621 // C++] Photoshoot 2  (0) 2023.09.20
[BOJ 9925 // C++] Scout Outing  (1) 2023.09.19
[BOJ 3024 // C++] 마라톤 틱택토  (0) 2023.09.17
[BOJ 22487 // C++] Do use segment tree  (0) 2023.09.16
[BOJ 16206 // C++] 롤케이크  (0) 2023.09.15

+ Recent posts