This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define left (h<<1),l,(l + r)/2
#define right ((h<<1)|1),(l + r)/2 + 1,r
#define int ll
using namespace std;
const int N = 50000 + 3,inf = 1e18;
int d[N][25][7][7];
int dp[7][7],dpold[7][7];
signed main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int k,n,m,q;
cin>>k>>n>>m>>q;
for (int i = 0; i <= n/k; i++)
for (int D=0;D<=20;D++)
for (int j = 0; j < k; j++)
for (int t=0;t<k;t++)
d[i][D][j][t] = inf;
for (int i = 1; i <= m; i++){
int a,b,c;
cin>>a>>b>>c;
d[a/k][0][a%k][b%k] = c;
}
for (int j = 1; j <= 20; j++){
for (int i = 0; i <= n/k; i++){
if (i + (1LL<<(j - 1)) > (n/k)) continue;
for (int x = 0; x < k; x++)
for (int y = 0; y < k;y++)
for (int z=0;z<k;z++)
d[i][j][x][y] = min(d[i][j][x][y],d[i][j - 1][x][z] + d[i + (1LL<<(j-1))][j - 1][z][y]);
}
}
while (q--){
int x,y;
cin>>x>>y;
if (x/k >= y/k){
cout<<-1<<endl;
continue;
}
for (int i = 0; i < k; i++)
for (int j = 0; j < k; j++)
dp[i][j] = dpold[i][j] = inf;
dpold[x%k][x%k] = 0;
int len = y/k - x/k,cur = x/k;
for (int i = 20; i >= 0; i--){
if (len >= (1LL<<i)) {
len -= (1LL<<i);
for (int y=0;y<k;y++)
for (int z=0;z<k;z++)
dp[x%k][y] = min(dp[x%k][y],dpold[x%k][z] + d[cur][i][z][y]);
for (int y=0;y<k;y++)
for (int z=0;z<k;z++)
dpold[y][z] = dp[y][z],dp[y][z] = inf;
cur += (1LL<<i);
}
}
if (dpold[x%k][y%k] == inf) cout<<-1<<endl;
else cout<<dpold[x%k][y%k]<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |