#include <bits/stdc++.h>
#define forr(_a,_b,_c) for(_a = (_b); _a <= (_c); ++_a)
#define ford(_a,_b,_c) for(_a = (_b) + 1; _a --> (_c);)
#define forf(_a,_b,_c) for(_a = (_b); _a < (_c); ++_a)
#define st first
#define nd second
#define ll long long
#define ull unsigned long long
#define pii pair <ll,ll>
#define pll pair <ll,ll>
#define piii pair <ll,pii>
#define vi vector <ll>
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define file "test"
using namespace std;
const ll N = 5e5 + 5;
const ll oo = 1e17;
const ll mod = 1e9 + 7;
inline void mnmize (ll &u, ll v){
if (u > v)
u = v;
}
ll n,m,x,q,u,v,w,i,up[20][100005][6][6],j,k,g,l;
vector <pii> a[N];
ll res[6][6],nres[6][6];
void jump (ll u, ll d){
forr (l,0,18)
if ((1 << l) & d){
forf (i,0,x)
forf (j,0,x)
nres[i][j] = oo;
forf (i,0,x)
forf (j,0,x)
forf (k,0,x)
mnmize (nres[i][k],res[i][j] + up[l][u][j][k]);
swap(res,nres);
u += (1 << l);
// cout << u << " " << res[0][1] << "\n";
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> x >> n >> m >> q;
ll bl = n / x + 1;
forr (i,1,m){
cin >> u >> v >> w;
a[u].pb({v,w});
}
forr (l,0,18)
forr (i,0,bl)
forf (j,0,x)
forf (k,0,x)
up[l][i][j][k] = oo;
forr (i,0,bl)
forf (j,0,x)
for (pii v : a[i * x + j]){
mnmize (up[0][i][j][v.st % x],v.nd);
}
forr (l,1,18)
forr (i,0,bl){
ll f = i + (1 << (l - 1));
forf (j,0,x)
forf (k,0,x)
forf (g,0,x)
mnmize (up[l][i][j][g],up[l - 1][i][j][k] + up[l - 1][f][k][g]);
}
while (q--){
ll b,c;
cin >> b >> c;
memset(res,0,sizeof res);
forf (i,0,x)
forf (j,0,x)
if (i != j)
res[i][j] = oo;
//cout << b/x << " " << c/x << " " << b % x << " " << c % x << "\n";
jump(b/x, c/x - b/x);
if (res[b % x][c % x] < oo && b / x != c / x)
cout << res[b % x][c % x] << "\n";
else cout << "-1\n";
}
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
308732 KB |
Output is correct |
2 |
Correct |
7 ms |
49756 KB |
Output is correct |
3 |
Correct |
7 ms |
49756 KB |
Output is correct |
4 |
Correct |
7 ms |
49848 KB |
Output is correct |
5 |
Correct |
8 ms |
53852 KB |
Output is correct |
6 |
Correct |
8 ms |
53852 KB |
Output is correct |
7 |
Correct |
8 ms |
53852 KB |
Output is correct |
8 |
Correct |
88 ms |
306600 KB |
Output is correct |
9 |
Correct |
86 ms |
306688 KB |
Output is correct |
10 |
Correct |
75 ms |
307076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
187952 KB |
Output is correct |
2 |
Correct |
7 ms |
49752 KB |
Output is correct |
3 |
Correct |
7 ms |
49756 KB |
Output is correct |
4 |
Correct |
7 ms |
49756 KB |
Output is correct |
5 |
Correct |
7 ms |
49768 KB |
Output is correct |
6 |
Correct |
7 ms |
49756 KB |
Output is correct |
7 |
Correct |
11 ms |
54108 KB |
Output is correct |
8 |
Correct |
13 ms |
51980 KB |
Output is correct |
9 |
Correct |
81 ms |
309588 KB |
Output is correct |
10 |
Correct |
82 ms |
144296 KB |
Output is correct |
11 |
Correct |
92 ms |
189604 KB |
Output is correct |
12 |
Correct |
63 ms |
141392 KB |
Output is correct |
13 |
Correct |
78 ms |
87768 KB |
Output is correct |
14 |
Correct |
55 ms |
109596 KB |
Output is correct |
15 |
Correct |
54 ms |
83696 KB |
Output is correct |
16 |
Correct |
55 ms |
83896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
49756 KB |
Output is correct |
2 |
Correct |
7 ms |
49756 KB |
Output is correct |
3 |
Correct |
7 ms |
49820 KB |
Output is correct |
4 |
Correct |
7 ms |
49872 KB |
Output is correct |
5 |
Correct |
7 ms |
49768 KB |
Output is correct |
6 |
Correct |
7 ms |
53848 KB |
Output is correct |
7 |
Correct |
8 ms |
52060 KB |
Output is correct |
8 |
Correct |
8 ms |
50012 KB |
Output is correct |
9 |
Correct |
9 ms |
52056 KB |
Output is correct |
10 |
Correct |
83 ms |
311380 KB |
Output is correct |
11 |
Correct |
77 ms |
189456 KB |
Output is correct |
12 |
Correct |
68 ms |
144216 KB |
Output is correct |
13 |
Correct |
69 ms |
144720 KB |
Output is correct |
14 |
Correct |
63 ms |
143112 KB |
Output is correct |
15 |
Correct |
49 ms |
83804 KB |
Output is correct |
16 |
Correct |
49 ms |
84108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
49756 KB |
Output is correct |
2 |
Correct |
7 ms |
49756 KB |
Output is correct |
3 |
Correct |
7 ms |
49820 KB |
Output is correct |
4 |
Correct |
7 ms |
49872 KB |
Output is correct |
5 |
Correct |
7 ms |
49768 KB |
Output is correct |
6 |
Correct |
7 ms |
53848 KB |
Output is correct |
7 |
Correct |
8 ms |
52060 KB |
Output is correct |
8 |
Correct |
8 ms |
50012 KB |
Output is correct |
9 |
Correct |
9 ms |
52056 KB |
Output is correct |
10 |
Correct |
83 ms |
311380 KB |
Output is correct |
11 |
Correct |
77 ms |
189456 KB |
Output is correct |
12 |
Correct |
68 ms |
144216 KB |
Output is correct |
13 |
Correct |
69 ms |
144720 KB |
Output is correct |
14 |
Correct |
63 ms |
143112 KB |
Output is correct |
15 |
Correct |
49 ms |
83804 KB |
Output is correct |
16 |
Correct |
49 ms |
84108 KB |
Output is correct |
17 |
Correct |
74 ms |
189380 KB |
Output is correct |
18 |
Correct |
7 ms |
49756 KB |
Output is correct |
19 |
Correct |
7 ms |
49756 KB |
Output is correct |
20 |
Correct |
7 ms |
49756 KB |
Output is correct |
21 |
Correct |
7 ms |
49756 KB |
Output is correct |
22 |
Correct |
6 ms |
49756 KB |
Output is correct |
23 |
Correct |
8 ms |
54012 KB |
Output is correct |
24 |
Correct |
10 ms |
52060 KB |
Output is correct |
25 |
Correct |
11 ms |
50044 KB |
Output is correct |
26 |
Correct |
10 ms |
52060 KB |
Output is correct |
27 |
Correct |
81 ms |
311236 KB |
Output is correct |
28 |
Correct |
82 ms |
144288 KB |
Output is correct |
29 |
Correct |
77 ms |
144976 KB |
Output is correct |
30 |
Correct |
71 ms |
143012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
308732 KB |
Output is correct |
2 |
Correct |
7 ms |
49756 KB |
Output is correct |
3 |
Correct |
7 ms |
49756 KB |
Output is correct |
4 |
Correct |
7 ms |
49848 KB |
Output is correct |
5 |
Correct |
8 ms |
53852 KB |
Output is correct |
6 |
Correct |
8 ms |
53852 KB |
Output is correct |
7 |
Correct |
8 ms |
53852 KB |
Output is correct |
8 |
Correct |
88 ms |
306600 KB |
Output is correct |
9 |
Correct |
86 ms |
306688 KB |
Output is correct |
10 |
Correct |
75 ms |
307076 KB |
Output is correct |
11 |
Correct |
76 ms |
187952 KB |
Output is correct |
12 |
Correct |
7 ms |
49752 KB |
Output is correct |
13 |
Correct |
7 ms |
49756 KB |
Output is correct |
14 |
Correct |
7 ms |
49756 KB |
Output is correct |
15 |
Correct |
7 ms |
49768 KB |
Output is correct |
16 |
Correct |
7 ms |
49756 KB |
Output is correct |
17 |
Correct |
11 ms |
54108 KB |
Output is correct |
18 |
Correct |
13 ms |
51980 KB |
Output is correct |
19 |
Correct |
81 ms |
309588 KB |
Output is correct |
20 |
Correct |
82 ms |
144296 KB |
Output is correct |
21 |
Correct |
92 ms |
189604 KB |
Output is correct |
22 |
Correct |
63 ms |
141392 KB |
Output is correct |
23 |
Correct |
78 ms |
87768 KB |
Output is correct |
24 |
Correct |
55 ms |
109596 KB |
Output is correct |
25 |
Correct |
54 ms |
83696 KB |
Output is correct |
26 |
Correct |
55 ms |
83896 KB |
Output is correct |
27 |
Correct |
7 ms |
49756 KB |
Output is correct |
28 |
Correct |
7 ms |
49756 KB |
Output is correct |
29 |
Correct |
7 ms |
49820 KB |
Output is correct |
30 |
Correct |
7 ms |
49872 KB |
Output is correct |
31 |
Correct |
7 ms |
49768 KB |
Output is correct |
32 |
Correct |
7 ms |
53848 KB |
Output is correct |
33 |
Correct |
8 ms |
52060 KB |
Output is correct |
34 |
Correct |
8 ms |
50012 KB |
Output is correct |
35 |
Correct |
9 ms |
52056 KB |
Output is correct |
36 |
Correct |
83 ms |
311380 KB |
Output is correct |
37 |
Correct |
77 ms |
189456 KB |
Output is correct |
38 |
Correct |
68 ms |
144216 KB |
Output is correct |
39 |
Correct |
69 ms |
144720 KB |
Output is correct |
40 |
Correct |
63 ms |
143112 KB |
Output is correct |
41 |
Correct |
49 ms |
83804 KB |
Output is correct |
42 |
Correct |
49 ms |
84108 KB |
Output is correct |
43 |
Correct |
74 ms |
189380 KB |
Output is correct |
44 |
Correct |
7 ms |
49756 KB |
Output is correct |
45 |
Correct |
7 ms |
49756 KB |
Output is correct |
46 |
Correct |
7 ms |
49756 KB |
Output is correct |
47 |
Correct |
7 ms |
49756 KB |
Output is correct |
48 |
Correct |
6 ms |
49756 KB |
Output is correct |
49 |
Correct |
8 ms |
54012 KB |
Output is correct |
50 |
Correct |
10 ms |
52060 KB |
Output is correct |
51 |
Correct |
11 ms |
50044 KB |
Output is correct |
52 |
Correct |
10 ms |
52060 KB |
Output is correct |
53 |
Correct |
81 ms |
311236 KB |
Output is correct |
54 |
Correct |
82 ms |
144288 KB |
Output is correct |
55 |
Correct |
77 ms |
144976 KB |
Output is correct |
56 |
Correct |
71 ms |
143012 KB |
Output is correct |
57 |
Correct |
110 ms |
125312 KB |
Output is correct |
58 |
Correct |
86 ms |
311376 KB |
Output is correct |
59 |
Correct |
80 ms |
189776 KB |
Output is correct |