티스토리 뷰
1854번: K번째 최단경로 찾기
첫째 줄에 n, m, k가 주어진다. (1 ≤ n ≤ 1000, 0 ≤ m ≤ 2000000, 1 ≤ k ≤ 100) n과 m은 각각 김 조교가 여행을 고려하고 있는 도시들의 개수와, 도시 간에 존재하는 도로의 수이다. 이어지는 m개의 줄에
www.acmicpc.net
백준 소스코드 [C++] 1854 K번째 최단경로 찾기
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n, m,k;
vector<pii> path[1000];
priority_queue<int> dist[1000];
void dijkstra() {
priority_queue<pii> pq;
dist[0].push(0);
pq.push(make_pair( 0,0 ));
while (pq.empty() != true) {
int cost = -pq.top().first;
int here = pq.top().second;
pq.pop();
for (int i = 0;i < (int)path[here].size();i++) {
int there = path[here][i].first;
int nextdist = cost + path[here][i].second;
if (dist[there].size() < k) {
dist[there].push(nextdist);
pq.push(make_pair(-nextdist, there ));
}
else if (dist[there].top() > nextdist) {
dist[there].pop();
dist[there].push(nextdist);
pq.push(make_pair (-nextdist,there));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m>>k;
int u, v, w;
for (int i = 0;i < m;i++) {
cin >> u >> v >> w;
path[u - 1].push_back(make_pair( v - 1,w));
}
dijkstra();
for (int i = 0;i < n;i++)
if (dist[i].size() == k) cout << dist[i].top() << '\n';
else cout << "-1\n";
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 3190 뱀 (0) | 2021.02.03 |
---|---|
백준 소스코드 [C++] 15686 치킨 배달 (0) | 2021.02.03 |
백준 소스코드 [C++] 14500 테트로미노 (0) | 2021.02.02 |
백준 소스코드 [C++] 14503 로봇 청소기 (0) | 2021.02.01 |
백준 소스코드 [C++] 1389 케빈 베이컨의 6단계 법칙 (0) | 2021.02.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- rxswift
- 네트워크 유량
- 컴퓨터 추상화
- 포드 풀커슨 알고리즘
- 벨만포드 알고리즘
- 다익스트라 시간복잡도
- CPU와 Memory
- 네트워크 플로우
- IOS
- 최단경로 문제
- WWDC21
- 최단경로 알고리즘
- State Restoration
- 강한 순환 참조
- WWDC19
- test coverage
- 벨만포드 시간복잡도
- Testable
- HIG
- MeTal
- 에드몬드 카프 알고리즘
- 부스트캠프 6기
- WWDC17
- 최단경로문제
- CompositionalLayout
- 코딩대회
- 최대 매칭
- mach-o
- observeOn
- WWDC16
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
글 보관함