#include <bits/stdc++.h>
#define ff first
#define ss second
#define PHB push_back
#define fastio ios::sync_with_stdio(false);cin.tie(nullptr);
#define ln '\n'
#define nwln cout<<ln;
#define v(T) vector<T>
#define PQ(T) priority_queue<T, v(T), greater<T>>
using namespace std;
typedef pair<int, int> pi;
typedef vector<int> vri;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
const int INF = 1001001001;
const int MAXN = 30003;
int N, M;
set<int> B[MAXN];
vri dist;
vvpi adj;
void dijkstra(int src) {
dist.assign(N, INF);
dist[src] = 0;
PQ(pi) pq;
pq.push({0, src});
while(!pq.empty()) {
int v = pq.top().ss, d_v = pq.top().ff;
pq.pop();
if(d_v != dist[v]) continue;
for(pi edge: adj[v]) {
int w = edge.ss, u = edge.ff;
if(dist[v] + w < dist[u]) {
dist[u] = dist[v] + w;
pq.push({dist[u], u});
}
}
}
}
int main()
{
fastio
cin>>N>>M, adj.resize(N);
while(M--) {
int b, p;
cin>>b>>p;
B[b].insert(p);
}
for(int i=0; i<N; ++i) {
for(int p: B[i]) {
int pos = i+p, cost = 1;
while(pos < N) {
adj[i].PHB({pos, cost});
if(B[pos].count(p)) break;
++cost, pos += p;
}
pos = i-p, cost = 1;
while(pos >= 0) {
adj[i].PHB({pos, cost});
if(B[pos].count(p)) break;
++cost, pos -= p;
}
}
}
dijkstra(0);
if(dist[1] == INF) cout<<-1;
else cout<<dist[1];
nwln
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Incorrect |
2 ms |
1620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
1 ms |
1620 KB |
Output is correct |
3 |
Incorrect |
2 ms |
1620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1620 KB |
Output is correct |
2 |
Correct |
2 ms |
1620 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |