Submission #1053559

#TimeUsernameProblemLanguageResultExecution timeMemory
1053559ArthuroWichToll (BOI17_toll)C++17
0 / 100
212 ms129876 KiB
#include <bits/stdc++.h> using namespace std; #define int long long int vector<vector<int>> calc(vector<vector<int>> a, vector<vector<int>> b, int k) { vector<vector<int>> ans(k, vector<int>(k, INT_MAX)); for (int x = 0; x < k; x++) { for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { ans[i][j] = min(ans[i][j], a[i][x]+b[x][j]); } } } return ans; } void solve() { // I dont really like this problem int n, o, m, k; cin >> k >> n >> m >> o; vector<vector<vector<vector<int>>>> dp(n+1, vector<vector<vector<int>>>(17, vector<vector<int>>(k, vector<int>(k, INT_MAX)))); for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; dp[u/k][0][u%k][v%k] = min(dp[u/k][0][u%k][v%k], w); } for (int j = 1; j < 17; j++) { for (int i = 0; i < (n+k-1)/k; i++) { if (i+(1<<j) >= (n+k-1)/k) { break; } dp[i][j] = calc(dp[i][j-1], dp[i+(1<<j)-1][j-1], k); } } while(o--) { int a, b; cin >> a >> b; vector<vector<int>> ans(k, vector<int>(k, INT_MAX)); for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { ans[i][i] = 0; } } int i = a/k; for (int j = 16; j >= 0; j--) { if (i+(1<<j) <= b/k) { ans = calc(ans, dp[i][j], k); i += (1 << j); } } if (ans[a%k][b%k] == INT_MAX) { cout << -1 << endl; } else { cout << ans[a%k][b%k] << endl; } } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int t; t = 1; while(t--) { solve(); } }
#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...