//alternativ cu rmq, aceasi complexitate
#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 |
80 ms |
24840 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
6 ms |
2736 KB |
Output is correct |
5 |
Correct |
6 ms |
3168 KB |
Output is correct |
6 |
Correct |
6 ms |
3240 KB |
Output is correct |
7 |
Correct |
5 ms |
3192 KB |
Output is correct |
8 |
Correct |
79 ms |
24736 KB |
Output is correct |
9 |
Correct |
79 ms |
24592 KB |
Output is correct |
10 |
Correct |
34 ms |
22392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
15584 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 |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2756 KB |
Output is correct |
7 |
Correct |
8 ms |
3576 KB |
Output is correct |
8 |
Correct |
9 ms |
3448 KB |
Output is correct |
9 |
Correct |
75 ms |
24712 KB |
Output is correct |
10 |
Correct |
160 ms |
13420 KB |
Output is correct |
11 |
Correct |
108 ms |
15680 KB |
Output is correct |
12 |
Correct |
100 ms |
12204 KB |
Output is correct |
13 |
Correct |
201 ms |
9236 KB |
Output is correct |
14 |
Correct |
110 ms |
9760 KB |
Output is correct |
15 |
Correct |
109 ms |
7696 KB |
Output is correct |
16 |
Correct |
109 ms |
7628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2652 KB |
Output is correct |
5 |
Correct |
5 ms |
2684 KB |
Output is correct |
6 |
Correct |
6 ms |
3172 KB |
Output is correct |
7 |
Correct |
0 ms |
2936 KB |
Output is correct |
8 |
Correct |
13 ms |
2908 KB |
Output is correct |
9 |
Correct |
5 ms |
2936 KB |
Output is correct |
10 |
Correct |
91 ms |
24200 KB |
Output is correct |
11 |
Correct |
126 ms |
15352 KB |
Output is correct |
12 |
Correct |
172 ms |
12984 KB |
Output is correct |
13 |
Correct |
191 ms |
13292 KB |
Output is correct |
14 |
Correct |
164 ms |
12584 KB |
Output is correct |
15 |
Correct |
104 ms |
7536 KB |
Output is correct |
16 |
Correct |
102 ms |
7464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
3 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2652 KB |
Output is correct |
5 |
Correct |
5 ms |
2684 KB |
Output is correct |
6 |
Correct |
6 ms |
3172 KB |
Output is correct |
7 |
Correct |
0 ms |
2936 KB |
Output is correct |
8 |
Correct |
13 ms |
2908 KB |
Output is correct |
9 |
Correct |
5 ms |
2936 KB |
Output is correct |
10 |
Correct |
91 ms |
24200 KB |
Output is correct |
11 |
Correct |
126 ms |
15352 KB |
Output is correct |
12 |
Correct |
172 ms |
12984 KB |
Output is correct |
13 |
Correct |
191 ms |
13292 KB |
Output is correct |
14 |
Correct |
164 ms |
12584 KB |
Output is correct |
15 |
Correct |
104 ms |
7536 KB |
Output is correct |
16 |
Correct |
102 ms |
7464 KB |
Output is correct |
17 |
Correct |
120 ms |
15452 KB |
Output is correct |
18 |
Correct |
4 ms |
2724 KB |
Output is correct |
19 |
Correct |
4 ms |
2680 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2724 KB |
Output is correct |
22 |
Correct |
4 ms |
2684 KB |
Output is correct |
23 |
Correct |
6 ms |
3192 KB |
Output is correct |
24 |
Correct |
7 ms |
3036 KB |
Output is correct |
25 |
Correct |
9 ms |
3040 KB |
Output is correct |
26 |
Correct |
8 ms |
3064 KB |
Output is correct |
27 |
Correct |
75 ms |
24436 KB |
Output is correct |
28 |
Correct |
159 ms |
13048 KB |
Output is correct |
29 |
Correct |
220 ms |
13268 KB |
Output is correct |
30 |
Correct |
145 ms |
12580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
24840 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
6 ms |
2736 KB |
Output is correct |
5 |
Correct |
6 ms |
3168 KB |
Output is correct |
6 |
Correct |
6 ms |
3240 KB |
Output is correct |
7 |
Correct |
5 ms |
3192 KB |
Output is correct |
8 |
Correct |
79 ms |
24736 KB |
Output is correct |
9 |
Correct |
79 ms |
24592 KB |
Output is correct |
10 |
Correct |
34 ms |
22392 KB |
Output is correct |
11 |
Correct |
109 ms |
15584 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 |
2680 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2756 KB |
Output is correct |
17 |
Correct |
8 ms |
3576 KB |
Output is correct |
18 |
Correct |
9 ms |
3448 KB |
Output is correct |
19 |
Correct |
75 ms |
24712 KB |
Output is correct |
20 |
Correct |
160 ms |
13420 KB |
Output is correct |
21 |
Correct |
108 ms |
15680 KB |
Output is correct |
22 |
Correct |
100 ms |
12204 KB |
Output is correct |
23 |
Correct |
201 ms |
9236 KB |
Output is correct |
24 |
Correct |
110 ms |
9760 KB |
Output is correct |
25 |
Correct |
109 ms |
7696 KB |
Output is correct |
26 |
Correct |
109 ms |
7628 KB |
Output is correct |
27 |
Correct |
1 ms |
2680 KB |
Output is correct |
28 |
Correct |
4 ms |
2680 KB |
Output is correct |
29 |
Correct |
3 ms |
2680 KB |
Output is correct |
30 |
Correct |
5 ms |
2652 KB |
Output is correct |
31 |
Correct |
5 ms |
2684 KB |
Output is correct |
32 |
Correct |
6 ms |
3172 KB |
Output is correct |
33 |
Correct |
0 ms |
2936 KB |
Output is correct |
34 |
Correct |
13 ms |
2908 KB |
Output is correct |
35 |
Correct |
5 ms |
2936 KB |
Output is correct |
36 |
Correct |
91 ms |
24200 KB |
Output is correct |
37 |
Correct |
126 ms |
15352 KB |
Output is correct |
38 |
Correct |
172 ms |
12984 KB |
Output is correct |
39 |
Correct |
191 ms |
13292 KB |
Output is correct |
40 |
Correct |
164 ms |
12584 KB |
Output is correct |
41 |
Correct |
104 ms |
7536 KB |
Output is correct |
42 |
Correct |
102 ms |
7464 KB |
Output is correct |
43 |
Correct |
120 ms |
15452 KB |
Output is correct |
44 |
Correct |
4 ms |
2724 KB |
Output is correct |
45 |
Correct |
4 ms |
2680 KB |
Output is correct |
46 |
Correct |
4 ms |
2680 KB |
Output is correct |
47 |
Correct |
4 ms |
2724 KB |
Output is correct |
48 |
Correct |
4 ms |
2684 KB |
Output is correct |
49 |
Correct |
6 ms |
3192 KB |
Output is correct |
50 |
Correct |
7 ms |
3036 KB |
Output is correct |
51 |
Correct |
9 ms |
3040 KB |
Output is correct |
52 |
Correct |
8 ms |
3064 KB |
Output is correct |
53 |
Correct |
75 ms |
24436 KB |
Output is correct |
54 |
Correct |
159 ms |
13048 KB |
Output is correct |
55 |
Correct |
220 ms |
13268 KB |
Output is correct |
56 |
Correct |
145 ms |
12580 KB |
Output is correct |
57 |
Correct |
259 ms |
11876 KB |
Output is correct |
58 |
Correct |
83 ms |
24720 KB |
Output is correct |
59 |
Correct |
121 ms |
15844 KB |
Output is correct |