#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[50005], rev[50005];
map<pair<int, int>, int> mp;
int dist[50005];
void recur(int l, int r, int c)
{
int mid=(l+r)/2;
for (int i=c*mid; i<c*(mid+1); i++)
{
for (int j=c*l; j<c*(r+1); j++)
dist[j]=1e9;
dist[i]=0;
for (int j=c*mid-1; j>=c*l; j--)
{
for (int k=0; k<adj[j].size(); k++)
dist[j]=min(dist[j], dist[adj[j][k].first]+adj[j][k].second);
mp[{j, i}]=dist[j];
}
for (int j=c*(mid+1); j<c*(r+1); j++)
{
for (int k=0; k<rev[j].size(); k++)
dist[j]=min(dist[j], dist[rev[j][k].first]+rev[j][k].second);
mp[{i, j}]=dist[j];
}
}
if (l+2<=mid)
recur(l, mid-1, c);
if (mid+2<=r)
recur(mid+1, r, c);
}
int dfs(int l, int r, int u, int v)
{
int mid=(l+r)/2;
if (v<mid)
return dfs(l, mid-1, u, v);
if (u>mid)
return dfs(mid+1, r, u, v);
return mid;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int c, n, m, q;
cin >> c >> n >> m >> q;
for (int i=0; i<m; i++)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
rev[v].push_back({u, w});
}
recur(0, (n-1)/c, c);
for (int i=0; i<q; i++)
{
int u, v;
cin >> u >> v;
int x=dfs(0, (n-1)/c, u/c, v/c);
if (u/c==x && v/c==x)
cout << -1 << '\n';
else if (u/c==x || v/c==x)
cout << (mp[{u, v}]==1e9?-1:mp[{u, v}]) << '\n';
else
{
int ans=1e9;
for (int j=c*x; j<c*(x+1); j++)
ans=min(ans, mp[{u, j}]+mp[{j, v}]);
cout << (ans==1e9?-1:ans) << '\n';
}
}
}
Compilation message
toll.cpp: In function 'void recur(int, int, int)':
toll.cpp:16:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for (int k=0; k<adj[j].size(); k++)
| ~^~~~~~~~~~~~~~
toll.cpp:22:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int k=0; k<rev[j].size(); k++)
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
48864 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
3 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3160 KB |
Output is correct |
7 |
Correct |
3 ms |
3420 KB |
Output is correct |
8 |
Correct |
156 ms |
49684 KB |
Output is correct |
9 |
Correct |
147 ms |
49492 KB |
Output is correct |
10 |
Correct |
130 ms |
45932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
85588 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
5 ms |
3420 KB |
Output is correct |
8 |
Correct |
7 ms |
3676 KB |
Output is correct |
9 |
Correct |
138 ms |
49840 KB |
Output is correct |
10 |
Correct |
367 ms |
123136 KB |
Output is correct |
11 |
Correct |
257 ms |
87440 KB |
Output is correct |
12 |
Correct |
357 ms |
120052 KB |
Output is correct |
13 |
Correct |
383 ms |
110416 KB |
Output is correct |
14 |
Correct |
211 ms |
71572 KB |
Output is correct |
15 |
Correct |
310 ms |
106496 KB |
Output is correct |
16 |
Correct |
337 ms |
106652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
3160 KB |
Output is correct |
7 |
Correct |
3 ms |
3652 KB |
Output is correct |
8 |
Correct |
6 ms |
4700 KB |
Output is correct |
9 |
Correct |
6 ms |
4704 KB |
Output is correct |
10 |
Correct |
137 ms |
48980 KB |
Output is correct |
11 |
Correct |
237 ms |
85540 KB |
Output is correct |
12 |
Correct |
361 ms |
120912 KB |
Output is correct |
13 |
Correct |
367 ms |
121176 KB |
Output is correct |
14 |
Correct |
350 ms |
120172 KB |
Output is correct |
15 |
Correct |
303 ms |
105368 KB |
Output is correct |
16 |
Correct |
331 ms |
105288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2648 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
2 ms |
3160 KB |
Output is correct |
7 |
Correct |
3 ms |
3652 KB |
Output is correct |
8 |
Correct |
6 ms |
4700 KB |
Output is correct |
9 |
Correct |
6 ms |
4704 KB |
Output is correct |
10 |
Correct |
137 ms |
48980 KB |
Output is correct |
11 |
Correct |
237 ms |
85540 KB |
Output is correct |
12 |
Correct |
361 ms |
120912 KB |
Output is correct |
13 |
Correct |
367 ms |
121176 KB |
Output is correct |
14 |
Correct |
350 ms |
120172 KB |
Output is correct |
15 |
Correct |
303 ms |
105368 KB |
Output is correct |
16 |
Correct |
331 ms |
105288 KB |
Output is correct |
17 |
Correct |
256 ms |
85500 KB |
Output is correct |
18 |
Correct |
1 ms |
2648 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2648 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
3 ms |
3420 KB |
Output is correct |
24 |
Correct |
5 ms |
3672 KB |
Output is correct |
25 |
Correct |
9 ms |
4700 KB |
Output is correct |
26 |
Correct |
8 ms |
4444 KB |
Output is correct |
27 |
Correct |
138 ms |
49812 KB |
Output is correct |
28 |
Correct |
378 ms |
122960 KB |
Output is correct |
29 |
Correct |
391 ms |
124244 KB |
Output is correct |
30 |
Correct |
394 ms |
122028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
48864 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
3 ms |
3164 KB |
Output is correct |
6 |
Correct |
2 ms |
3160 KB |
Output is correct |
7 |
Correct |
3 ms |
3420 KB |
Output is correct |
8 |
Correct |
156 ms |
49684 KB |
Output is correct |
9 |
Correct |
147 ms |
49492 KB |
Output is correct |
10 |
Correct |
130 ms |
45932 KB |
Output is correct |
11 |
Correct |
256 ms |
85588 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
5 ms |
3420 KB |
Output is correct |
18 |
Correct |
7 ms |
3676 KB |
Output is correct |
19 |
Correct |
138 ms |
49840 KB |
Output is correct |
20 |
Correct |
367 ms |
123136 KB |
Output is correct |
21 |
Correct |
257 ms |
87440 KB |
Output is correct |
22 |
Correct |
357 ms |
120052 KB |
Output is correct |
23 |
Correct |
383 ms |
110416 KB |
Output is correct |
24 |
Correct |
211 ms |
71572 KB |
Output is correct |
25 |
Correct |
310 ms |
106496 KB |
Output is correct |
26 |
Correct |
337 ms |
106652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
1 ms |
2648 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
3160 KB |
Output is correct |
33 |
Correct |
3 ms |
3652 KB |
Output is correct |
34 |
Correct |
6 ms |
4700 KB |
Output is correct |
35 |
Correct |
6 ms |
4704 KB |
Output is correct |
36 |
Correct |
137 ms |
48980 KB |
Output is correct |
37 |
Correct |
237 ms |
85540 KB |
Output is correct |
38 |
Correct |
361 ms |
120912 KB |
Output is correct |
39 |
Correct |
367 ms |
121176 KB |
Output is correct |
40 |
Correct |
350 ms |
120172 KB |
Output is correct |
41 |
Correct |
303 ms |
105368 KB |
Output is correct |
42 |
Correct |
331 ms |
105288 KB |
Output is correct |
43 |
Correct |
256 ms |
85500 KB |
Output is correct |
44 |
Correct |
1 ms |
2648 KB |
Output is correct |
45 |
Correct |
1 ms |
2652 KB |
Output is correct |
46 |
Correct |
1 ms |
2652 KB |
Output is correct |
47 |
Correct |
1 ms |
2648 KB |
Output is correct |
48 |
Correct |
1 ms |
2652 KB |
Output is correct |
49 |
Correct |
3 ms |
3420 KB |
Output is correct |
50 |
Correct |
5 ms |
3672 KB |
Output is correct |
51 |
Correct |
9 ms |
4700 KB |
Output is correct |
52 |
Correct |
8 ms |
4444 KB |
Output is correct |
53 |
Correct |
138 ms |
49812 KB |
Output is correct |
54 |
Correct |
378 ms |
122960 KB |
Output is correct |
55 |
Correct |
391 ms |
124244 KB |
Output is correct |
56 |
Correct |
394 ms |
122028 KB |
Output is correct |
57 |
Correct |
527 ms |
158176 KB |
Output is correct |
58 |
Correct |
149 ms |
49788 KB |
Output is correct |
59 |
Correct |
269 ms |
87244 KB |
Output is correct |