#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e5+10;
vi zap[N],war[N];
int z[N];
vector<pair<int,int> >graf[2][N];
int odl[2][N],n,m,ans[N];
void bfs(int p,bool xd,int w,int kon){
vector<int> curr;
curr.push_back(p);
odl[xd][p]=0;
while(curr.size() and w!=kon){
vi nxt;
for(auto v:curr){
for(auto [u,c]:graf[xd][v]){
if(odl[xd][u]==-1) odl[xd][u]=odl[xd][v]+c,nxt.push_back(u);
else odl[xd][u]=min(odl[xd][u],odl[xd][v]+c);
}
}
if(xd) w--;
else w++;
curr=nxt;
}
}
void dc(int p=0,int k=n/m){
if(p==k) return;
int sr=(p+k)>>1;
for(auto curr:war[sr]){
for(int i=p;i<=k;i++)
for(auto xd:war[i]) odl[1][xd]=-1,odl[0][xd]=-1;
bfs(curr,0,sr,k);
bfs(curr,1,sr,p);
for(int i=p;i<=sr;i++){
for(auto xd:war[i]){
for(auto u:zap[xd])
if(z[u]/m>sr and z[u]/m<=k and odl[1][xd]!=-1 and odl[0][z[u]]!=-1) ans[u]=min(ans[u],odl[1][xd]+odl[0][z[u]]);
}
}
}
dc(p,sr),dc(sr+1,k);
}
void solve(){
int kr,q;
cin>>m>>n>>kr>>q;
for(int i=1;i<=kr;i++){
int a,b,c;
cin>>a>>b>>c;
graf[0][a].push_back({b,c}),graf[1][b].push_back({a,c});
}
for(int i=0;i<n;i++) war[i/m].push_back(i);
for(int i=1;i<=q;i++){
int a,b;
cin>>a>>b;
ans[i]=INT_MAX;
z[i]=b,zap[a].push_back(i);
}
for(int i=1;i<=n;i++) odl[0][i]=-1;
dc();
for(int i=1;i<=q;i++) cout<<(ans[i]==INT_MAX?-1:ans[i])<<"\n";
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
Compilation message
toll.cpp: In function 'void bfs(int, bool, int, int)':
toll.cpp:20:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | for(auto [u,c]:graf[xd][v]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
15156 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9648 KB |
Output is correct |
5 |
Correct |
8 ms |
9812 KB |
Output is correct |
6 |
Correct |
7 ms |
9812 KB |
Output is correct |
7 |
Correct |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
73 ms |
16100 KB |
Output is correct |
9 |
Correct |
52 ms |
15824 KB |
Output is correct |
10 |
Correct |
22 ms |
12172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
14152 KB |
Output is correct |
2 |
Correct |
6 ms |
9740 KB |
Output is correct |
3 |
Correct |
5 ms |
9700 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9732 KB |
Output is correct |
6 |
Correct |
5 ms |
9732 KB |
Output is correct |
7 |
Correct |
9 ms |
10068 KB |
Output is correct |
8 |
Correct |
12 ms |
10184 KB |
Output is correct |
9 |
Correct |
68 ms |
15868 KB |
Output is correct |
10 |
Correct |
202 ms |
17540 KB |
Output is correct |
11 |
Correct |
130 ms |
15896 KB |
Output is correct |
12 |
Correct |
87 ms |
15040 KB |
Output is correct |
13 |
Correct |
178 ms |
17696 KB |
Output is correct |
14 |
Correct |
118 ms |
14796 KB |
Output is correct |
15 |
Correct |
127 ms |
14100 KB |
Output is correct |
16 |
Correct |
124 ms |
14092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9732 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
7 ms |
9844 KB |
Output is correct |
8 |
Correct |
9 ms |
9996 KB |
Output is correct |
9 |
Correct |
8 ms |
9940 KB |
Output is correct |
10 |
Correct |
66 ms |
15596 KB |
Output is correct |
11 |
Correct |
124 ms |
15552 KB |
Output is correct |
12 |
Correct |
188 ms |
17284 KB |
Output is correct |
13 |
Correct |
210 ms |
18124 KB |
Output is correct |
14 |
Correct |
172 ms |
16164 KB |
Output is correct |
15 |
Correct |
127 ms |
13968 KB |
Output is correct |
16 |
Correct |
130 ms |
14016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9732 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
7 ms |
9844 KB |
Output is correct |
8 |
Correct |
9 ms |
9996 KB |
Output is correct |
9 |
Correct |
8 ms |
9940 KB |
Output is correct |
10 |
Correct |
66 ms |
15596 KB |
Output is correct |
11 |
Correct |
124 ms |
15552 KB |
Output is correct |
12 |
Correct |
188 ms |
17284 KB |
Output is correct |
13 |
Correct |
210 ms |
18124 KB |
Output is correct |
14 |
Correct |
172 ms |
16164 KB |
Output is correct |
15 |
Correct |
127 ms |
13968 KB |
Output is correct |
16 |
Correct |
130 ms |
14016 KB |
Output is correct |
17 |
Correct |
125 ms |
15736 KB |
Output is correct |
18 |
Correct |
5 ms |
9684 KB |
Output is correct |
19 |
Correct |
5 ms |
9684 KB |
Output is correct |
20 |
Correct |
6 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9684 KB |
Output is correct |
22 |
Correct |
7 ms |
9640 KB |
Output is correct |
23 |
Correct |
7 ms |
9940 KB |
Output is correct |
24 |
Correct |
8 ms |
9940 KB |
Output is correct |
25 |
Correct |
10 ms |
10000 KB |
Output is correct |
26 |
Correct |
9 ms |
9940 KB |
Output is correct |
27 |
Correct |
63 ms |
15820 KB |
Output is correct |
28 |
Correct |
202 ms |
17448 KB |
Output is correct |
29 |
Correct |
201 ms |
18196 KB |
Output is correct |
30 |
Correct |
173 ms |
16300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
15156 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9648 KB |
Output is correct |
5 |
Correct |
8 ms |
9812 KB |
Output is correct |
6 |
Correct |
7 ms |
9812 KB |
Output is correct |
7 |
Correct |
7 ms |
9812 KB |
Output is correct |
8 |
Correct |
73 ms |
16100 KB |
Output is correct |
9 |
Correct |
52 ms |
15824 KB |
Output is correct |
10 |
Correct |
22 ms |
12172 KB |
Output is correct |
11 |
Correct |
134 ms |
14152 KB |
Output is correct |
12 |
Correct |
6 ms |
9740 KB |
Output is correct |
13 |
Correct |
5 ms |
9700 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
5 ms |
9732 KB |
Output is correct |
16 |
Correct |
5 ms |
9732 KB |
Output is correct |
17 |
Correct |
9 ms |
10068 KB |
Output is correct |
18 |
Correct |
12 ms |
10184 KB |
Output is correct |
19 |
Correct |
68 ms |
15868 KB |
Output is correct |
20 |
Correct |
202 ms |
17540 KB |
Output is correct |
21 |
Correct |
130 ms |
15896 KB |
Output is correct |
22 |
Correct |
87 ms |
15040 KB |
Output is correct |
23 |
Correct |
178 ms |
17696 KB |
Output is correct |
24 |
Correct |
118 ms |
14796 KB |
Output is correct |
25 |
Correct |
127 ms |
14100 KB |
Output is correct |
26 |
Correct |
124 ms |
14092 KB |
Output is correct |
27 |
Correct |
5 ms |
9684 KB |
Output is correct |
28 |
Correct |
5 ms |
9732 KB |
Output is correct |
29 |
Correct |
5 ms |
9684 KB |
Output is correct |
30 |
Correct |
5 ms |
9728 KB |
Output is correct |
31 |
Correct |
5 ms |
9684 KB |
Output is correct |
32 |
Correct |
6 ms |
9812 KB |
Output is correct |
33 |
Correct |
7 ms |
9844 KB |
Output is correct |
34 |
Correct |
9 ms |
9996 KB |
Output is correct |
35 |
Correct |
8 ms |
9940 KB |
Output is correct |
36 |
Correct |
66 ms |
15596 KB |
Output is correct |
37 |
Correct |
124 ms |
15552 KB |
Output is correct |
38 |
Correct |
188 ms |
17284 KB |
Output is correct |
39 |
Correct |
210 ms |
18124 KB |
Output is correct |
40 |
Correct |
172 ms |
16164 KB |
Output is correct |
41 |
Correct |
127 ms |
13968 KB |
Output is correct |
42 |
Correct |
130 ms |
14016 KB |
Output is correct |
43 |
Correct |
125 ms |
15736 KB |
Output is correct |
44 |
Correct |
5 ms |
9684 KB |
Output is correct |
45 |
Correct |
5 ms |
9684 KB |
Output is correct |
46 |
Correct |
6 ms |
9684 KB |
Output is correct |
47 |
Correct |
5 ms |
9684 KB |
Output is correct |
48 |
Correct |
7 ms |
9640 KB |
Output is correct |
49 |
Correct |
7 ms |
9940 KB |
Output is correct |
50 |
Correct |
8 ms |
9940 KB |
Output is correct |
51 |
Correct |
10 ms |
10000 KB |
Output is correct |
52 |
Correct |
9 ms |
9940 KB |
Output is correct |
53 |
Correct |
63 ms |
15820 KB |
Output is correct |
54 |
Correct |
202 ms |
17448 KB |
Output is correct |
55 |
Correct |
201 ms |
18196 KB |
Output is correct |
56 |
Correct |
173 ms |
16300 KB |
Output is correct |
57 |
Correct |
264 ms |
19268 KB |
Output is correct |
58 |
Correct |
63 ms |
16068 KB |
Output is correct |
59 |
Correct |
137 ms |
16080 KB |
Output is correct |