Submission #547391

#TimeUsernameProblemLanguageResultExecution timeMemory
547391JomnoiToll (BOI17_toll)C++17
100 / 100
106 ms99056 KiB
#include <bits/stdc++.h> #define DEBUG 0 using namespace std; const int MAX_N = 5e4 + 10; const int MAX_B = 20; const int MAX_K = 5; const int INF = 1e9 + 7; int N, K; int dist[MAX_N][MAX_B][MAX_K][MAX_K]; int now[MAX_K][MAX_K], prv[MAX_K][MAX_K]; void init() { for(int i = 0; i <= (N - 1) / K; i++) { for(int j = 0; j < MAX_B; j++) { for(int k = 0; k < MAX_K; k++) { for(int l = 0; l < MAX_K; l++) { dist[i][j][k][l] = INF; } } } } } void FloydWarshall(int c[MAX_K][MAX_K], int a[MAX_K][MAX_K], int b[MAX_K][MAX_K]) { for(int k = 0; k < K; k++) { for(int i = 0; i < K; i++) { for(int j = 0; j < K; j++) { c[i][j] = min(c[i][j], a[i][k] + b[k][j]); } } } } int main() { cin.tie(0)->sync_with_stdio(0); int M, O; cin >> K >> N >> M >> O; init(); while(M--) { int a, b, t; cin >> a >> b >> t; dist[a / K][0][a % K][b % K] = min(dist[a / K][0][a % K][b % K], t); } for(int j = 1; (1<<j) <= (N - 1) / K; j++) { for(int i = 0; i + (1<<j) - 1 <= (N - 1) / K; i++) { FloydWarshall(dist[i][j], dist[i][j - 1], dist[i + (1<<(j - 1))][j - 1]); } } while(O--) { int a, b; cin >> a >> b; if(a / K == b / K) { cout << "-1\n"; continue; } for(int i = 0; i < K; i++) { for(int j = 0; j < K; j++) { prv[i][j] = INF; if(i == j) { prv[i][j] = 0; } } } int s = a / K, t = b / K; for(int j = MAX_B - 1; j >= 0; j--) { if(s + (1<<j) > t) { continue; } for(int k = 0; k < K; k++) { for(int l = 0; l < K; l++) { now[k][l] = prv[k][l]; prv[k][l] = INF; } } FloydWarshall(prv, now, dist[s][j]); s += (1<<j); } if(prv[a % K][b % K] == INF) { cout << "-1\n"; } else { cout << prv[a % K][b % K] << '\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...