#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXN=5e4+6;
const ll INF=1e15;
const ll logN=18;
const ll MAXK=6;
struct query
{
ll lb,rb,lk,rk;
ll pos;
};
ll ans[2*MAXN],n,m,k;
ll prefL[MAXN][MAXK][MAXK];
ll prefR[MAXN][MAXK][MAXK];
ll matrix1[MAXN][MAXK];
ll matrix2[MAXN][MAXK];
void dq(ll l,ll r,vector<query>q)
{
if(q.size()==0)return;
ll mid=(l+r)/2;
for(ll i1=0;i1<k;i1++)
{
for(ll i2=0;i2<k;i2++)
{
prefR[mid][i1][i2]=INF;
prefL[mid][i1][i2]=INF;
}
}
for(ll i=0;i<k;i++)
{
prefR[mid][i][i]=0;
prefL[mid][i][i]=0;
}
for(ll j=mid+1;j<=r;j++)
{
for(ll k1=0;k1<k;k1++)
{
for(ll k2=0;k2<k;k2++)
{
prefR[j][k1][k2]=INF;
}
}
for(ll k1=0;k1<k;k1++)
{
for(ll k2=0;k2<k;k2++)
{
for(ll k3=0;k3<k;k3++)
{
prefR[j][k1][k2]=min(prefR[j][k1][k2],prefR[j-1][k1][k3]+matrix1[(j-1)*k+k3][k2]);
}
}
}
}
for(ll j=mid-1;j>=l;j--)
{
for(ll k1=0;k1<k;k1++)
{
for(ll k2=0;k2<k;k2++)
{
prefL[j][k1][k2]=INF;
}
}
for(ll k1=0;k1<k;k1++)
{
for(ll k2=0;k2<k;k2++)
{
for(ll k3=0;k3<k;k3++)
{
prefL[j][k1][k2]=min(prefL[j][k1][k2],prefL[j+1][k1][k3]+matrix2[(j+1)*k+k3][k2]);
}
}
}
}
vector<query>for_left,for_right;
for(auto xd:q)
{
if(xd.rb<mid)
{
for_left.push_back(xd);
continue;
}
if(xd.lb>mid)
{
for_right.push_back(xd);
continue;
}
//cout<<mid<<" "<<l<<" "<<r<<endl;
//cout<<xd.pos<<" "<<xd.lb*k+xd.lk<<" "<<xd.rb*k+xd.rk<<" :\n";
ans[xd.pos]=INF;
for(ll k1=0;k1<k;k1++)
{
//cout<<k1<<" "<<prefL[xd.lb][k1][xd.lk]<<" "<<prefR[xd.rb][k1][xd.rk]<<endl;
ans[xd.pos]=min(ans[xd.pos],prefL[xd.lb][k1][xd.lk]+prefR[xd.rb][k1][xd.rk]);
}
}
dq(l,mid-1,for_left);
dq(mid+1,r,for_right);
}
int main()
{
for(ll i=0;i<MAXN;i++)
{
for(ll j1=0;j1<MAXK;j1++)
{
matrix1[i][j1]=INF;
matrix2[i][j1]=INF;
}
}
cin>>k>>n>>m;
ll o;cin>>o;
for(ll i=1;i<=m;i++)
{
ll x,y,z;
cin>>x>>y>>z;
matrix1[x][y%k]=z;
matrix2[y][x%k]=z;
}
vector<query>queries;
for(ll i=0;i<o;i++)
{
ll x,y;
cin>>x>>y;
queries.push_back({x/k,y/k,x%k,y%k,i});
}
dq(0,n/k+1,queries);
for(ll i=0;i<o;i++)
{
if(ans[i]>=INF)cout<<-1<<endl;
else cout<<ans[i]<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
34276 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
3 ms |
5100 KB |
Output is correct |
4 |
Correct |
3 ms |
5100 KB |
Output is correct |
5 |
Correct |
8 ms |
5740 KB |
Output is correct |
6 |
Correct |
7 ms |
5740 KB |
Output is correct |
7 |
Correct |
7 ms |
5740 KB |
Output is correct |
8 |
Correct |
95 ms |
35440 KB |
Output is correct |
9 |
Correct |
95 ms |
35300 KB |
Output is correct |
10 |
Correct |
53 ms |
34580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
17400 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
3 ms |
5100 KB |
Output is correct |
4 |
Correct |
3 ms |
5100 KB |
Output is correct |
5 |
Correct |
3 ms |
5100 KB |
Output is correct |
6 |
Correct |
4 ms |
5100 KB |
Output is correct |
7 |
Correct |
31 ms |
7140 KB |
Output is correct |
8 |
Correct |
34 ms |
7032 KB |
Output is correct |
9 |
Correct |
87 ms |
28784 KB |
Output is correct |
10 |
Correct |
161 ms |
15972 KB |
Output is correct |
11 |
Correct |
132 ms |
18916 KB |
Output is correct |
12 |
Correct |
113 ms |
15076 KB |
Output is correct |
13 |
Correct |
143 ms |
10444 KB |
Output is correct |
14 |
Correct |
92 ms |
11212 KB |
Output is correct |
15 |
Correct |
79 ms |
9312 KB |
Output is correct |
16 |
Correct |
80 ms |
9292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
4972 KB |
Output is correct |
3 |
Correct |
3 ms |
4972 KB |
Output is correct |
4 |
Correct |
3 ms |
4972 KB |
Output is correct |
5 |
Correct |
3 ms |
4972 KB |
Output is correct |
6 |
Correct |
4 ms |
5612 KB |
Output is correct |
7 |
Correct |
5 ms |
5356 KB |
Output is correct |
8 |
Correct |
7 ms |
5100 KB |
Output is correct |
9 |
Correct |
6 ms |
5228 KB |
Output is correct |
10 |
Correct |
63 ms |
32364 KB |
Output is correct |
11 |
Correct |
97 ms |
18284 KB |
Output is correct |
12 |
Correct |
134 ms |
15852 KB |
Output is correct |
13 |
Correct |
148 ms |
15980 KB |
Output is correct |
14 |
Correct |
112 ms |
15724 KB |
Output is correct |
15 |
Correct |
73 ms |
9452 KB |
Output is correct |
16 |
Correct |
74 ms |
9452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5100 KB |
Output is correct |
2 |
Correct |
3 ms |
4972 KB |
Output is correct |
3 |
Correct |
3 ms |
4972 KB |
Output is correct |
4 |
Correct |
3 ms |
4972 KB |
Output is correct |
5 |
Correct |
3 ms |
4972 KB |
Output is correct |
6 |
Correct |
4 ms |
5612 KB |
Output is correct |
7 |
Correct |
5 ms |
5356 KB |
Output is correct |
8 |
Correct |
7 ms |
5100 KB |
Output is correct |
9 |
Correct |
6 ms |
5228 KB |
Output is correct |
10 |
Correct |
63 ms |
32364 KB |
Output is correct |
11 |
Correct |
97 ms |
18284 KB |
Output is correct |
12 |
Correct |
134 ms |
15852 KB |
Output is correct |
13 |
Correct |
148 ms |
15980 KB |
Output is correct |
14 |
Correct |
112 ms |
15724 KB |
Output is correct |
15 |
Correct |
73 ms |
9452 KB |
Output is correct |
16 |
Correct |
74 ms |
9452 KB |
Output is correct |
17 |
Correct |
118 ms |
20940 KB |
Output is correct |
18 |
Correct |
4 ms |
5100 KB |
Output is correct |
19 |
Correct |
3 ms |
5100 KB |
Output is correct |
20 |
Correct |
3 ms |
5100 KB |
Output is correct |
21 |
Correct |
3 ms |
5100 KB |
Output is correct |
22 |
Correct |
3 ms |
5100 KB |
Output is correct |
23 |
Correct |
14 ms |
6092 KB |
Output is correct |
24 |
Correct |
13 ms |
5836 KB |
Output is correct |
25 |
Correct |
15 ms |
5708 KB |
Output is correct |
26 |
Correct |
14 ms |
5708 KB |
Output is correct |
27 |
Correct |
74 ms |
34252 KB |
Output is correct |
28 |
Correct |
144 ms |
16972 KB |
Output is correct |
29 |
Correct |
163 ms |
17356 KB |
Output is correct |
30 |
Correct |
133 ms |
16588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
34276 KB |
Output is correct |
2 |
Correct |
3 ms |
5100 KB |
Output is correct |
3 |
Correct |
3 ms |
5100 KB |
Output is correct |
4 |
Correct |
3 ms |
5100 KB |
Output is correct |
5 |
Correct |
8 ms |
5740 KB |
Output is correct |
6 |
Correct |
7 ms |
5740 KB |
Output is correct |
7 |
Correct |
7 ms |
5740 KB |
Output is correct |
8 |
Correct |
95 ms |
35440 KB |
Output is correct |
9 |
Correct |
95 ms |
35300 KB |
Output is correct |
10 |
Correct |
53 ms |
34580 KB |
Output is correct |
11 |
Correct |
124 ms |
17400 KB |
Output is correct |
12 |
Correct |
4 ms |
5100 KB |
Output is correct |
13 |
Correct |
3 ms |
5100 KB |
Output is correct |
14 |
Correct |
3 ms |
5100 KB |
Output is correct |
15 |
Correct |
3 ms |
5100 KB |
Output is correct |
16 |
Correct |
4 ms |
5100 KB |
Output is correct |
17 |
Correct |
31 ms |
7140 KB |
Output is correct |
18 |
Correct |
34 ms |
7032 KB |
Output is correct |
19 |
Correct |
87 ms |
28784 KB |
Output is correct |
20 |
Correct |
161 ms |
15972 KB |
Output is correct |
21 |
Correct |
132 ms |
18916 KB |
Output is correct |
22 |
Correct |
113 ms |
15076 KB |
Output is correct |
23 |
Correct |
143 ms |
10444 KB |
Output is correct |
24 |
Correct |
92 ms |
11212 KB |
Output is correct |
25 |
Correct |
79 ms |
9312 KB |
Output is correct |
26 |
Correct |
80 ms |
9292 KB |
Output is correct |
27 |
Correct |
3 ms |
5100 KB |
Output is correct |
28 |
Correct |
3 ms |
4972 KB |
Output is correct |
29 |
Correct |
3 ms |
4972 KB |
Output is correct |
30 |
Correct |
3 ms |
4972 KB |
Output is correct |
31 |
Correct |
3 ms |
4972 KB |
Output is correct |
32 |
Correct |
4 ms |
5612 KB |
Output is correct |
33 |
Correct |
5 ms |
5356 KB |
Output is correct |
34 |
Correct |
7 ms |
5100 KB |
Output is correct |
35 |
Correct |
6 ms |
5228 KB |
Output is correct |
36 |
Correct |
63 ms |
32364 KB |
Output is correct |
37 |
Correct |
97 ms |
18284 KB |
Output is correct |
38 |
Correct |
134 ms |
15852 KB |
Output is correct |
39 |
Correct |
148 ms |
15980 KB |
Output is correct |
40 |
Correct |
112 ms |
15724 KB |
Output is correct |
41 |
Correct |
73 ms |
9452 KB |
Output is correct |
42 |
Correct |
74 ms |
9452 KB |
Output is correct |
43 |
Correct |
118 ms |
20940 KB |
Output is correct |
44 |
Correct |
4 ms |
5100 KB |
Output is correct |
45 |
Correct |
3 ms |
5100 KB |
Output is correct |
46 |
Correct |
3 ms |
5100 KB |
Output is correct |
47 |
Correct |
3 ms |
5100 KB |
Output is correct |
48 |
Correct |
3 ms |
5100 KB |
Output is correct |
49 |
Correct |
14 ms |
6092 KB |
Output is correct |
50 |
Correct |
13 ms |
5836 KB |
Output is correct |
51 |
Correct |
15 ms |
5708 KB |
Output is correct |
52 |
Correct |
14 ms |
5708 KB |
Output is correct |
53 |
Correct |
74 ms |
34252 KB |
Output is correct |
54 |
Correct |
144 ms |
16972 KB |
Output is correct |
55 |
Correct |
163 ms |
17356 KB |
Output is correct |
56 |
Correct |
133 ms |
16588 KB |
Output is correct |
57 |
Correct |
221 ms |
16740 KB |
Output is correct |
58 |
Correct |
96 ms |
35300 KB |
Output is correct |
59 |
Correct |
137 ms |
22116 KB |
Output is correct |