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
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1000000000;
const int MAX_N = 50000;
const int MAX_K = 5;
int N, Q, M, K;
vector<pii> gp[MAX_N+1];
ll dp[MAX_K+1][MAX_K+1], ndp[MAX_K+1][MAX_K+1];
bool chk;
struct SEG{
struct NODE{
int l, r;
ll d[MAX_K+1][MAX_K+1];
};
NODE seg[MAX_N*2+1];
int cnt = 0;
int SZ;
void add(){
seg[cnt].l = seg[cnt].r = -1;
for(int i=0 ;i<K; i++){
for(int j=0; j<K; j++){
seg[cnt].d[i][j] = INF;
}
}
cnt++;
}
void Init(int x){
SZ = x;
add();
init(0, 1, SZ);
}
void init(int idx, int s, int e){
if(s==e){
for(int j=0; j<K; j++){
int i = (s-1)*K + j;
for(pii p : gp[i]){
seg[idx].d[j][p.first%K] = p.second;
}
}
// cout<<s<<" "<<e<<endl;
// for(int j=0; j<K; j++){
// for(int k=0; k<K; k++){
// cout<<seg[idx].d[j][k]<<" ";
// }cout<<endl;
// }
return;
}
seg[idx].l = cnt; add();
seg[idx].r = cnt; add();
init(seg[idx].l, s, (s+e)/2); init(seg[idx].r, (s+e)/2+1, e);
for(int i=0; i<K; i++){
for(int j=0; j<K; j++){
for(int k=0; k<K; k++){
seg[idx].d[i][k] = min(seg[idx].d[i][k], seg[seg[idx].l].d[i][j] + seg[seg[idx].r].d[j][k]);
}
}
}
// cout<<s<<" "<<e<<endl;
// for(int j=0; j<K; j++){
// for(int k=0; k<K; k++){
// cout<<seg[idx].d[j][k]<<" ";
// }cout<<endl;
// }
}
void Calc(int x, int y){
calc(0, 1, SZ, x, y);
}
void calc(int idx, int s, int e, int x, int y){
if(x<=s && e<=y){
if(chk){
for(int i=0; i<K; i++){
for(int j=0; j<K; j++){
dp[i][j] = seg[idx].d[i][j];
}
}
return;
}
for(int i=0; i<K; i++){
for(int j=0; j<K; j++){
ndp[i][j] = INF;
}
}
for(int i=0; i<K; i++){
for(int j=0; j<K; j++){
for(int k=0; k<K; k++){
ndp[i][k] = min(ndp[i][k], dp[i][j]+seg[idx].d[j][k]);
}
}
}
for(int i=0; i<K; i++){
for(int j=0; j<K; j++){
dp[i][j] = ndp[i][j];
}
}
return;
}
if(x>e || y<s) return;
calc(seg[idx].l, s, (s+e)/2, x, y);
calc(seg[idx].r, (s+e)/2+1, e, x, y);
}
}Seg;
int main(){
scanf("%d%d%d%d", &K, &N, &M, &Q);
for(int i=1; i<=M; i++){
int a, b, t;
scanf("%d%d%d", &a, &b, &t);
gp[a].pb({b, t});
}
Seg.Init(N/K);
for(int i=1; i<=Q; i++){
int a, b; scanf("%d%d", &a, &b);
if(a==b){
printf("0\n");
continue;
}
if(a/K>=b/K){
printf("-1\n");
continue;
}
int ga = a/K, gb = b/K;
chk = true;
Seg.Calc(ga+1, gb);
if(dp[a%K][b%K]==INF){
printf("-1\n");
}else{
printf("%lld\n", dp[a%K][b%K]);
}
}
}
Compilation message (stderr)
toll.cpp: In function 'int main()':
toll.cpp:114:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &K, &N, &M, &Q);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &a, &b, &t);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:122:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int a, b; scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# | 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... |