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 <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int inf = 1'000'000'007;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int nodes, edges; cin >> nodes >> edges;
vector<vector<int>> adjmat(nodes + 1, vector<int>(nodes + 1, inf));
vector<vector<pair<int, int>>> adjlist(nodes + 1);
for (int i = 0; i < edges; i++) {
int a, b, t; cin >> a >> b >> t;
adjmat[a][b] = min(adjmat[a][b], t);
}
for (int i = 1; i <= nodes; i++)
for (int j = 1; j <= nodes; j++)
if (adjmat[i][j] != inf)
adjlist[i].push_back({ j,adjmat[i][j] });
int maxbus, queries; cin >> maxbus >> queries;
maxbus = min(maxbus, nodes - 1);
int dist[71][71][71];
for (int i = 0; i < 71; i++)
for (int j = 0; j < 71; j++)
for (int k = 0; k < 71; k++)
dist[i][j][k] = inf;
for (int i = 1; i <= nodes; i++) {
dist[i][i][0] = 0;
priority_queue<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>, greater<pair<pair<int, int>, int>>> pq;
pq.push({ {0,i}, 0 });
while (!pq.empty()) {
int d = pq.top().first.first, x = pq.top().first.second, bus = pq.top().second;
pq.pop();
if (d != dist[i][x][bus] || bus == maxbus) continue;
for (auto nxt : adjlist[x]) {
int nn = nxt.first, nd = nxt.second;
if (d + nd < dist[i][nn][bus + 1]) {
dist[i][nn][bus + 1] = d + nd;
pq.push({ {dist[i][nn][bus + 1], nn}, bus + 1 });
}
}
}
}
for (int i = 0; i < queries; i++) {
int a, b; cin >> a >> b;
int ans = inf;
for (int j = 0; j <= maxbus; j++) ans = min(ans, dist[a][b][j]);
cout << ((ans == inf) ? -1 : ans) << '\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... |