#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 + 1);
const int N = 30000 + 10;
bitset<N> vis[N];
int s = -1 , d = -1;
for(int i = 0 ; i < m ; i++){
int a , b;
cin >> a >> b;
if(s == -1) s = a;
else if(d == -1) d = a;
if(vis[a][b]) continue;
vis[a][b] = true;
for(int j = a + b , cnt = 1 ; j < n ; j += b , cnt++){
adj[a].emplace_back(j , cnt);
if(vis[j][b]) break;
vis[j][b] = true;
}
for(int j = a - b , cnt = 1 ; j >= 0 ; j -= b , cnt++){
adj[a].emplace_back(j , cnt);
if(vis[j][b]) break;
}
}
priority_queue<pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>>> pq;
pq.emplace(0 , s);
vector<int> dist(n + 1 , (int)1e9);
dist[s] = 0;
while(!pq.empty()){
auto f = pq.top();
pq.pop();
if(f.second == d) break;
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[d] == (int)1e9) cout << -1 << '\n';
else cout << dist[d] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
58 ms |
110388 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
58 ms |
110404 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
58 ms |
110392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
62 ms |
110336 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
110464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |