일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 자료구조
- cs
- swea
- db
- D3
- algorithm
- 감상문
- Programmers
- 데이터베이스
- LeetCode
- 재테크/투자
- 알고리즘
- OS
- 운영체제
- language
- SW Expert Academy
- BOJ
- Computer Science
- 문제풀이
- Database
- 백준
- algogritim
- c++
- D2
- data structure
- 법의학
- network
- 네트워크
- 프로그래머스
- 독서
Archives
- Today
- Total
선택은 나의 것
[BOJ 백준] 1854번 K번째 최단경로 찾기 본문
문제
BOJ 1854 : https://www.acmicpc.net/problem/1854
1854번: K번째 최단경로 찾기
첫째 줄에 n, m, k가 주어진다. (1 ≤ n ≤ 1000, 0 ≤ m ≤ 2000000, 1 ≤ k ≤ 100) n과 m은 각각 김 조교가 여행을 고려하고 있는 도시들의 개수와, 도시 간에 존재하는 도로의 수이다. 이어지는 m개의 줄에
www.acmicpc.net
코드
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n, m, k, from, to, cost, Start;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > pq;
priority_queue<int> heap[1001];
vector<pair<int, int> > v[1001];
int main() {
cin >> n >> m >> k;
for (int i = 0; i < m; i++){
cin >> from >> to >> cost;
v[from].push_back(make_pair(to, cost));
}
pq.push(make_pair(0, 1));
heap[1].push(0);
while(!pq.empty()) {
int x = pq.top().second;
int num = pq.top().first;
pq.pop();
for(int i = 0; i < v[x].size(); i++) {
int next = v[x][i].first;
int cost = num+v[x][i].second;
if (heap[next].size()<k) {
heap[next].push(cost);
pq.push(make_pair(cost, next));
} else {
if (heap[next].top()>cost) {
heap[next].pop();
heap[next].push(cost);
pq.push(make_pair(cost, next));
}
}
}
}
for (int i = 1; i <= n; i++) {
// 1번 도시에서 i번 도시까지의 k번째 최단 경로
if (heap[i].size()<k)
cout << "-1" << "\n";
else
cout << heap[i].top() << "\n";
}
return 0;
}
'☽ Algorithm > BOJ' 카테고리의 다른 글
[BOJ 백준] 17089번 세 친구 (0) | 2021.11.24 |
---|---|
[BOJ 백준] 18310번 안테나 (0) | 2021.11.22 |
[BOJ 백준] 22252번 정보 상인 호석 (0) | 2021.09.06 |
[BOJ 백준] 21608번 상어 초등학교 (0) | 2021.08.18 |
[BOJ 백준] 1520번 내리막 길 (0) | 2021.07.26 |
Comments