#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll MOD = (1LL << 61) - 1;
const int nax = 1e5+5;
const int MAX_VAL = 1e6+1;
double PI=3.14159265359;
int arx[8]={1,0,0,-1,-1,-1, 1, 1};
int ary[8]={0,1,-1, 0, 1,-1,-1, 1};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int k,n,m,o;
int dp[50005][17][5][5],ans[5][5],tmp[5][5];
void combine(int target[5][5], int a[5][5], int b[5][5]) {
for (int x = 0; x < k; x++) {
for (int y = 0; y < k; y++) {
for (int z = 0; z < k; z++) {
target[x][y] = min(target[x][y], a[x][z] + b[z][y]);
}
}
}
}
int main(){
optimise;
//setIO("dec");
cin>>k>>n>>m>>o;
memset(dp,0x3f,sizeof dp);
for (int i = 0; i < m; ++i)
{
int a,b,t;
cin>>a>>b>>t;
dp[a/k][0][a%k][b%k] = t;
}
for (int j = 1; j < 17; ++j)
{
for (int i = 0; i + (1<<j) < (n+k-1)/k ; i++)
{
combine(dp[i][j],dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
}
while(o--){
int a,b;
cin>>a>>b;
memset(ans,0x3f,sizeof ans);
for (int i = 0; i < 5; ++i) ans[i][i]=0;
for (int cur = a/k, dis = b/k , i = 16 ; i >= 0; --i)
{
if(cur+(1<<i)<=dis){
memset(tmp,0x3f,sizeof tmp);
combine(tmp,ans,dp[cur][i]);
memcpy(ans,tmp,sizeof ans);
cur+=(1<<i);
}
}
cout << (ans[a%k][b%k]==0x3f3f3f3f ? -1 : ans[a%k][b%k])<<'\n';
}
return 0;
}
Compilation message
toll.cpp: In function 'void setIO(std::string)':
toll.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:37:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
84628 KB |
Output is correct |
2 |
Correct |
22 ms |
83544 KB |
Output is correct |
3 |
Correct |
21 ms |
83548 KB |
Output is correct |
4 |
Correct |
22 ms |
83548 KB |
Output is correct |
5 |
Correct |
24 ms |
83548 KB |
Output is correct |
6 |
Correct |
24 ms |
83536 KB |
Output is correct |
7 |
Correct |
24 ms |
83544 KB |
Output is correct |
8 |
Correct |
55 ms |
84516 KB |
Output is correct |
9 |
Correct |
55 ms |
84304 KB |
Output is correct |
10 |
Correct |
43 ms |
83756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
85064 KB |
Output is correct |
2 |
Correct |
27 ms |
83548 KB |
Output is correct |
3 |
Correct |
25 ms |
83440 KB |
Output is correct |
4 |
Correct |
25 ms |
83540 KB |
Output is correct |
5 |
Correct |
24 ms |
83540 KB |
Output is correct |
6 |
Correct |
24 ms |
83592 KB |
Output is correct |
7 |
Correct |
29 ms |
83536 KB |
Output is correct |
8 |
Correct |
31 ms |
83776 KB |
Output is correct |
9 |
Correct |
58 ms |
84500 KB |
Output is correct |
10 |
Correct |
78 ms |
85920 KB |
Output is correct |
11 |
Correct |
66 ms |
85212 KB |
Output is correct |
12 |
Correct |
61 ms |
84820 KB |
Output is correct |
13 |
Correct |
73 ms |
86100 KB |
Output is correct |
14 |
Correct |
50 ms |
85012 KB |
Output is correct |
15 |
Correct |
54 ms |
84820 KB |
Output is correct |
16 |
Correct |
51 ms |
84828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
83800 KB |
Output is correct |
2 |
Correct |
25 ms |
83532 KB |
Output is correct |
3 |
Correct |
24 ms |
83556 KB |
Output is correct |
4 |
Correct |
25 ms |
83548 KB |
Output is correct |
5 |
Correct |
29 ms |
83508 KB |
Output is correct |
6 |
Correct |
25 ms |
83548 KB |
Output is correct |
7 |
Correct |
26 ms |
83668 KB |
Output is correct |
8 |
Correct |
25 ms |
83540 KB |
Output is correct |
9 |
Correct |
25 ms |
83660 KB |
Output is correct |
10 |
Correct |
52 ms |
84200 KB |
Output is correct |
11 |
Correct |
62 ms |
85148 KB |
Output is correct |
12 |
Correct |
65 ms |
85584 KB |
Output is correct |
13 |
Correct |
69 ms |
85904 KB |
Output is correct |
14 |
Correct |
66 ms |
85328 KB |
Output is correct |
15 |
Correct |
47 ms |
84560 KB |
Output is correct |
16 |
Correct |
47 ms |
84568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
83800 KB |
Output is correct |
2 |
Correct |
25 ms |
83532 KB |
Output is correct |
3 |
Correct |
24 ms |
83556 KB |
Output is correct |
4 |
Correct |
25 ms |
83548 KB |
Output is correct |
5 |
Correct |
29 ms |
83508 KB |
Output is correct |
6 |
Correct |
25 ms |
83548 KB |
Output is correct |
7 |
Correct |
26 ms |
83668 KB |
Output is correct |
8 |
Correct |
25 ms |
83540 KB |
Output is correct |
9 |
Correct |
25 ms |
83660 KB |
Output is correct |
10 |
Correct |
52 ms |
84200 KB |
Output is correct |
11 |
Correct |
62 ms |
85148 KB |
Output is correct |
12 |
Correct |
65 ms |
85584 KB |
Output is correct |
13 |
Correct |
69 ms |
85904 KB |
Output is correct |
14 |
Correct |
66 ms |
85328 KB |
Output is correct |
15 |
Correct |
47 ms |
84560 KB |
Output is correct |
16 |
Correct |
47 ms |
84568 KB |
Output is correct |
17 |
Correct |
62 ms |
85136 KB |
Output is correct |
18 |
Correct |
25 ms |
83408 KB |
Output is correct |
19 |
Correct |
25 ms |
83464 KB |
Output is correct |
20 |
Correct |
24 ms |
83548 KB |
Output is correct |
21 |
Correct |
24 ms |
83548 KB |
Output is correct |
22 |
Correct |
24 ms |
83476 KB |
Output is correct |
23 |
Correct |
25 ms |
83664 KB |
Output is correct |
24 |
Correct |
25 ms |
83672 KB |
Output is correct |
25 |
Correct |
28 ms |
83792 KB |
Output is correct |
26 |
Correct |
30 ms |
83556 KB |
Output is correct |
27 |
Correct |
56 ms |
84460 KB |
Output is correct |
28 |
Correct |
69 ms |
85824 KB |
Output is correct |
29 |
Correct |
69 ms |
85840 KB |
Output is correct |
30 |
Correct |
63 ms |
85464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
84628 KB |
Output is correct |
2 |
Correct |
22 ms |
83544 KB |
Output is correct |
3 |
Correct |
21 ms |
83548 KB |
Output is correct |
4 |
Correct |
22 ms |
83548 KB |
Output is correct |
5 |
Correct |
24 ms |
83548 KB |
Output is correct |
6 |
Correct |
24 ms |
83536 KB |
Output is correct |
7 |
Correct |
24 ms |
83544 KB |
Output is correct |
8 |
Correct |
55 ms |
84516 KB |
Output is correct |
9 |
Correct |
55 ms |
84304 KB |
Output is correct |
10 |
Correct |
43 ms |
83756 KB |
Output is correct |
11 |
Correct |
83 ms |
85064 KB |
Output is correct |
12 |
Correct |
27 ms |
83548 KB |
Output is correct |
13 |
Correct |
25 ms |
83440 KB |
Output is correct |
14 |
Correct |
25 ms |
83540 KB |
Output is correct |
15 |
Correct |
24 ms |
83540 KB |
Output is correct |
16 |
Correct |
24 ms |
83592 KB |
Output is correct |
17 |
Correct |
29 ms |
83536 KB |
Output is correct |
18 |
Correct |
31 ms |
83776 KB |
Output is correct |
19 |
Correct |
58 ms |
84500 KB |
Output is correct |
20 |
Correct |
78 ms |
85920 KB |
Output is correct |
21 |
Correct |
66 ms |
85212 KB |
Output is correct |
22 |
Correct |
61 ms |
84820 KB |
Output is correct |
23 |
Correct |
73 ms |
86100 KB |
Output is correct |
24 |
Correct |
50 ms |
85012 KB |
Output is correct |
25 |
Correct |
54 ms |
84820 KB |
Output is correct |
26 |
Correct |
51 ms |
84828 KB |
Output is correct |
27 |
Correct |
24 ms |
83800 KB |
Output is correct |
28 |
Correct |
25 ms |
83532 KB |
Output is correct |
29 |
Correct |
24 ms |
83556 KB |
Output is correct |
30 |
Correct |
25 ms |
83548 KB |
Output is correct |
31 |
Correct |
29 ms |
83508 KB |
Output is correct |
32 |
Correct |
25 ms |
83548 KB |
Output is correct |
33 |
Correct |
26 ms |
83668 KB |
Output is correct |
34 |
Correct |
25 ms |
83540 KB |
Output is correct |
35 |
Correct |
25 ms |
83660 KB |
Output is correct |
36 |
Correct |
52 ms |
84200 KB |
Output is correct |
37 |
Correct |
62 ms |
85148 KB |
Output is correct |
38 |
Correct |
65 ms |
85584 KB |
Output is correct |
39 |
Correct |
69 ms |
85904 KB |
Output is correct |
40 |
Correct |
66 ms |
85328 KB |
Output is correct |
41 |
Correct |
47 ms |
84560 KB |
Output is correct |
42 |
Correct |
47 ms |
84568 KB |
Output is correct |
43 |
Correct |
62 ms |
85136 KB |
Output is correct |
44 |
Correct |
25 ms |
83408 KB |
Output is correct |
45 |
Correct |
25 ms |
83464 KB |
Output is correct |
46 |
Correct |
24 ms |
83548 KB |
Output is correct |
47 |
Correct |
24 ms |
83548 KB |
Output is correct |
48 |
Correct |
24 ms |
83476 KB |
Output is correct |
49 |
Correct |
25 ms |
83664 KB |
Output is correct |
50 |
Correct |
25 ms |
83672 KB |
Output is correct |
51 |
Correct |
28 ms |
83792 KB |
Output is correct |
52 |
Correct |
30 ms |
83556 KB |
Output is correct |
53 |
Correct |
56 ms |
84460 KB |
Output is correct |
54 |
Correct |
69 ms |
85824 KB |
Output is correct |
55 |
Correct |
69 ms |
85840 KB |
Output is correct |
56 |
Correct |
63 ms |
85464 KB |
Output is correct |
57 |
Correct |
111 ms |
86864 KB |
Output is correct |
58 |
Correct |
56 ms |
84536 KB |
Output is correct |
59 |
Correct |
69 ms |
85328 KB |
Output is correct |