#include<bits/stdc++.h>
#define fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout<<"\n";
#define int long long
#define inf (int)1e18
#define pii pair<int,int>
#define vii vector<pii>
#define lb(x) x&-x
#define bit(i,j) ((i>>j)&1)
#define offbit(i,j) (i^(1<<j))
#define onbit(i,j) (i|(1<<j))
#define vi vector<int>
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
int f[50010][18][5][5];
int ans[5][5],now[5][5];
int n,k,m,q;
void combine(int ans[5][5],int a[5][5],int b[5][5])
{
fo(i,0,4) fo(j,0,4) fo(k,0,4) ans[i][j] = min(ans[i][j],a[i][k] + b[k][j]);
}
main()
{
#define name "TASK"
if(fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> k >> n >> m >> q;
fo(i,0,n / k) fo(j,0,17) fo(x,0,4) fo(y,0,4) f[i][j][x][y] = inf;
fo(i,1,m)
{
int u,v,w;
cin >> u >> v >> w;
f[u / k][0][u % k][v % k] = w;
}
fo(j,1,17) fo(i,0,(n + k - 1) / k - (1 << j))
combine(f[i][j],f[i][j - 1],f[i + (1 << (j - 1))][j - 1]);
while(q--)
{
fo(i,0,4) fo(j,0,4) ans[i][j] = inf;
fo(i,0,4) ans[i][i] = 0;
int u,v;
cin >> u >> v;
int U = u / k,V = v / k;
int del = V - U;
fod(i,17,0) if(bit(del,i))
{
fo(j,0,4) fo(k,0,4) now[j][k] = inf;
combine(now,ans,f[U][i]);
memcpy(ans,now,sizeof ans);
U = U + (1 << i);
}
cout << (ans[u % k][v % k] == inf ? -1 : ans[u % k][v % k]);en;
}
}
Compilation message
toll.cpp:31:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
31 | main()
| ^~~~
toll.cpp: In function 'int main()':
toll.cpp:36:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
toll.cpp:37:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
176564 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
4700 KB |
Output is correct |
6 |
Correct |
4 ms |
4864 KB |
Output is correct |
7 |
Correct |
4 ms |
4700 KB |
Output is correct |
8 |
Correct |
275 ms |
176564 KB |
Output is correct |
9 |
Correct |
268 ms |
176560 KB |
Output is correct |
10 |
Correct |
259 ms |
176348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
88508 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
10 ms |
4700 KB |
Output is correct |
8 |
Correct |
9 ms |
2652 KB |
Output is correct |
9 |
Correct |
250 ms |
177232 KB |
Output is correct |
10 |
Correct |
99 ms |
62376 KB |
Output is correct |
11 |
Correct |
141 ms |
90296 KB |
Output is correct |
12 |
Correct |
96 ms |
61420 KB |
Output is correct |
13 |
Correct |
45 ms |
25580 KB |
Output is correct |
14 |
Correct |
43 ms |
38996 KB |
Output is correct |
15 |
Correct |
35 ms |
24428 KB |
Output is correct |
16 |
Correct |
34 ms |
24348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2648 KB |
Output is correct |
9 |
Correct |
3 ms |
2528 KB |
Output is correct |
10 |
Correct |
254 ms |
177376 KB |
Output is correct |
11 |
Correct |
125 ms |
90384 KB |
Output is correct |
12 |
Correct |
82 ms |
62300 KB |
Output is correct |
13 |
Correct |
83 ms |
62540 KB |
Output is correct |
14 |
Correct |
76 ms |
61888 KB |
Output is correct |
15 |
Correct |
27 ms |
24156 KB |
Output is correct |
16 |
Correct |
28 ms |
24348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
4700 KB |
Output is correct |
7 |
Correct |
2 ms |
2652 KB |
Output is correct |
8 |
Correct |
2 ms |
2648 KB |
Output is correct |
9 |
Correct |
3 ms |
2528 KB |
Output is correct |
10 |
Correct |
254 ms |
177376 KB |
Output is correct |
11 |
Correct |
125 ms |
90384 KB |
Output is correct |
12 |
Correct |
82 ms |
62300 KB |
Output is correct |
13 |
Correct |
83 ms |
62540 KB |
Output is correct |
14 |
Correct |
76 ms |
61888 KB |
Output is correct |
15 |
Correct |
27 ms |
24156 KB |
Output is correct |
16 |
Correct |
28 ms |
24348 KB |
Output is correct |
17 |
Correct |
140 ms |
90292 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 KB |
Output is correct |
23 |
Correct |
7 ms |
4700 KB |
Output is correct |
24 |
Correct |
4 ms |
2652 KB |
Output is correct |
25 |
Correct |
5 ms |
2652 KB |
Output is correct |
26 |
Correct |
3 ms |
2652 KB |
Output is correct |
27 |
Correct |
249 ms |
177404 KB |
Output is correct |
28 |
Correct |
94 ms |
62352 KB |
Output is correct |
29 |
Correct |
104 ms |
62544 KB |
Output is correct |
30 |
Correct |
86 ms |
61776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
176564 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
4 ms |
4700 KB |
Output is correct |
6 |
Correct |
4 ms |
4864 KB |
Output is correct |
7 |
Correct |
4 ms |
4700 KB |
Output is correct |
8 |
Correct |
275 ms |
176564 KB |
Output is correct |
9 |
Correct |
268 ms |
176560 KB |
Output is correct |
10 |
Correct |
259 ms |
176348 KB |
Output is correct |
11 |
Correct |
135 ms |
88508 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
10 ms |
4700 KB |
Output is correct |
18 |
Correct |
9 ms |
2652 KB |
Output is correct |
19 |
Correct |
250 ms |
177232 KB |
Output is correct |
20 |
Correct |
99 ms |
62376 KB |
Output is correct |
21 |
Correct |
141 ms |
90296 KB |
Output is correct |
22 |
Correct |
96 ms |
61420 KB |
Output is correct |
23 |
Correct |
45 ms |
25580 KB |
Output is correct |
24 |
Correct |
43 ms |
38996 KB |
Output is correct |
25 |
Correct |
35 ms |
24428 KB |
Output is correct |
26 |
Correct |
34 ms |
24348 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
4700 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2648 KB |
Output is correct |
35 |
Correct |
3 ms |
2528 KB |
Output is correct |
36 |
Correct |
254 ms |
177376 KB |
Output is correct |
37 |
Correct |
125 ms |
90384 KB |
Output is correct |
38 |
Correct |
82 ms |
62300 KB |
Output is correct |
39 |
Correct |
83 ms |
62540 KB |
Output is correct |
40 |
Correct |
76 ms |
61888 KB |
Output is correct |
41 |
Correct |
27 ms |
24156 KB |
Output is correct |
42 |
Correct |
28 ms |
24348 KB |
Output is correct |
43 |
Correct |
140 ms |
90292 KB |
Output is correct |
44 |
Correct |
1 ms |
344 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
344 KB |
Output is correct |
49 |
Correct |
7 ms |
4700 KB |
Output is correct |
50 |
Correct |
4 ms |
2652 KB |
Output is correct |
51 |
Correct |
5 ms |
2652 KB |
Output is correct |
52 |
Correct |
3 ms |
2652 KB |
Output is correct |
53 |
Correct |
249 ms |
177404 KB |
Output is correct |
54 |
Correct |
94 ms |
62352 KB |
Output is correct |
55 |
Correct |
104 ms |
62544 KB |
Output is correct |
56 |
Correct |
86 ms |
61776 KB |
Output is correct |
57 |
Correct |
93 ms |
49060 KB |
Output is correct |
58 |
Correct |
273 ms |
177512 KB |
Output is correct |
59 |
Correct |
140 ms |
90580 KB |
Output is correct |