#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];
}
}
chk = false;
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
toll.cpp: In function 'int main()':
toll.cpp:115: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:118: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:123: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);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
32120 KB |
Output is correct |
2 |
Correct |
4 ms |
1556 KB |
Output is correct |
3 |
Correct |
2 ms |
1528 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
5 ms |
2168 KB |
Output is correct |
6 |
Correct |
5 ms |
2168 KB |
Output is correct |
7 |
Correct |
5 ms |
1912 KB |
Output is correct |
8 |
Correct |
83 ms |
32092 KB |
Output is correct |
9 |
Correct |
75 ms |
32120 KB |
Output is correct |
10 |
Correct |
42 ms |
30456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
17528 KB |
Output is correct |
2 |
Correct |
6 ms |
1528 KB |
Output is correct |
3 |
Correct |
3 ms |
1528 KB |
Output is correct |
4 |
Correct |
8 ms |
1528 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
3 ms |
1532 KB |
Output is correct |
7 |
Correct |
7 ms |
2172 KB |
Output is correct |
8 |
Correct |
10 ms |
1912 KB |
Output is correct |
9 |
Correct |
69 ms |
32120 KB |
Output is correct |
10 |
Correct |
109 ms |
13656 KB |
Output is correct |
11 |
Correct |
85 ms |
17656 KB |
Output is correct |
12 |
Correct |
69 ms |
12664 KB |
Output is correct |
13 |
Correct |
101 ms |
7848 KB |
Output is correct |
14 |
Correct |
62 ms |
8952 KB |
Output is correct |
15 |
Correct |
56 ms |
6340 KB |
Output is correct |
16 |
Correct |
56 ms |
6264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1500 KB |
Output is correct |
2 |
Correct |
3 ms |
1528 KB |
Output is correct |
3 |
Correct |
3 ms |
1528 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
4 ms |
2168 KB |
Output is correct |
7 |
Correct |
5 ms |
1784 KB |
Output is correct |
8 |
Correct |
6 ms |
1656 KB |
Output is correct |
9 |
Correct |
5 ms |
1656 KB |
Output is correct |
10 |
Correct |
61 ms |
31964 KB |
Output is correct |
11 |
Correct |
73 ms |
17540 KB |
Output is correct |
12 |
Correct |
89 ms |
13564 KB |
Output is correct |
13 |
Correct |
98 ms |
13816 KB |
Output is correct |
14 |
Correct |
78 ms |
13296 KB |
Output is correct |
15 |
Correct |
48 ms |
6264 KB |
Output is correct |
16 |
Correct |
49 ms |
6328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1500 KB |
Output is correct |
2 |
Correct |
3 ms |
1528 KB |
Output is correct |
3 |
Correct |
3 ms |
1528 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
3 ms |
1528 KB |
Output is correct |
6 |
Correct |
4 ms |
2168 KB |
Output is correct |
7 |
Correct |
5 ms |
1784 KB |
Output is correct |
8 |
Correct |
6 ms |
1656 KB |
Output is correct |
9 |
Correct |
5 ms |
1656 KB |
Output is correct |
10 |
Correct |
61 ms |
31964 KB |
Output is correct |
11 |
Correct |
73 ms |
17540 KB |
Output is correct |
12 |
Correct |
89 ms |
13564 KB |
Output is correct |
13 |
Correct |
98 ms |
13816 KB |
Output is correct |
14 |
Correct |
78 ms |
13296 KB |
Output is correct |
15 |
Correct |
48 ms |
6264 KB |
Output is correct |
16 |
Correct |
49 ms |
6328 KB |
Output is correct |
17 |
Correct |
75 ms |
17588 KB |
Output is correct |
18 |
Correct |
3 ms |
1528 KB |
Output is correct |
19 |
Correct |
3 ms |
1528 KB |
Output is correct |
20 |
Correct |
4 ms |
1528 KB |
Output is correct |
21 |
Correct |
3 ms |
1528 KB |
Output is correct |
22 |
Correct |
3 ms |
1528 KB |
Output is correct |
23 |
Correct |
6 ms |
2168 KB |
Output is correct |
24 |
Correct |
6 ms |
1912 KB |
Output is correct |
25 |
Correct |
10 ms |
1660 KB |
Output is correct |
26 |
Correct |
9 ms |
1788 KB |
Output is correct |
27 |
Correct |
64 ms |
31992 KB |
Output is correct |
28 |
Correct |
97 ms |
13688 KB |
Output is correct |
29 |
Correct |
107 ms |
13816 KB |
Output is correct |
30 |
Correct |
85 ms |
13304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
32120 KB |
Output is correct |
2 |
Correct |
4 ms |
1556 KB |
Output is correct |
3 |
Correct |
2 ms |
1528 KB |
Output is correct |
4 |
Correct |
3 ms |
1528 KB |
Output is correct |
5 |
Correct |
5 ms |
2168 KB |
Output is correct |
6 |
Correct |
5 ms |
2168 KB |
Output is correct |
7 |
Correct |
5 ms |
1912 KB |
Output is correct |
8 |
Correct |
83 ms |
32092 KB |
Output is correct |
9 |
Correct |
75 ms |
32120 KB |
Output is correct |
10 |
Correct |
42 ms |
30456 KB |
Output is correct |
11 |
Correct |
82 ms |
17528 KB |
Output is correct |
12 |
Correct |
6 ms |
1528 KB |
Output is correct |
13 |
Correct |
3 ms |
1528 KB |
Output is correct |
14 |
Correct |
8 ms |
1528 KB |
Output is correct |
15 |
Correct |
3 ms |
1528 KB |
Output is correct |
16 |
Correct |
3 ms |
1532 KB |
Output is correct |
17 |
Correct |
7 ms |
2172 KB |
Output is correct |
18 |
Correct |
10 ms |
1912 KB |
Output is correct |
19 |
Correct |
69 ms |
32120 KB |
Output is correct |
20 |
Correct |
109 ms |
13656 KB |
Output is correct |
21 |
Correct |
85 ms |
17656 KB |
Output is correct |
22 |
Correct |
69 ms |
12664 KB |
Output is correct |
23 |
Correct |
101 ms |
7848 KB |
Output is correct |
24 |
Correct |
62 ms |
8952 KB |
Output is correct |
25 |
Correct |
56 ms |
6340 KB |
Output is correct |
26 |
Correct |
56 ms |
6264 KB |
Output is correct |
27 |
Correct |
1 ms |
1500 KB |
Output is correct |
28 |
Correct |
3 ms |
1528 KB |
Output is correct |
29 |
Correct |
3 ms |
1528 KB |
Output is correct |
30 |
Correct |
3 ms |
1528 KB |
Output is correct |
31 |
Correct |
3 ms |
1528 KB |
Output is correct |
32 |
Correct |
4 ms |
2168 KB |
Output is correct |
33 |
Correct |
5 ms |
1784 KB |
Output is correct |
34 |
Correct |
6 ms |
1656 KB |
Output is correct |
35 |
Correct |
5 ms |
1656 KB |
Output is correct |
36 |
Correct |
61 ms |
31964 KB |
Output is correct |
37 |
Correct |
73 ms |
17540 KB |
Output is correct |
38 |
Correct |
89 ms |
13564 KB |
Output is correct |
39 |
Correct |
98 ms |
13816 KB |
Output is correct |
40 |
Correct |
78 ms |
13296 KB |
Output is correct |
41 |
Correct |
48 ms |
6264 KB |
Output is correct |
42 |
Correct |
49 ms |
6328 KB |
Output is correct |
43 |
Correct |
75 ms |
17588 KB |
Output is correct |
44 |
Correct |
3 ms |
1528 KB |
Output is correct |
45 |
Correct |
3 ms |
1528 KB |
Output is correct |
46 |
Correct |
4 ms |
1528 KB |
Output is correct |
47 |
Correct |
3 ms |
1528 KB |
Output is correct |
48 |
Correct |
3 ms |
1528 KB |
Output is correct |
49 |
Correct |
6 ms |
2168 KB |
Output is correct |
50 |
Correct |
6 ms |
1912 KB |
Output is correct |
51 |
Correct |
10 ms |
1660 KB |
Output is correct |
52 |
Correct |
9 ms |
1788 KB |
Output is correct |
53 |
Correct |
64 ms |
31992 KB |
Output is correct |
54 |
Correct |
97 ms |
13688 KB |
Output is correct |
55 |
Correct |
107 ms |
13816 KB |
Output is correct |
56 |
Correct |
85 ms |
13304 KB |
Output is correct |
57 |
Correct |
156 ms |
11484 KB |
Output is correct |
58 |
Correct |
75 ms |
32092 KB |
Output is correct |
59 |
Correct |
91 ms |
17656 KB |
Output is correct |