#include<iostream>
#include<vector>
using namespace std;
vector<pair<int,int> > A[50005],Qu[50005];
int k,DpSt[50005][7][7],DpDr[50005][7][7],Rez[10005];
pair<int,int> Q[10005];
void solve(int st, int dr)
{
if(st==dr)
{
for(auto qu:Qu[st])
{
if(qu.first!=st)
continue;
if(Q[qu.second].first==Q[qu.second].second)
Rez[qu.second]=0;
}
return;
}
int mij=(st+dr)/2;
for(int i=st; i<=mij; i++)
for(int j=0; j<k; j++)
for(int ind=0; ind<k; ind++)
DpSt[i][j][ind]=1000000000;
for(int i=mij; i<=dr; i++)
for(int j=0; j<k; j++)
for(int ind=0; ind<k; ind++)
DpDr[i][j][ind]=1000000000;
for(int i=0; i<k; i++)
DpSt[mij][i][i]=DpDr[mij][i][i]=0;
for(int i=mij; i<dr; i++)
{
for(int j=0; j<k; j++) //la mij am fost in j
{
for(int ind=0; ind<k; ind++) //la i am fost in ind
{
for(auto other:A[i*k+ind])
DpDr[i+1][j][other.first%k]=min(DpDr[i+1][j][other.first%k],DpDr[i][j][ind]+other.second);
}
}
}
for(int i=mij-1; i>=st; i--)
{
for(int j=0; j<k; j++)
{
for(int ind=0; ind<k; ind++)
{
for(auto other:A[i*k+ind])
DpSt[i][j][ind]=min(DpSt[i][j][ind],DpSt[i+1][j][other.first%k]+other.second);
}
}
}
for(int i=st; i<=mij; i++)
{
for(auto qu:Qu[i])
{
if(qu.first<=mij || qu.first>dr)
continue;
for(int j=0; j<k; j++)
Rez[qu.second]=min(Rez[qu.second],DpSt[i][j][Q[qu.second].first%k]+DpDr[qu.first][j][Q[qu.second].second%k]);
}
}
solve(st,mij);
solve(mij+1,dr);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,q,m;
cin>>k>>n>>m>>q;
for(int i=1; i<=m; i++)
{
int x,y,t;
cin>>x>>y>>t;
A[x].push_back({y,t});
}
for(int i=1; i<=q; i++)
{
int x,y;
cin>>x>>y;
Q[i]={x,y};
Qu[x/k].push_back({y/k,i});
Rez[i]=1000000000;
}
solve(0,n/k);
for(int i=1; i<=q; i++)
{
if(Rez[i]==1000000000)
cout<<"-1\n";
else
cout<<Rez[i]<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
24952 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
3192 KB |
Output is correct |
6 |
Correct |
2 ms |
3188 KB |
Output is correct |
7 |
Correct |
6 ms |
3192 KB |
Output is correct |
8 |
Correct |
80 ms |
24824 KB |
Output is correct |
9 |
Correct |
80 ms |
24732 KB |
Output is correct |
10 |
Correct |
34 ms |
22392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
16000 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2736 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
8 ms |
3576 KB |
Output is correct |
8 |
Correct |
10 ms |
3452 KB |
Output is correct |
9 |
Correct |
96 ms |
24696 KB |
Output is correct |
10 |
Correct |
162 ms |
14328 KB |
Output is correct |
11 |
Correct |
115 ms |
16076 KB |
Output is correct |
12 |
Correct |
111 ms |
12252 KB |
Output is correct |
13 |
Correct |
201 ms |
10252 KB |
Output is correct |
14 |
Correct |
99 ms |
9848 KB |
Output is correct |
15 |
Correct |
106 ms |
7672 KB |
Output is correct |
16 |
Correct |
104 ms |
7672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
5 ms |
3064 KB |
Output is correct |
7 |
Correct |
6 ms |
3064 KB |
Output is correct |
8 |
Correct |
8 ms |
2936 KB |
Output is correct |
9 |
Correct |
7 ms |
2936 KB |
Output is correct |
10 |
Correct |
73 ms |
24184 KB |
Output is correct |
11 |
Correct |
104 ms |
15384 KB |
Output is correct |
12 |
Correct |
157 ms |
13688 KB |
Output is correct |
13 |
Correct |
178 ms |
14240 KB |
Output is correct |
14 |
Correct |
138 ms |
12912 KB |
Output is correct |
15 |
Correct |
111 ms |
7624 KB |
Output is correct |
16 |
Correct |
100 ms |
7544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
5 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2680 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
5 ms |
3064 KB |
Output is correct |
7 |
Correct |
6 ms |
3064 KB |
Output is correct |
8 |
Correct |
8 ms |
2936 KB |
Output is correct |
9 |
Correct |
7 ms |
2936 KB |
Output is correct |
10 |
Correct |
73 ms |
24184 KB |
Output is correct |
11 |
Correct |
104 ms |
15384 KB |
Output is correct |
12 |
Correct |
157 ms |
13688 KB |
Output is correct |
13 |
Correct |
178 ms |
14240 KB |
Output is correct |
14 |
Correct |
138 ms |
12912 KB |
Output is correct |
15 |
Correct |
111 ms |
7624 KB |
Output is correct |
16 |
Correct |
100 ms |
7544 KB |
Output is correct |
17 |
Correct |
112 ms |
15588 KB |
Output is correct |
18 |
Correct |
4 ms |
2680 KB |
Output is correct |
19 |
Correct |
5 ms |
2680 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2680 KB |
Output is correct |
23 |
Correct |
9 ms |
3320 KB |
Output is correct |
24 |
Correct |
7 ms |
3168 KB |
Output is correct |
25 |
Correct |
9 ms |
3064 KB |
Output is correct |
26 |
Correct |
8 ms |
3064 KB |
Output is correct |
27 |
Correct |
73 ms |
24440 KB |
Output is correct |
28 |
Correct |
186 ms |
14068 KB |
Output is correct |
29 |
Correct |
183 ms |
14360 KB |
Output is correct |
30 |
Correct |
137 ms |
13048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
24952 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
3192 KB |
Output is correct |
6 |
Correct |
2 ms |
3188 KB |
Output is correct |
7 |
Correct |
6 ms |
3192 KB |
Output is correct |
8 |
Correct |
80 ms |
24824 KB |
Output is correct |
9 |
Correct |
80 ms |
24732 KB |
Output is correct |
10 |
Correct |
34 ms |
22392 KB |
Output is correct |
11 |
Correct |
108 ms |
16000 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2736 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
8 ms |
3576 KB |
Output is correct |
18 |
Correct |
10 ms |
3452 KB |
Output is correct |
19 |
Correct |
96 ms |
24696 KB |
Output is correct |
20 |
Correct |
162 ms |
14328 KB |
Output is correct |
21 |
Correct |
115 ms |
16076 KB |
Output is correct |
22 |
Correct |
111 ms |
12252 KB |
Output is correct |
23 |
Correct |
201 ms |
10252 KB |
Output is correct |
24 |
Correct |
99 ms |
9848 KB |
Output is correct |
25 |
Correct |
106 ms |
7672 KB |
Output is correct |
26 |
Correct |
104 ms |
7672 KB |
Output is correct |
27 |
Correct |
4 ms |
2680 KB |
Output is correct |
28 |
Correct |
5 ms |
2680 KB |
Output is correct |
29 |
Correct |
3 ms |
2680 KB |
Output is correct |
30 |
Correct |
5 ms |
2680 KB |
Output is correct |
31 |
Correct |
4 ms |
2680 KB |
Output is correct |
32 |
Correct |
5 ms |
3064 KB |
Output is correct |
33 |
Correct |
6 ms |
3064 KB |
Output is correct |
34 |
Correct |
8 ms |
2936 KB |
Output is correct |
35 |
Correct |
7 ms |
2936 KB |
Output is correct |
36 |
Correct |
73 ms |
24184 KB |
Output is correct |
37 |
Correct |
104 ms |
15384 KB |
Output is correct |
38 |
Correct |
157 ms |
13688 KB |
Output is correct |
39 |
Correct |
178 ms |
14240 KB |
Output is correct |
40 |
Correct |
138 ms |
12912 KB |
Output is correct |
41 |
Correct |
111 ms |
7624 KB |
Output is correct |
42 |
Correct |
100 ms |
7544 KB |
Output is correct |
43 |
Correct |
112 ms |
15588 KB |
Output is correct |
44 |
Correct |
4 ms |
2680 KB |
Output is correct |
45 |
Correct |
5 ms |
2680 KB |
Output is correct |
46 |
Correct |
4 ms |
2680 KB |
Output is correct |
47 |
Correct |
4 ms |
2680 KB |
Output is correct |
48 |
Correct |
4 ms |
2680 KB |
Output is correct |
49 |
Correct |
9 ms |
3320 KB |
Output is correct |
50 |
Correct |
7 ms |
3168 KB |
Output is correct |
51 |
Correct |
9 ms |
3064 KB |
Output is correct |
52 |
Correct |
8 ms |
3064 KB |
Output is correct |
53 |
Correct |
73 ms |
24440 KB |
Output is correct |
54 |
Correct |
186 ms |
14068 KB |
Output is correct |
55 |
Correct |
183 ms |
14360 KB |
Output is correct |
56 |
Correct |
137 ms |
13048 KB |
Output is correct |
57 |
Correct |
270 ms |
13828 KB |
Output is correct |
58 |
Correct |
80 ms |
24712 KB |
Output is correct |
59 |
Correct |
115 ms |
16092 KB |
Output is correct |