#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 < ll > dist(n, INF);
priority_queue < pair < ll, pair < int, int > > > pq;
dist[s] = 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] != d)
continue;
for (pair < int, int > vecino : adj[u]) {
int v = vecino.first;
int w = vecino.second;
if (dist[v] > dist[u] + w) {
dist[v] = dist[u] + w;
pq.push(make_pair(-dist[v], make_pair(v, l + 1)));
}
}
}
if (dist[f] == INF) return -1;
return dist[f];
}
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 |
1 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Incorrect |
12 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |