This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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)));
}
}
}
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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |