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 <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cmath>
#include <stack>
#include <set>
#include <functional>
#include <bitset>
#include <map>
#include <unordered_map>
#include <queue>
#include <array>
#include <numeric>
#include <complex>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename A> ostream& operator<<(ostream& os, vector<A>& a) { for(auto &c:a)os<<c<<' '; return os;}
template<typename A> istream& operator>>(istream& os, vector<A>& a) { for(auto &c:a)os>>c; return os;}
template<typename A,size_t N> istream& operator>>(istream &os, array<A,N>&a) { for(auto &c:a)os>>c; return os;}
template<typename A,typename B> istream& operator>>(istream& os, pair<A,B>&a) { os>>a.first>>a.second; return os;}
template<typename A> istream& operator >>(istream& os,complex<A>& a){pair<A,A>aux;os>>aux;a=complex<A>(aux.first,aux.second);return os;}
#define bug(a) cerr << "(" << #a << ": " << a << ")\n";
#define all(x) x.begin(),x.end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define PQ priority_queue
#define x real
#define y imag
using pii= pair<int,int>;
using VI= vector<int>;
using v64= vector<int64_t>;
using point=complex<double>;
using i64= int64_t;
using i16= int16_t;
using u64= uint64_t;
using u32= uint32_t;
using i32= int32_t;
using u16= uint16_t;
const i32 inf=1e9;
const i64 INF=1e18;
const int mod=1e9+7;
const int sigma=26;
///sparse table dp[(int)log2(saritura)][poz/k][x][y]
#define vvii vector<vector<int>>
void solve()
{
int k,n,m,q;
cin>>k>>n>>m>>q;
int mx_jump=(n-1)/k;
//position of last element
vector<vector<vector<vector<int>>>>dp((int)log2(mx_jump)+1,vector<vector<vector<int>>>(mx_jump+1,vector<vector<int>>(k,vector<int>(k,inf))));
for(int i=0;i<m;i++)
{
int x,y,z;
cin>>x>>y>>z;
dp[0][x/k][x%k][y%k]=z;
}
auto combine=[&](const vvii& x,const vvii& y)
{
vvii sol(k,vector<int>(k,inf));
for(int i=0;i<k;i++)
{
for(int j=0;j<k;j++)
{
for(int m=0;m<k;m++)
{
sol[i][j]=min(sol[i][j],x[i][m]+y[m][j]);
}
}
}
return sol;
};
for(int i=1;(1<<i)-1<=mx_jump;i++)
{
for(int j=0;j+(1<<i)-1<=mx_jump;j++)
{
dp[i][j]=combine(dp[i-1][j],dp[i-1][j+(1<<(i-1))]);
}
}
while(q--)
{
int x,y;
cin>>x>>y;
vector<int> sol(5,inf);
sol[x%k]=0;
int caut=y%k;
x/=k;
y/=k;
if(x!=y)
{
for(int i=(int)log2(y-x);i>=0;i--)
{
if(x+(1<<i)<=y)
{
auto old=sol;
fill(all(sol),inf);
for(int a=0;a<k;a++)
{
for(int b=0;b<k;b++)
{
sol[b]=min(sol[b],old[a]+dp[i][x][a][b]);
}
}
x+=(1<<i);
}
}
}
if(sol[caut]==inf)
sol[caut]=-1;
cout<<sol[caut]<<'\n';
}
}
main()
{
i32 tt=1;
ios::sync_with_stdio(false);
cin.tie(0);
//cin>>tt;
while(tt--)
{
solve();
}
}
Compilation message (stderr)
toll.cpp:118:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
118 | main()
| ^~~~
# | 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... |