Submission #860501

#TimeUsernameProblemLanguageResultExecution timeMemory
860501The_SamuraiToll (BOI17_toll)C++17
0 / 100
3052 ms6076 KiB
#include "bits/stdc++.h" using namespace std; using ll = long long; const int inf = 1e9; int main() { cin.tie(0)->sync_with_stdio(false); #ifdef sunnatov freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n, m, k, q; cin >> k >> n >> m >> q; vector queries(n, vector<pair<int, int>>()); vector<int> ans(q); vector<vector<pair<int, int>>> out(n); while (m--) { int u, v, t; cin >> u >> v >> t; out[u].emplace_back(v, t); } for (int i = 0; i < q; i++) { int u, v; cin >> u >> v; queries[u].emplace_back(v, i); } vector dist(k, vector(n, inf)); for (int i = n - 1; i / k == (n - 1) / k; i--) dist[i % k][i] = 0; for (int i = (n - 1) / k; i >= 0; i--) { int left = k * i, right = min(n - 1, k * (i + 1) - 1); vector new_dist(k, vector(n, inf)); for (int u = left; u <= right; u++) { new_dist[u % k][u] = 0; for (auto [v, t]: out[u]) { for (int j = u; j < n; j++) { new_dist[u % k][j] = min(new_dist[u % k][j], dist[v % k][j] + t); } } for (auto [v, j]: queries[u]) { ans[j] = new_dist[u % k][v]; } } dist = new_dist; } for (int i = 0; i < q; i++) cout << (ans[i] == inf ? -1 : ans[i]) << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...