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>
using namespace std ;
#define i64 int64_t // typecast using i64(x)
#define int int64_t
#define ld long double
#define f(i,a,b) for(int i=a; i<b; ++i)
#define endl '\n'
#define PQ priority_queue
#define LB lower_bound
#define UB upper_bound
#define fr first
#define sc second
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define sz(x) ((int)(x).size())
//#ifndef ONLINE_JUDGE
template<typename T>
void __p(T a) {
cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
cout<<"{";
__p(a.first);
cout<<",";
__p(a.second);
cout<<"}\n";
}
template<typename T>
void __p(std::vector<T> a) {
cout<<"{";
for(auto it=a.begin(); it<a.end(); it++)
__p(*it),cout<<",}\n"[it+1==a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
__p(a1);
__p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";
__p(arg1);
cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(;; i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<" | ";
__f(comma+1,args...);
}
//#endif
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin.exceptions(cin.failbit);
if(sz(s)){
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
}
const int INF = 1e12 ;
signed main(){
setIO() ;
int K, N, M, O; cin >> K >> N >> M >> O ;
int LOG = log2(N) + 1 ;
int dis[N][K][LOG] ;
f(i,0,N) f(j,0,K) f(k,0,LOG) dis[i][j][k] = INF ;
f(_,0,M){
int a, b, t; cin >> a >> b >> t ;
dis[a][b%K][0] = t ;
}
f(k,1,LOG){
f(i,0,N){
f(j,0,K){
f(u,0,K){
int mid = K * ( i/K + (1 << (k-1)) ) + u ;
if(mid >= N) continue ;
dis[i][j][k] = min(dis[i][j][k], dis[i][u][k-1] + dis[mid][j][k-1]) ;
}
}
}
}
while(O--){
int a, b; cin >> a >> b ; assert(a != b) ;
int d = b/K - a/K ;
if(d <= 0){ cout << "-1" << endl ; continue ;}
d--;
vector<int> cur(K), D(K) ;
f(i,0,K){
cur[i] = K * (a/K + 1) + i ;
D[i] = dis[a][i][0] ;
}
for(int k = LOG-1; k >= 0 ; --k){
if( (1 << k) <= d){
vector<int> newD(K,INF) ;
f(i,0,K){
f(j,0,K){
if(cur[i] >= N) continue ;
newD[j] = min(newD[j], dis[cur[i]][j][k] + D[i] ) ;
}
}
int Q = cur[0]/K ;
f(i,0,K) cur[i] = K * ( Q + (1 << k) ) + i ;
D = newD ;
d -= (1 << k) ;
}
}
if( D[b%K] >= INF){
cout << "-1" << endl ; continue ;
}
cout << D[b%K] << endl ;
}
}
Compilation message (stderr)
toll.cpp: In function 'void setIO(std::string)':
toll.cpp:73:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
73 | freopen((s+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
toll.cpp:74:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
74 | freopen((s+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |