Submission #979322

#TimeUsernameProblemLanguageResultExecution timeMemory
979322chautanphatToll (BOI17_toll)C++14
100 / 100
74 ms83832 KiB
#include <bits/stdc++.h> using namespace std; const int N = 5e4; int k, n, m, o, cost[N][5][5], ans[__lg(N)+1][N][5][5], mask[N]; void dnc(int l, int r, int level = 0) { if (l == r) return; int mid = (l+r)/2; for (int a = 0; a < k; a++) ans[level][mid][a][a] = 0; for (int i = mid-1; i >= l; i--) for (int a = 0; a < k; a++) for (int b = 0; b < k; b++) for (int c = 0; c < k; c++) ans[level][i][a][c] = min(ans[level][i][a][c], ans[level][i+1][b][c]+cost[i][a][b]); for (int a = 0; a < k; a++) for (int b = 0; b < k; b++) ans[level][mid+1][a][b] = cost[mid][a][b]; for (int i = mid+2; i <= r; i++) for (int a = 0; a < k; a++) for (int b = 0; b < k; b++) for (int c = 0; c < k; c++) ans[level][i][c][a] = min(ans[level][i][c][a], ans[level][i-1][c][b]+cost[i-1][b][a]); for (int i = mid+1; i <= r; i++) mask[i] |= 1<<level; dnc(l, mid, level+1); dnc(mid+1, r, level+1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> k >> n >> m >> o; for (int i = 0; i < n; i++) for (int a = 0; a < k; a++) for (int b = 0; b < k; b++) cost[i][a][b] = 1e9; for (int level = 0; (1<<level) < n; level++) for (int i = 0; i < n; i++) for (int a = 0; a < k; a++) for (int b = 0; b < k; b++) ans[level][i][a][b] = 1e9; for (int i = 0; i < m; i++) { int a, b, t; cin >> a >> b >> t; cost[a/k][a%k][b%k] = t; } dnc(0, (n-1)/k); for (int i = 0; i < o; i++) { int a, b; cin >> a >> b; if (a/k == b/k) { cout << -1 << '\n'; continue; } int level = __builtin_ctz(mask[a/k]^mask[b/k]), res = 1e9; for (int j = 0; j < k; j++) res = min(res, ans[level][a/k][a%k][j]+ans[level][b/k][j][b%k]); cout << (res == 1e9 ? -1 : res) << '\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...
#Verdict Execution timeMemoryGrader output
Fetching results...