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 FOR(i, a, b) for(int i = a; i <= b; i++)
#define MAXN 30005
int n, m, t[MAXN], s, e;
pair<int, int> d[MAXN];
vector<int> p[MAXN];
priority_queue<pair<int, int>> pq;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
FOR(i, 0, m - 1) t[i] = -1;
FOR(i, 0, m - 1){
cin >> d[i].first >> d[i].second;
if(i == 0) {
s = d[i].first;
t[0] = 0;
pq.push({-t[0], 0});
}
if(i == 1) e = d[i].first;
}
while(pq.size()){
int v = pq.top().first;
int pqi = pq.top().second; pq.pop();
if(t[pqi] != -v) continue;
// cout << pqi << " " << -v << endl;
FOR(i, 0, m - 1) if(i != pqi){
if(abs(d[pqi].first - d[i].first) % d[pqi].second == 0 && (t[i] == -1 || t[pqi] + abs(d[pqi].first - d[i].first) / d[pqi].second < t[i])){
t[i] = t[pqi] + abs(d[pqi].first - d[i].first) / d[pqi].second;
pq.push({-t[i], i});
// cout << " => " << i << " " << t[i] << " " << d[pqi].second << endl;
}
}
}
cout << t[1] << endl;
}
# | 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... |