#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define debug(x) cout << #x << "= " << x << ", "
#define ll long long
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define SZ(x) (int) x.size()
#define wall cout << endl;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 5e4 + 10 , maxsq = 100 , maxk = 7 , inf = 1e9;
int n , m , q , k , dis[maxn] , dis_jmp[maxn][maxk];
vector <pair<int , int>> adj[maxn];
vector <int> vec;
void Relax()
{
for(auto u : vec) dis[u] = inf;
vec.clear();
}
void find_nex(int now , int to)
{
int l = now * k , r = min(n , l + k);
for(int i = l ; i < r ; i++) vec.pb(i);
if(now == to) return;
for(int i = l ; i < r ; i++)
for(auto u : adj[i])
dis[u.F] = min(dis[u.F] , dis[i] + u.S);
find_nex(now + 1 , to);
}
void Build(int num)
{
int l = num * k , r = min(n , l + k);
int l2 = (num + maxsq) * k;
for(int i = l ; i < r ; i++)
{
dis[i] = 0;
find_nex(num , num + maxsq);
for(int j = 0 ; j < k ; j++)
dis_jmp[i][j] = dis[l2 + j];
Relax();
}
}
void jmp_nex(int a , int b)
{
int l = a * k , r = min(n , l + k) , l2 = (a + maxsq) * k;
for(int i = l ; i < r ; i++) vec.pb(i);
if(a == b) return;
for(int i = l ; i < r ; i++)
for(int j = 0 ; j < k ; j++)
dis[l2 + j] = min(dis[l2 + j] , dis[i] + dis_jmp[i][j]);
jmp_nex(a + maxsq , b);
}
void Find_dis(int a , int b)
{
int tmp = (a + maxsq) / maxsq;
tmp *= maxsq;
if(b <= tmp)
{
find_nex(a , b);
return;
}
find_nex(a , tmp);
int tmp2 = b / maxsq;
tmp2 *= maxsq;
jmp_nex(tmp , tmp2);
find_nex(tmp2 , b);
}
int32_t main()
{
fast;
cin >> k >> n >> m >> q;
for(int i = 0 ; i < n ; i++) dis[i] = inf;
for(int i = 0 ; i < m ; i++)
{
int v , u , c; cin >> v >> u >> c;
adj[v].pb({u , c});
}
for(int i = 0 ; (i + maxsq) * k < n ; i += maxsq) Build(i);
while(q--)
{
int v , u; cin >> v >> u;
if(v > u)
{
cout << -1 << '\n';
continue;
}
dis[v] = 0;
vec.pb(v);
Find_dis(v / k , u / k);
cout << (dis[u] == inf ? -1 : dis[u]) << '\n';
Relax();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
4684 KB |
Output is correct |
2 |
Correct |
1 ms |
1512 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
3 ms |
1492 KB |
Output is correct |
6 |
Correct |
3 ms |
1492 KB |
Output is correct |
7 |
Correct |
3 ms |
1492 KB |
Output is correct |
8 |
Correct |
62 ms |
4644 KB |
Output is correct |
9 |
Correct |
55 ms |
4564 KB |
Output is correct |
10 |
Correct |
29 ms |
3084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
4296 KB |
Output is correct |
2 |
Correct |
2 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
22 ms |
1568 KB |
Output is correct |
8 |
Correct |
43 ms |
1696 KB |
Output is correct |
9 |
Correct |
76 ms |
4640 KB |
Output is correct |
10 |
Correct |
200 ms |
4928 KB |
Output is correct |
11 |
Correct |
146 ms |
4280 KB |
Output is correct |
12 |
Correct |
170 ms |
3832 KB |
Output is correct |
13 |
Correct |
126 ms |
4616 KB |
Output is correct |
14 |
Correct |
95 ms |
3716 KB |
Output is correct |
15 |
Correct |
96 ms |
3168 KB |
Output is correct |
16 |
Correct |
99 ms |
3176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
2 ms |
1492 KB |
Output is correct |
7 |
Correct |
2 ms |
1492 KB |
Output is correct |
8 |
Correct |
4 ms |
1492 KB |
Output is correct |
9 |
Correct |
3 ms |
1492 KB |
Output is correct |
10 |
Correct |
24 ms |
4556 KB |
Output is correct |
11 |
Correct |
36 ms |
4136 KB |
Output is correct |
12 |
Correct |
52 ms |
4824 KB |
Output is correct |
13 |
Correct |
50 ms |
4992 KB |
Output is correct |
14 |
Correct |
41 ms |
4432 KB |
Output is correct |
15 |
Correct |
30 ms |
3172 KB |
Output is correct |
16 |
Correct |
33 ms |
3144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
2 ms |
1492 KB |
Output is correct |
7 |
Correct |
2 ms |
1492 KB |
Output is correct |
8 |
Correct |
4 ms |
1492 KB |
Output is correct |
9 |
Correct |
3 ms |
1492 KB |
Output is correct |
10 |
Correct |
24 ms |
4556 KB |
Output is correct |
11 |
Correct |
36 ms |
4136 KB |
Output is correct |
12 |
Correct |
52 ms |
4824 KB |
Output is correct |
13 |
Correct |
50 ms |
4992 KB |
Output is correct |
14 |
Correct |
41 ms |
4432 KB |
Output is correct |
15 |
Correct |
30 ms |
3172 KB |
Output is correct |
16 |
Correct |
33 ms |
3144 KB |
Output is correct |
17 |
Correct |
59 ms |
4200 KB |
Output is correct |
18 |
Correct |
1 ms |
1492 KB |
Output is correct |
19 |
Correct |
1 ms |
1492 KB |
Output is correct |
20 |
Correct |
1 ms |
1492 KB |
Output is correct |
21 |
Correct |
1 ms |
1492 KB |
Output is correct |
22 |
Correct |
1 ms |
1492 KB |
Output is correct |
23 |
Correct |
6 ms |
1580 KB |
Output is correct |
24 |
Correct |
10 ms |
1580 KB |
Output is correct |
25 |
Correct |
20 ms |
1616 KB |
Output is correct |
26 |
Correct |
23 ms |
1492 KB |
Output is correct |
27 |
Correct |
32 ms |
4596 KB |
Output is correct |
28 |
Correct |
84 ms |
4832 KB |
Output is correct |
29 |
Correct |
86 ms |
5072 KB |
Output is correct |
30 |
Correct |
81 ms |
4436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
4684 KB |
Output is correct |
2 |
Correct |
1 ms |
1512 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
3 ms |
1492 KB |
Output is correct |
6 |
Correct |
3 ms |
1492 KB |
Output is correct |
7 |
Correct |
3 ms |
1492 KB |
Output is correct |
8 |
Correct |
62 ms |
4644 KB |
Output is correct |
9 |
Correct |
55 ms |
4564 KB |
Output is correct |
10 |
Correct |
29 ms |
3084 KB |
Output is correct |
11 |
Correct |
120 ms |
4296 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
1 ms |
1492 KB |
Output is correct |
15 |
Correct |
1 ms |
1492 KB |
Output is correct |
16 |
Correct |
1 ms |
1492 KB |
Output is correct |
17 |
Correct |
22 ms |
1568 KB |
Output is correct |
18 |
Correct |
43 ms |
1696 KB |
Output is correct |
19 |
Correct |
76 ms |
4640 KB |
Output is correct |
20 |
Correct |
200 ms |
4928 KB |
Output is correct |
21 |
Correct |
146 ms |
4280 KB |
Output is correct |
22 |
Correct |
170 ms |
3832 KB |
Output is correct |
23 |
Correct |
126 ms |
4616 KB |
Output is correct |
24 |
Correct |
95 ms |
3716 KB |
Output is correct |
25 |
Correct |
96 ms |
3168 KB |
Output is correct |
26 |
Correct |
99 ms |
3176 KB |
Output is correct |
27 |
Correct |
1 ms |
1492 KB |
Output is correct |
28 |
Correct |
1 ms |
1492 KB |
Output is correct |
29 |
Correct |
1 ms |
1492 KB |
Output is correct |
30 |
Correct |
1 ms |
1492 KB |
Output is correct |
31 |
Correct |
1 ms |
1492 KB |
Output is correct |
32 |
Correct |
2 ms |
1492 KB |
Output is correct |
33 |
Correct |
2 ms |
1492 KB |
Output is correct |
34 |
Correct |
4 ms |
1492 KB |
Output is correct |
35 |
Correct |
3 ms |
1492 KB |
Output is correct |
36 |
Correct |
24 ms |
4556 KB |
Output is correct |
37 |
Correct |
36 ms |
4136 KB |
Output is correct |
38 |
Correct |
52 ms |
4824 KB |
Output is correct |
39 |
Correct |
50 ms |
4992 KB |
Output is correct |
40 |
Correct |
41 ms |
4432 KB |
Output is correct |
41 |
Correct |
30 ms |
3172 KB |
Output is correct |
42 |
Correct |
33 ms |
3144 KB |
Output is correct |
43 |
Correct |
59 ms |
4200 KB |
Output is correct |
44 |
Correct |
1 ms |
1492 KB |
Output is correct |
45 |
Correct |
1 ms |
1492 KB |
Output is correct |
46 |
Correct |
1 ms |
1492 KB |
Output is correct |
47 |
Correct |
1 ms |
1492 KB |
Output is correct |
48 |
Correct |
1 ms |
1492 KB |
Output is correct |
49 |
Correct |
6 ms |
1580 KB |
Output is correct |
50 |
Correct |
10 ms |
1580 KB |
Output is correct |
51 |
Correct |
20 ms |
1616 KB |
Output is correct |
52 |
Correct |
23 ms |
1492 KB |
Output is correct |
53 |
Correct |
32 ms |
4596 KB |
Output is correct |
54 |
Correct |
84 ms |
4832 KB |
Output is correct |
55 |
Correct |
86 ms |
5072 KB |
Output is correct |
56 |
Correct |
81 ms |
4436 KB |
Output is correct |
57 |
Correct |
228 ms |
4964 KB |
Output is correct |
58 |
Correct |
59 ms |
4644 KB |
Output is correct |
59 |
Correct |
131 ms |
4264 KB |
Output is correct |