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>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
typedef pair<ll,ll> pi;
#define ff first
#define ss second
#define fip(a,b) for(ll i = a ; i < b ; i++)
#define fjp(a,b) for(ll j = a ; j < b ; j++)
#define fp(x,a,b) for(ll x = a ; x < b ; x++)
#define fin(a,b) for(ll i = a : i >= b; i--)
#define fjn(a,b) for(ll j = a : j >= b; j--)
#define fn(x,a,b) for(ll x = a ; x >= b ; x--);
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define total_one(a) __builtin_popcount(a)
#define front_zero(a) __builtin_clzll(a)
#define back_zero(a) __builtin_ctzll(a)
#define fx(a) for(auto x : a)
#define nli "\n"
void _printn(ll a){ cerr<<a<<" ";}
void _printn(int a){ cerr<<a<<" ";}
template <class T,class V> void _printn(pair<T,V> a){ cerr<<"( "<<a.ff<<" , "<<a.ss<<" )";}
template <class T> void _printn(vector<T> a){
cerr<<"[ ";
for(auto x : a){
_printn(x);
cerr<< " ";
}
cerr<<"] ";
return;
}
#ifndef ONLINE_JUDGE
#define debug(x) cerr<< #x <<" ";_printn(x); cerr << nli;
#else
#define debug(x)
#endif
const ll mx = 1e5+5;
ll n,m,k,q,tp,tp2,res,sum,tptp,cnt,ans;
bool f = false;
ll sr,de;
vector<pi> adj[mx][20];
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("input1.txt","r",stdin);
// freopen("output1.txt","w",stdout);
// freopen("error1.txt","w",stderr);
// #endif
cin>>k>>n>>m>>tptp;
vector<pi> ve;
fip(0,m){
cin>>sr>>de>>tp;
adj[sr][0].push_back({de,tp});
}
fjp(1,20){
fip(0,n){
sr = i;
if(i/k + (1<<j) > (n-1)/k)
break;
ve.clear();
fx(adj[sr][j-1]){
for(auto y : adj[x.ff][j-1]){
ve.push_back({y.ff,x.ss+y.ss});
}
}
sort(ve.begin(),ve.end());
fip(0,(int)ve.size()){
if(i == 0 || ve[i].ff != ve[i-1].ff)
adj[sr][j].push_back({ve[i].ff,ve[i].ss});
}
}
}
ll l,r;
vector<pi> v,v2;
while(tptp--){
cin>>sr>>de;
if(sr==de){
cout<<0<<nli;
continue;
}
v.clear();
l = sr;
r = de;
ans = -1;
v2.clear();
v.push_back({0,l});
while(1){
if(l/k - r/k > 0)
break;
res = (r/k) - (l/k);
res = 63 - front_zero(res);
v2.clear();
fx(v){
for( auto y : adj[x.ss][res]){
v2.push_back({y.ss+x.ff,y.ff});
}
}
v.clear();
fx(v2)
v.push_back(x);
if((int)v.size()==0){
ans = -1;
break;
}
f = 0;
fx(v){
if(x.ss == de){
ans = x.ff;
f = 1;
break;
}
}
if(f)
break;
l = v[0].ss;
}
cout<<ans<<nli;
}
cerr << "time elapsed : " <<setprecision(6)<< clock() * 1000.00 / CLOCKS_PER_SEC << " ms"<<nli;
return 0;
}
# | 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... |