Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- vector
- c++
- cs
- 북리뷰
- 스택
- 문제풀이
- coding
- 정석
- 그래프
- 오퍼레이팅시스템
- 컴공과
- 정석학술정보관
- 컴퓨터공학과
- Stack
- Operating System
- 브루트포스
- OS
- 알고리즘
- 개발
- bfs
- DP
- 너비우선탐색
- 구현
- Computer science
- 자료구조
- 코딩
- 백준
- 컴공
- 코테
- 오에스
Archives
- Today
- Total
Little Jay
[C++] 백준 18352 - 특정 거리의 도시 찾기 본문
다익스트라를 활용한 간단한 문제.
다익스트라를 사용하면서 거리가 k가 될때 정답 벡터에 넣어주면 된다.
#include <bits/stdc++.h>
#define endl '\n'
#define INF 987654321
#define pii pair<int, int>
using namespace std;
int n, m, k, x;
vector<pii> v[300001];
int dist[300001];
vector<int> ans;
void dijikstra(int start) {
for (int i = 0; i <= n; i++) {
dist[i] = INF;
}
dist[start] = 0;
priority_queue<pii, vector<pii>, greater<>> pq;
pq.push({ 0, start });
while (!pq.empty()) {
int current = pq.top().second;
int current_dist = pq.top().first;
pq.pop();
for (int i = 0; i < v[current].size(); i++) {
int next = v[current][i].second;
int next_dist = v[current][i].first;
if (dist[next] > current_dist + next_dist) {
dist[next] = current_dist + next_dist;
pq.push({ dist[next], next });
if (dist[next] == k) ans.push_back(next);
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m >> k >> x;
for (auto i = 0; i < m; i++) {
int a, b; cin >> a >> b;
v[a].push_back({ 1, b });
}
dijikstra(x);
sort(ans.begin(), ans.end());
if (ans.size() == 0) {
cout << -1 << endl;
}
else {
for (auto& i : ans) {
cout << i << endl;
}
}
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 20055번 - 컨베이어 벨트 위의 로봇 (0) | 2022.07.18 |
---|---|
[C++] 백준 1504 - 특정한 최단 경로 (0) | 2022.07.16 |
[C++] 백준 1717번 - 집합의 표현 (0) | 2022.07.11 |
[C++] 백준 24524번 - 아름다운 문자열 (0) | 2022.07.10 |
[C++] 백준 3187번 - 양치기 꿍 (0) | 2022.07.09 |
Comments