#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n , m;
cin >> n >> m;
vector<vector<pair<int,int>>> adj(n);
for(int i = 0 ; i < m ; i++){
int a , b;
cin >> a >> b;
for(int j = a + b , cnt = 1 ; j < n ; j += b , cnt++){
adj[a].emplace_back(j , cnt);
}
for(int j = a - b , cnt = 1 ; j >= 0 ; j -= b , cnt++){
adj[a].emplace_back(j , cnt);
}
}
priority_queue<pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>>> pq;
pq.emplace(0 , 0);
vector<int> dist(n + 1 , (int)1e9);
dist[0] = 0;
while(!pq.empty()){
auto f = pq.top();
pq.pop();
if(f.first != dist[f.second]) continue;
for(auto x : adj[f.second]){
int node = x.first;
int cost = x.second;
if(dist[node] > f.first + cost){
dist[node] = f.first + cost;
pq.emplace(dist[node] , node);
}
}
}
if(dist[1] == (int)1e9) cout << -1 << '\n';
else cout << dist[1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
276 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |