/*
:-=-
:%@@@@@@@@@=
.#@@@@@%@%@@@@@@=
-@@@@@@@@@@@@@@@@@#
.+@@@@@@@@@@@@@@@@@@@@.
#@@@@@@@@@@@@@@@@@@@@@%
.*@@@@@@@@@@@@@@@@@@@@@@@= / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄\
.@@@@@@@@@@@@@@@@@@@@@@@@# | lftroq ♥ |
+@@@@@@@@@@@%%@@@@@@@@@@@@- \_________/
.%@@@@@@@@@@%##%@@@@@@@@@@@# //
-@@@@@@@@%%%%%%%%%@@@@@@@@@@:
-+- =@@@@@%##%#%##**+#@@@@@@@@@@-
.+++.%@@@%##%%%%%%%#**+##%%@@@@@@+
+=*%@@@#*###%%#=--+=+*##%@@@@@@%.
:*+%@@*+**##*=======+***#@@@@@@@:
=+++**%@###*=++=-==++**#@@@@@@@:
.**===*%@@@##+=**#*==***@@@@@@@@@:
:+++=+*+%%@@%#==+*%#*+=++@@@@@@@@%
.**#*+*+**+%#+=---=***+==*@@@@@@@:
:**#*+=+**@%##*%#++++--=++#@@@@@@@+-::.
=%**###**#@@%*=*%%%%%*==++++%@@@@@@#=-----.
.%@@%***###@@@%*++%%%#*===+=++++%@@@@%*=-----=.
=**##+=+##@@@@=+%+=***#%@#+**++++%@@%+=-------=:
.++===++====@@@@+--==-=#%%%%+++*++++#%+=------==+=.
-++--=++-:::::=#@#----+--*%%*+**+++++++==-----===--+=
:*+=--++-::::::--=-----==-=%==+%%***++=---=---===-=++:
:*+--+=-::::::::---:----+=-=++##***===-::::-+----=*+:
.=--:::.:::-=-::---::--------++--=====-::::--:-==++
:=::::::--::::::--=+-:--++++==++--==:::-:::----==+=
*-:::::::.....:-++++++++++++++=+=---::::===-:------
:+:--:......:-+++++++++++++++++====---:--=-=----:-:=.
:==--:::.:=+++++++++++++++++++++-::===-:-===-----=--:
.+=--:::-:=+++++==++++++++++++++=--:.:-===+-------+=-.
.+==-----::-+++-:+++=====+++++++=--=======--:----==+=.
:+===-----------:-++====+++++++++=--==--+-:------==-::
+=====-::----+-::-+====+++++++++++=---==:-===:-==-==.
:+===========+-::::-----+++++++++++*=--======-=+=-=.
.=========++=-::::::----:::-++++++*+=+======+++==.
.=========-::::::-----:::=+++++**+=+**====-:. .
*======-::::::::-:-----++++++++==++++++-
#======-:::::::::-------++++++++====+++=
=======-:::::::::----:::=+++++++=====+++
=====---:::::::::::------=++++++===+==++=
:===-:--:::::::::::-------+++++++===++=++
.===-:--:::::::::::::-----=+++++====++===.
.+=-::--::::::::::::-:-----++++++====++=+:
.=-:::--::::::::::::-------=+++++===-----+=
.=-:::--:::::::::::---------=++++==-----==+=.
.=-:::---:::::::::::---------+++===-----===++.
.=::::---:::::::-:-----------=++==-:-----===+-
.--:::---:::::::--------------=+=----==--===+-
.=::::---::::::::::-------===--==-------=--==:
.=--::--::::----:---:::::-:----:::-----======-
.=---:--:::---::::::--------------------==++++
*/
#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MOD 1000000007ll
#define MOD2 998244353ll
#define endl '\n'
#define PI acos(-1)
#define INFINITE 1000000000
#define INFINITE2 1000000000000000000ll
#define llll pair<ll,ll>
#define ldld pair<ld,ld>
#define fi first
#define se second
#define sqrt sqrtl
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
int k,n,m,q;
int dp[17][50005][5][5],cost[50005][5][5],mask[50005];
void dnc(int l,int r,int level=0)
{
if(l==r) return;
int mid=(l+r)>>1;
for(int a=0;a<k;a++) dp[level][mid][a][a]=0;
for(int i=mid-1;i>=l;i--)
{
for(int a=0;a<k;a++) for(int b=0;b<k;b++) for(int c=0;c<k;c++) dp[level][i][a][c]=min(dp[level][i][a][c],dp[level][i+1][b][c]+cost[i][a][b]);
}
for(int a=0;a<k;a++) for(int b=0;b<k;b++) dp[level][mid+1][a][b]=cost[mid][a][b];
for(int i=mid+2;i<=r;i++)
{
for(int a=0;a<k;a++) for(int b=0;b<k;b++) for(int c=0;c<k;c++) dp[level][i][c][a]=min(dp[level][i][c][a],dp[level][i-1][c][b]+cost[i-1][b][a]);
}
for(int i=mid+1;i<=r;i++) mask[i]|=(1<<level);
dnc(l,mid,level+1);dnc(mid+1,r,level+1);
}
void solve()
{
cin >> k >> n >> m >> q;
for(int i=0;i<n;i++) for(int a=0;a<k;a++) for(int b=0;b<k;b++) cost[i][a][b]=INFINITE;
for(int level=0;(1<<level)<n;level++) for(int i=0;i<n;i++) for(int a=0;a<k;a++) for(int b=0;b<k;b++) dp[level][i][a][b]=INFINITE;
while(m--)
{
int u,v,w;
cin >> u >> v >> w;
cost[u/k][u%k][v%k]=w;
}
dnc(0,(n-1)/k);
while(q--)
{
int u,v;
cin >> u >> v;
if(u/k==v/k)
{
cout << -1 << endl;
continue;
}
int level=__builtin_ctz(mask[u/k]^mask[v/k]),ans=INFINITE;
for(int j=0;j<k;j++) ans=min(ans,dp[level][u/k][u%k][j]+dp[level][v/k][j][v%k]);
if(ans==INFINITE) ans=-1;
cout << ans << endl;
}
}
int main()
{
fastIO
//freopen("hanhhhh.inp","r",stdin);
//freopen("hanhhhh.out","w",stdout);
int t=1;
//cin >> t;
while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
84820 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10708 KB |
Output is correct |
5 |
Correct |
3 ms |
22876 KB |
Output is correct |
6 |
Correct |
3 ms |
22876 KB |
Output is correct |
7 |
Correct |
3 ms |
22872 KB |
Output is correct |
8 |
Correct |
36 ms |
84572 KB |
Output is correct |
9 |
Correct |
35 ms |
84784 KB |
Output is correct |
10 |
Correct |
23 ms |
84572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
84568 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10744 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
5 ms |
22876 KB |
Output is correct |
8 |
Correct |
5 ms |
22876 KB |
Output is correct |
9 |
Correct |
36 ms |
84584 KB |
Output is correct |
10 |
Correct |
59 ms |
84544 KB |
Output is correct |
11 |
Correct |
44 ms |
84572 KB |
Output is correct |
12 |
Correct |
46 ms |
84636 KB |
Output is correct |
13 |
Correct |
52 ms |
78428 KB |
Output is correct |
14 |
Correct |
32 ms |
78428 KB |
Output is correct |
15 |
Correct |
40 ms |
78428 KB |
Output is correct |
16 |
Correct |
46 ms |
78428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10784 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10840 KB |
Output is correct |
6 |
Correct |
3 ms |
22876 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
23000 KB |
Output is correct |
9 |
Correct |
4 ms |
22876 KB |
Output is correct |
10 |
Correct |
35 ms |
84736 KB |
Output is correct |
11 |
Correct |
44 ms |
84568 KB |
Output is correct |
12 |
Correct |
48 ms |
84568 KB |
Output is correct |
13 |
Correct |
50 ms |
84572 KB |
Output is correct |
14 |
Correct |
51 ms |
84584 KB |
Output is correct |
15 |
Correct |
39 ms |
78428 KB |
Output is correct |
16 |
Correct |
41 ms |
78424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10784 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10840 KB |
Output is correct |
6 |
Correct |
3 ms |
22876 KB |
Output is correct |
7 |
Correct |
3 ms |
22876 KB |
Output is correct |
8 |
Correct |
4 ms |
23000 KB |
Output is correct |
9 |
Correct |
4 ms |
22876 KB |
Output is correct |
10 |
Correct |
35 ms |
84736 KB |
Output is correct |
11 |
Correct |
44 ms |
84568 KB |
Output is correct |
12 |
Correct |
48 ms |
84568 KB |
Output is correct |
13 |
Correct |
50 ms |
84572 KB |
Output is correct |
14 |
Correct |
51 ms |
84584 KB |
Output is correct |
15 |
Correct |
39 ms |
78428 KB |
Output is correct |
16 |
Correct |
41 ms |
78424 KB |
Output is correct |
17 |
Correct |
46 ms |
84568 KB |
Output is correct |
18 |
Correct |
1 ms |
10584 KB |
Output is correct |
19 |
Correct |
2 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10584 KB |
Output is correct |
23 |
Correct |
4 ms |
22876 KB |
Output is correct |
24 |
Correct |
4 ms |
22876 KB |
Output is correct |
25 |
Correct |
5 ms |
22876 KB |
Output is correct |
26 |
Correct |
5 ms |
22996 KB |
Output is correct |
27 |
Correct |
37 ms |
84568 KB |
Output is correct |
28 |
Correct |
46 ms |
84636 KB |
Output is correct |
29 |
Correct |
49 ms |
84588 KB |
Output is correct |
30 |
Correct |
43 ms |
84572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
84820 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10708 KB |
Output is correct |
5 |
Correct |
3 ms |
22876 KB |
Output is correct |
6 |
Correct |
3 ms |
22876 KB |
Output is correct |
7 |
Correct |
3 ms |
22872 KB |
Output is correct |
8 |
Correct |
36 ms |
84572 KB |
Output is correct |
9 |
Correct |
35 ms |
84784 KB |
Output is correct |
10 |
Correct |
23 ms |
84572 KB |
Output is correct |
11 |
Correct |
40 ms |
84568 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10744 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
5 ms |
22876 KB |
Output is correct |
18 |
Correct |
5 ms |
22876 KB |
Output is correct |
19 |
Correct |
36 ms |
84584 KB |
Output is correct |
20 |
Correct |
59 ms |
84544 KB |
Output is correct |
21 |
Correct |
44 ms |
84572 KB |
Output is correct |
22 |
Correct |
46 ms |
84636 KB |
Output is correct |
23 |
Correct |
52 ms |
78428 KB |
Output is correct |
24 |
Correct |
32 ms |
78428 KB |
Output is correct |
25 |
Correct |
40 ms |
78428 KB |
Output is correct |
26 |
Correct |
46 ms |
78428 KB |
Output is correct |
27 |
Correct |
1 ms |
10584 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10784 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
1 ms |
10840 KB |
Output is correct |
32 |
Correct |
3 ms |
22876 KB |
Output is correct |
33 |
Correct |
3 ms |
22876 KB |
Output is correct |
34 |
Correct |
4 ms |
23000 KB |
Output is correct |
35 |
Correct |
4 ms |
22876 KB |
Output is correct |
36 |
Correct |
35 ms |
84736 KB |
Output is correct |
37 |
Correct |
44 ms |
84568 KB |
Output is correct |
38 |
Correct |
48 ms |
84568 KB |
Output is correct |
39 |
Correct |
50 ms |
84572 KB |
Output is correct |
40 |
Correct |
51 ms |
84584 KB |
Output is correct |
41 |
Correct |
39 ms |
78428 KB |
Output is correct |
42 |
Correct |
41 ms |
78424 KB |
Output is correct |
43 |
Correct |
46 ms |
84568 KB |
Output is correct |
44 |
Correct |
1 ms |
10584 KB |
Output is correct |
45 |
Correct |
2 ms |
10588 KB |
Output is correct |
46 |
Correct |
1 ms |
10588 KB |
Output is correct |
47 |
Correct |
1 ms |
10588 KB |
Output is correct |
48 |
Correct |
1 ms |
10584 KB |
Output is correct |
49 |
Correct |
4 ms |
22876 KB |
Output is correct |
50 |
Correct |
4 ms |
22876 KB |
Output is correct |
51 |
Correct |
5 ms |
22876 KB |
Output is correct |
52 |
Correct |
5 ms |
22996 KB |
Output is correct |
53 |
Correct |
37 ms |
84568 KB |
Output is correct |
54 |
Correct |
46 ms |
84636 KB |
Output is correct |
55 |
Correct |
49 ms |
84588 KB |
Output is correct |
56 |
Correct |
43 ms |
84572 KB |
Output is correct |
57 |
Correct |
79 ms |
84572 KB |
Output is correct |
58 |
Correct |
36 ms |
84584 KB |
Output is correct |
59 |
Correct |
41 ms |
84572 KB |
Output is correct |