forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlight Routes.cpp
More file actions
40 lines (34 loc) · 869 Bytes
/
Flight Routes.cpp
File metadata and controls
40 lines (34 loc) · 869 Bytes
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
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,ll> edge;
typedef pair<ll,int> node;
const int maxN = 2e5+1;
int N, M, K, cnt[maxN];
vector<edge> G[maxN];
priority_queue<node, vector<node>, greater<node>> Q;
int main(){
scanf("%d %d %d", &N, &M, &K);
for(int i = 0, a, b, c; i < M; i++){
scanf("%d %d %d", &a, &b, &c);
G[a].push_back({b, c});
}
Q.push({0, 1});
while(!Q.empty()){
ll d = get<0>(Q.top());
int u = get<1>(Q.top());
Q.pop();
cnt[u]++;
if(u == N){
printf("%lld%c", d, (" \n")[cnt[u]==K]);
if(cnt[u] == K) return 0;
}
if(cnt[u] <= K){
for(edge e : G[u]){
int v = e.first;
ll w = e.second;
Q.push({d+w, v});
}
}
}
}