Skip to content

0x1D강 (다익스트라) : boj 1854.cpp 문제 풀이 추가 #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions 0x1D/solutions/1854.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : -
// http://boj.kr/****************
// Co-authored by : Baejinho4028
// https://www.acmicpc.net/problem/1854
#include <bits/stdc++.h>
using namespace std;

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

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

int n, m, k;
cin >> n >> m >> k;

vector<pair<int,int>> adj[1002];
while (m--) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({w, v});
}

priority_queue<int> d[1002];
d[1].push(0);

priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
pq.push({d[1].top(), 1});

while (!pq.empty()) {
auto [cw, cv] = pq.top(); pq.pop();

for (auto [nw, nv] : adj[cv]) {
if (d[nv].size() < k) {
d[nv].push(cw + nw);
pq.push({cw + nw, nv});
}
else if (d[nv].top() > cw + nw) {
d[nv].pop();
d[nv].push(cw + nw);
pq.push({cw + nw, nv});
}
}
}

for (int i = 1; i <= n; ++i) {
cout << (d[i].size() < k ? -1 : d[i].top()) << '\n';
}
}

/**
* 각 도시마다 걸리는 거리를 내림차순 힙으로 관리합니다.
* 해당 도시까지의 상위 k개의 거리까지만을 저장하여, d[i].top()으로 k번째 최단거리를 추출함으로써 문제를 해결합니다.
* 만약 거리의 개수가 k개 이하라면 -1을 출력합니다.
*/
2 changes: 1 addition & 1 deletion workbook/0x1D.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
| 응용 문제✔ | 20183 | [골목 대장 호석 - 효율성 2](https://www.acmicpc.net/problem/20183) | [정답 코드](../0x1D/solutions/20183.cpp) |
| 응용 문제 | 24042 | [횡단보도](https://www.acmicpc.net/problem/24042) | [정답 코드](../0x1D/solutions/24042.cpp) |
| 응용 문제 | 1162 | [도로포장](https://www.acmicpc.net/problem/1162) | [정답 코드](../0x1D/solutions/1162.cpp) |
| 응용 문제 | 1854 | [K번째 최단경로 찾기](https://www.acmicpc.net/problem/1854) | - |
| 응용 문제 | 1854 | [K번째 최단경로 찾기](https://www.acmicpc.net/problem/1854) | [정답 코드](../0x1D/solutions/1854.cpp) |
| 응용 문제 | 5719 | [거의 최단 경로](https://www.acmicpc.net/problem/5719) | [정답 코드](../0x1D/solutions/5719.cpp) |
| 응용 문제 | 13907 | [세금](https://www.acmicpc.net/problem/13907) | [정답 코드](../0x1D/solutions/13907.cpp) |
| 응용 문제 | 22870 | [산책 (large)](https://www.acmicpc.net/problem/22870) | - |