Submission #753626

#TimeUsernameProblemLanguageResultExecution timeMemory
753626hafoToll (BOI17_toll)C++14
100 / 100
126 ms157800 KiB
#include <bits/stdc++.h> #define ll long long #define ull unsigned ll #define pb push_back #define pa pair<int, int> #define pall pair<ll, int> #define fi first #define se second #define TASK "test" #define Size(x) (int) x.size() #define all(x) x.begin(), x.end() using namespace std; template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;} template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;} const int MOD = 1e9 + 7; const int LOG = 16; const int maxn = 5e4 + 7; const ll oo = 1e17 + 69; int k, n, q, m, u, v, w; struct matrix{ ll a[5][5]; void init() { for(int i = 0; i < k; i++) for(int j = 0; j < k; j++) a[i][j] = oo; } friend matrix operator * (matrix a, matrix b) { matrix c; c.init(); for(int i = 0; i < k; i++) { for(int j = 0; j < k; j++) { for(int h = 0; h < k; h++) { mini(c.a[i][j], a.a[i][h] + b.a[h][j]); } } } return c; } }; matrix st[LOG][maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //freopen(TASK".inp", "r", stdin); //freopen(TASK".out", "w", stdout); cin>>k>>n>>m>>q; for(int i = 0; i < LOG; i++) for(int j = 0; j < n / k; j++) st[i][j].init(); while(m--) { cin>>u>>v>>w; st[0][u / k].a[u % k][v % k] = w; } for(int i = 1; i < LOG; i++) { for(int u = 0; u + (1 << i) - 1 <= (n - 1) / k; u++) { st[i][u] = st[i - 1][u] * st[i - 1][u + (1 << (i - 1))]; } } while(q--) { cin>>u>>v; if(u / k >= v / k) { cout<<-1<<"\n"; continue; } matrix ans; for(int i = 0; i < k; i++) for(int j = 0; j < k; j++) ans.a[i][j] = (i == j ? 0:oo); int len = v / k - u / k; int s = u / k; for(int i = 0; i < LOG; i++) if((len >> i) & 1) { matrix cur; cur.init(); cur = ans * st[i][s]; ans = cur; s += (1 << i); } if(ans.a[u % k][v % k] == oo) cout<<-1; else cout<<ans.a[u % k][v % k]; cout<<"\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...