-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1854.cpp
54 lines (49 loc) · 1.13 KB
/
1854.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <queue>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
struct Node {
int x;
ll weight;
};
const int MAX = 1e3+1;
struct Compare {
bool operator()(const Node &a, const Node &b) {
return a.weight > b.weight;
}
};
bool visited[MAX];
void solve(){
int N, M, K; cin >> N >> M >> K;
vector<vector<pair<int,int>>> edges(N+1);
for (int i = 0; i <M;i++) {
int u,v,w; cin >> u >> v >> w;
edges[u].push_back({v,w});
}
priority_queue<Node, vector<Node>, Compare> pq;
priority_queue<ll> weights[N+1]; weights[1].push(0); pq.push({1,0});
while (pq.size()) {
auto [x, weight] = pq.top(); pq.pop();
for (auto [nx, w] : edges[x]) {
ll nw=w+weight;
if (weights[nx].size()<K) {
weights[nx].push(nw);
pq.push({nx, nw});
} else if (weights[nx].top() > nw) {
weights[nx].pop();
weights[nx].push(nw);
pq.push({nx, nw});
}
}
}
for (int i = 1; i<=N; i++) {
cout << (weights[i].size() == K ? weights[i].top() : -1) << '\n';
}
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
solve();
return 0;
}