This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
const ll inf = 1e17;
int k;
struct node{
vector<vector<ll>> A, B;
node(){
A.assign(k, vector<ll>(k, 0));
B = vector<vector<ll>>(k, vector<ll>(k, inf));
for(int i = 0; i < k; i++) B[i][i] = 0;
}
node(vector<vector<ll>> _A){
A = _A;
B.assign(k, vector<ll>(k, inf));
for(int i = 0; i < k; i++) B[i][i] = 0;
}
};
node operator+(node a, node b){
node rt;
rt.A = a.A, rt.B = vector<vector<ll>>(k, vector<ll>(k, inf));
for(int i = 0; i < k; i++) for(int j = 0; j < k; j++)
for(int l = 0; l < k; l++) for(int m = 0; m < k; m++)
rt.B[i][j] = min(rt.B[i][j], a.B[i][l]+b.B[m][j]+b.A[l][m]);
return rt;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m, o; cin >> k >> n >> m >> o;
vector<vector<vector<ll>>> div((n+k-1)/k, vector<vector<ll>>(k, vector<ll>(k, inf)));
for(int i = 0; i < m; i++){
int a, b; ll w; cin >> a >> b >> w;
div[b/k][a%k][b%k] = w;
}
while(o--){
int a, b; cin >> a >> b;
auto nd = node(div[a/k]);
for(int i = a/k+1; i <= b/k; i++) nd = nd+node(div[i]);
ll ans = nd.B[a%k][b%k];
if(ans >= inf) ans = -1;
cout << ans << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |