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 pb push_back
#define fi first
#define se second
#define ll long long
const int MAX=5e4+10;
const int MAXV=1e9+10;///////////////
int k, n, m, o, a, b, w, wyn[MAX], odl[MAX];
pair<int,int>zap[MAX];
vector<pair<int,int>>g[MAX], g2[MAX];
vector<int> nrzap[MAX];
bool odw[MAX];
priority_queue<pair<int,int>>kol;
void dijkstra(int p){
while(kol.size()){
auto v=kol.top();
kol.pop();
v.fi*=-1;
if(odw[v.se]) continue;
odw[v.se]=true;
for(auto s: g[v.se])
if(s.fi/k<=p && odl[s.fi]>v.fi+s.se)
odl[s.fi]=v.fi+s.se, kol.push({-odl[s.fi], s.fi});
}
}
void dijkstra2(int l){
while(kol.size()){
auto v=kol.top();
kol.pop();
v.fi*=-1;
if(odw[v.se]) continue;
odw[v.se]=true;
for(auto s: g2[v.se])
if(s.fi/k>=l && odl[s.fi]>v.fi+s.se)
odl[s.fi]=v.fi+s.se, kol.push({-odl[s.fi], s.fi});
}
}
void rek(int a, int b){
if(a==b) return;
int sr=(a+b)/2;
for(int j=k*sr; j<k*(sr+1); j++){
for(int i=k*sr; i<k*(b+1); i++)
odw[i]=false, odl[i]=MAXV;
kol.push({0, j}), odl[j]=0;
dijkstra(b);
for(int i=k*(sr+1)-1; i>=(k*a); i--)
odw[i]=false, odl[i]=MAXV;
kol.push({0, j}), odl[j]=0;
dijkstra2(a);
/* if(sr==1){
cout<<j<<": "<<"\n";
for(int i=k*sr; i<k*(b+1); i++)
cout<<i<<": "<<odl[i]<<"\n";
for(int i=k*sr; i>=(k*a); i--)
cout<<i<<": "<<odl[i]<<"\n";;
cout<<"\n";
}
*/ for(int i=k*(sr+1)-1; i>=k*a; i--){
for(auto x:nrzap[i]){
if(zap[x].se>=k*sr && zap[x].se<k*(b+1)){
// cout<<x<<" "<<zap[x].se<<" "<<i<<"x "<<odl[i]<<" "<<odl[zap[x].se]<<"\n";
wyn[x]=min(wyn[x], odl[i]+odl[zap[x].se]);
}
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin>>k>>n>>m>>o;
for(int i=1; i<=m; i++){
cin>>a>>b>>w;
g[a].pb({b, w}), g2[b].pb({a, w});
}
for(int i=1; i<=o; i++){
cin>>zap[i].fi>>zap[i].se;
wyn[i]=MAXV;
nrzap[zap[i].fi].pb(i);
}
rek(0, n/k);
for(int i=1; i<=o; i++){
if(wyn[i]==MAXV)
cout<<-1<<"\n";
else
cout<<wyn[i]<<"\n";
}
}
# | 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... |