#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll INF = 1e18;
int n, m;
vector < vector < pair < int, int > > > adj;
ll dijkstra(int s, int f, int k) {
vector < pair < ll, int > > dist(n, {INF, 0});
priority_queue < pair < ll, pair < int, int > > > pq;
dist[s] = make_pair(0, 0);
pq.push(make_pair(0, make_pair(s, 0)));
while (!pq.empty()) {
ll d = -pq.top().first;
int u = pq.top().second.first;
int l = pq.top().second.second;
pq.pop();
if (l == k) continue;
if (dist[u].first != d && l >= dist[u].second) continue;
for (pair < int, int > vecino : adj[u]) {
int v = vecino.first;
int w = vecino.second;
if (dist[v].first > d + w) {
dist[v] = make_pair(d + w, l + 1);
pq.push(make_pair(-dist[v].first, make_pair(v, l + 1)));
} else if (dist[v].second > l + 1) {
pq.push(make_pair(-(d + w), make_pair(v, l + 1)));
}
}
}
if (dist[f].first == INF) return -1;
return dist[f].first;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
adj = vector < vector < pair < int, int > > >(n);
while (m--) {
int a, b, t;
cin >> a >> b >> t;
--a; --b;
adj[a].push_back(make_pair(b, t));
}
int k, q;
cin >> k >> q;
while (q--) {
int a, b;
cin >> a >> b;
cout << dijkstra(a - 1, b - 1, k) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
424 KB |
Output is correct |
2 |
Correct |
14 ms |
336 KB |
Output is correct |
3 |
Correct |
42 ms |
384 KB |
Output is correct |
4 |
Correct |
45 ms |
372 KB |
Output is correct |
5 |
Correct |
138 ms |
376 KB |
Output is correct |
6 |
Correct |
116 ms |
504 KB |
Output is correct |
7 |
Execution timed out |
1089 ms |
580 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Execution timed out |
1063 ms |
844 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
4 ms |
424 KB |
Output is correct |
8 |
Correct |
14 ms |
336 KB |
Output is correct |
9 |
Correct |
42 ms |
384 KB |
Output is correct |
10 |
Correct |
45 ms |
372 KB |
Output is correct |
11 |
Correct |
138 ms |
376 KB |
Output is correct |
12 |
Correct |
116 ms |
504 KB |
Output is correct |
13 |
Execution timed out |
1089 ms |
580 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |