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 <iostream>
#include <vector>
#include <set>
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;
vector<int> p[MAXN];
set<int> a;
int main(){
cin >> n >> m;
FOR(i, 0, n - 1) t[i] = -1;
FOR(i, 0, m - 1){
int b, pi; cin >> b >> pi;
if(i == 0) s = i, t[b] = 0; else if(b != s) a.insert(b);
if(i == 1) e = i;
p[b].push_back(pi);
}
int c = s;
pair<int, int> nx = make_pair(-1, -1);
while(c != -1){
for(int i : a){
for(int j : p[c]){
if(abs(i - c) % j == 0){
t[i] = (t[i] == -1) ? (t[c] + abs(i - c) / j) : min(t[i], t[c] + abs(i - c) / j);
}
}
if(nx.first == -1 || (nx.first > t[i])){
nx.first = t[i];
nx.second = i;
}
}
a.erase(c);
c = nx.second;
nx.first = nx.second = -1;
}
cout << t[e] << 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... |