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 int long long
priority_queue<pair<int,int>> q;
vector<int> buildings[30001];
int power[30001];
int distn[30001];
bool visited[30001];
const int INF = 1e15;
int n;
inline void adddoge(int x,int building,int dist){
int minimum = building-power[x]*(building/power[x]);
for(int i = minimum;i<n;i+=power[x])if(distn[i]>((abs(building-i)/power[x])-dist)){
q.emplace(dist-(abs(building-i)/power[x]),i);
distn[i] = (abs(building-i)/power[x])-dist;
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
for(int&i:distn)i=INF;
int m;
cin >> n >> m;
int base,tar;
cin>>base>>power[0]>>tar>>power[1];
buildings[base].emplace_back(0);
buildings[tar].emplace_back(1);
for(int i=2;i<m;i++){
int b;cin>>b>>power[i];
buildings[b].emplace_back(i);
}
q.emplace(0,base);
while(!q.empty()){
auto curr = q.top();q.pop();
if(visited[curr.second])continue;
visited[curr.second]=true;
distn[curr.second] = -curr.first;
for(int&i:buildings[curr.second])adddoge(i,curr.second,curr.first);
}
cout << (distn[tar]>=INF ? -1 : distn[tar]) << '\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... |