#include <iostream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <numeric>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define ull unsigned long long
#define f first
#define s second
#define PF push_front
#define PB push_back
#define MP make_pair
#define max(a, b) ((a > b)? a : b)
#define min(a, b) ((a < b)? a : b)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
const int N = 5e4 + 5;
const int M = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll int INF = 1e18;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
// ifstream cin();
// ofstream cout();
int k, n, m, q;
cin >> k >> n >> m >> q;
ll par[N][5][16];
for(int i = 0; i < n; i++){
for(int j = 0; j < k; j++){
for(int step = 0; step < 16; step++){
par[i][j][step] = INF;
}
}
}
for(int i = 0; i < m; i++){
int a, b, w;
cin >> a >> b >> w;
par[a][b%k][0] = w;
}
for(int i = 1; i < 16; i++){
for(int j = 0; j < n; j++){
for(int pos = 0; pos < k; pos++){
for(int temp = 0; temp < k; temp++){
int jump = j + (1 << (i-1))*k - j%k + temp;
if(jump >= n) continue;
par[j][pos][i] = min(par[j][pos][i], par[j][temp][i-1] + par[jump][pos][i-1]);
}
}
}
}
while(q--){
int a, b;
cin >> a >> b;
int pa = a/k, pb = b/k;
if(pb <= pa){
cout << "-1" << endl;
continue;
}
vector<ll> dp(k, INF);
dp[a%k] = 0;
for(int i = 15; i >= 0; i--){
if(((pb - pa) >> i) & 1){
vector<ll> cur(k, INF);
for(int j = 0; j < k; j++){
for(int from = 0; from < k; from++){
cur[j] = min(cur[j], dp[from] + par[pa*k + from][j][i]);
}
}
dp = cur;
pa += (1 << i);
}
}
cout << ((dp[b%k] == INF)? -1 : dp[b%k]) << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
32660 KB |
Output is correct |
2 |
Correct |
15 ms |
31580 KB |
Output is correct |
3 |
Correct |
14 ms |
31580 KB |
Output is correct |
4 |
Correct |
14 ms |
31528 KB |
Output is correct |
5 |
Correct |
15 ms |
31692 KB |
Output is correct |
6 |
Correct |
17 ms |
31580 KB |
Output is correct |
7 |
Correct |
16 ms |
31576 KB |
Output is correct |
8 |
Correct |
42 ms |
32696 KB |
Output is correct |
9 |
Correct |
44 ms |
32508 KB |
Output is correct |
10 |
Correct |
33 ms |
31852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
33368 KB |
Output is correct |
2 |
Correct |
14 ms |
31580 KB |
Output is correct |
3 |
Correct |
14 ms |
31576 KB |
Output is correct |
4 |
Correct |
14 ms |
31768 KB |
Output is correct |
5 |
Correct |
14 ms |
31580 KB |
Output is correct |
6 |
Correct |
14 ms |
31580 KB |
Output is correct |
7 |
Correct |
27 ms |
31836 KB |
Output is correct |
8 |
Correct |
30 ms |
31880 KB |
Output is correct |
9 |
Correct |
46 ms |
32620 KB |
Output is correct |
10 |
Correct |
82 ms |
33876 KB |
Output is correct |
11 |
Correct |
61 ms |
33364 KB |
Output is correct |
12 |
Correct |
65 ms |
32848 KB |
Output is correct |
13 |
Correct |
67 ms |
34128 KB |
Output is correct |
14 |
Correct |
44 ms |
33104 KB |
Output is correct |
15 |
Correct |
46 ms |
32848 KB |
Output is correct |
16 |
Correct |
52 ms |
32852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31580 KB |
Output is correct |
2 |
Correct |
14 ms |
31580 KB |
Output is correct |
3 |
Correct |
14 ms |
31532 KB |
Output is correct |
4 |
Correct |
14 ms |
31620 KB |
Output is correct |
5 |
Correct |
15 ms |
31580 KB |
Output is correct |
6 |
Correct |
19 ms |
31580 KB |
Output is correct |
7 |
Correct |
15 ms |
31576 KB |
Output is correct |
8 |
Correct |
16 ms |
31580 KB |
Output is correct |
9 |
Correct |
15 ms |
31580 KB |
Output is correct |
10 |
Correct |
29 ms |
32392 KB |
Output is correct |
11 |
Correct |
50 ms |
33020 KB |
Output is correct |
12 |
Correct |
61 ms |
33872 KB |
Output is correct |
13 |
Correct |
75 ms |
34128 KB |
Output is correct |
14 |
Correct |
55 ms |
33304 KB |
Output is correct |
15 |
Correct |
46 ms |
32848 KB |
Output is correct |
16 |
Correct |
45 ms |
32772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31580 KB |
Output is correct |
2 |
Correct |
14 ms |
31580 KB |
Output is correct |
3 |
Correct |
14 ms |
31532 KB |
Output is correct |
4 |
Correct |
14 ms |
31620 KB |
Output is correct |
5 |
Correct |
15 ms |
31580 KB |
Output is correct |
6 |
Correct |
19 ms |
31580 KB |
Output is correct |
7 |
Correct |
15 ms |
31576 KB |
Output is correct |
8 |
Correct |
16 ms |
31580 KB |
Output is correct |
9 |
Correct |
15 ms |
31580 KB |
Output is correct |
10 |
Correct |
29 ms |
32392 KB |
Output is correct |
11 |
Correct |
50 ms |
33020 KB |
Output is correct |
12 |
Correct |
61 ms |
33872 KB |
Output is correct |
13 |
Correct |
75 ms |
34128 KB |
Output is correct |
14 |
Correct |
55 ms |
33304 KB |
Output is correct |
15 |
Correct |
46 ms |
32848 KB |
Output is correct |
16 |
Correct |
45 ms |
32772 KB |
Output is correct |
17 |
Correct |
48 ms |
33276 KB |
Output is correct |
18 |
Correct |
14 ms |
31576 KB |
Output is correct |
19 |
Correct |
15 ms |
31576 KB |
Output is correct |
20 |
Correct |
14 ms |
31576 KB |
Output is correct |
21 |
Correct |
14 ms |
31668 KB |
Output is correct |
22 |
Correct |
14 ms |
31576 KB |
Output is correct |
23 |
Correct |
18 ms |
31616 KB |
Output is correct |
24 |
Correct |
17 ms |
31836 KB |
Output is correct |
25 |
Correct |
19 ms |
31844 KB |
Output is correct |
26 |
Correct |
19 ms |
31836 KB |
Output is correct |
27 |
Correct |
32 ms |
32592 KB |
Output is correct |
28 |
Correct |
77 ms |
33892 KB |
Output is correct |
29 |
Correct |
75 ms |
34128 KB |
Output is correct |
30 |
Correct |
70 ms |
33368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
32660 KB |
Output is correct |
2 |
Correct |
15 ms |
31580 KB |
Output is correct |
3 |
Correct |
14 ms |
31580 KB |
Output is correct |
4 |
Correct |
14 ms |
31528 KB |
Output is correct |
5 |
Correct |
15 ms |
31692 KB |
Output is correct |
6 |
Correct |
17 ms |
31580 KB |
Output is correct |
7 |
Correct |
16 ms |
31576 KB |
Output is correct |
8 |
Correct |
42 ms |
32696 KB |
Output is correct |
9 |
Correct |
44 ms |
32508 KB |
Output is correct |
10 |
Correct |
33 ms |
31852 KB |
Output is correct |
11 |
Correct |
59 ms |
33368 KB |
Output is correct |
12 |
Correct |
14 ms |
31580 KB |
Output is correct |
13 |
Correct |
14 ms |
31576 KB |
Output is correct |
14 |
Correct |
14 ms |
31768 KB |
Output is correct |
15 |
Correct |
14 ms |
31580 KB |
Output is correct |
16 |
Correct |
14 ms |
31580 KB |
Output is correct |
17 |
Correct |
27 ms |
31836 KB |
Output is correct |
18 |
Correct |
30 ms |
31880 KB |
Output is correct |
19 |
Correct |
46 ms |
32620 KB |
Output is correct |
20 |
Correct |
82 ms |
33876 KB |
Output is correct |
21 |
Correct |
61 ms |
33364 KB |
Output is correct |
22 |
Correct |
65 ms |
32848 KB |
Output is correct |
23 |
Correct |
67 ms |
34128 KB |
Output is correct |
24 |
Correct |
44 ms |
33104 KB |
Output is correct |
25 |
Correct |
46 ms |
32848 KB |
Output is correct |
26 |
Correct |
52 ms |
32852 KB |
Output is correct |
27 |
Correct |
14 ms |
31580 KB |
Output is correct |
28 |
Correct |
14 ms |
31580 KB |
Output is correct |
29 |
Correct |
14 ms |
31532 KB |
Output is correct |
30 |
Correct |
14 ms |
31620 KB |
Output is correct |
31 |
Correct |
15 ms |
31580 KB |
Output is correct |
32 |
Correct |
19 ms |
31580 KB |
Output is correct |
33 |
Correct |
15 ms |
31576 KB |
Output is correct |
34 |
Correct |
16 ms |
31580 KB |
Output is correct |
35 |
Correct |
15 ms |
31580 KB |
Output is correct |
36 |
Correct |
29 ms |
32392 KB |
Output is correct |
37 |
Correct |
50 ms |
33020 KB |
Output is correct |
38 |
Correct |
61 ms |
33872 KB |
Output is correct |
39 |
Correct |
75 ms |
34128 KB |
Output is correct |
40 |
Correct |
55 ms |
33304 KB |
Output is correct |
41 |
Correct |
46 ms |
32848 KB |
Output is correct |
42 |
Correct |
45 ms |
32772 KB |
Output is correct |
43 |
Correct |
48 ms |
33276 KB |
Output is correct |
44 |
Correct |
14 ms |
31576 KB |
Output is correct |
45 |
Correct |
15 ms |
31576 KB |
Output is correct |
46 |
Correct |
14 ms |
31576 KB |
Output is correct |
47 |
Correct |
14 ms |
31668 KB |
Output is correct |
48 |
Correct |
14 ms |
31576 KB |
Output is correct |
49 |
Correct |
18 ms |
31616 KB |
Output is correct |
50 |
Correct |
17 ms |
31836 KB |
Output is correct |
51 |
Correct |
19 ms |
31844 KB |
Output is correct |
52 |
Correct |
19 ms |
31836 KB |
Output is correct |
53 |
Correct |
32 ms |
32592 KB |
Output is correct |
54 |
Correct |
77 ms |
33892 KB |
Output is correct |
55 |
Correct |
75 ms |
34128 KB |
Output is correct |
56 |
Correct |
70 ms |
33368 KB |
Output is correct |
57 |
Correct |
99 ms |
34900 KB |
Output is correct |
58 |
Correct |
41 ms |
32600 KB |
Output is correct |
59 |
Correct |
59 ms |
33372 KB |
Output is correct |