Submission #526698

#TimeUsernameProblemLanguageResultExecution timeMemory
526698siewjhAutobus (COCI22_autobus)C++17
30 / 70
1096 ms18464 KiB
#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<pair<int, int>>> adjlist(edges); for (int i = 0; i < edges; i++) { int a, b, t; cin >> a >> b >> t; adjlist[a].push_back({ b,t }); } 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...