#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;
bool ac[MAXN];
vector<int> p[MAXN];
set<int> a;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
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 = b, t[b] = 0;
if(i == 1) e = b;
p[b].push_back(pi);
}
FOR(i, 0, n - 1) if(i != s) ac[i] = true;
int c = s;
pair<int, int> nx = make_pair(-1, -1);
while(c != -1){
FOR(i, 0, m - 1) if(ac[i]) {
for(int j : p[c]){
if(abs(i - c) % j == 0){
t[i] = (t[i] == -1) ? (t[c] + (int)abs(i - c) / j) : min(t[i], t[c] + (int)abs(i - c) / j);
}
}
if(t[i] != -1 && (nx.first == -1 || (nx.first > t[i]))){
nx.first = t[i];
nx.second = i;
}
}
c = nx.second;
ac[c] = false;
nx.first = nx.second = -1;
}
cout << t[e] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1112 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
1116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |