#include <bits/stdc++.h>
using namespace std;
#define MAXN 30005
#define pb push_back
#define mp make_pair
typedef pair<int, int> pii;
int N, M;
int D[MAXN];
set <int> A[200][200];
vector <int> doge[MAXN];
int main()
{
for (scanf("%d%d", &N, &M);M--;){
int b, p;
scanf("%d%d", &b, &p); doge[b].pb(p);
}
for (int i=1;i<N;i++) D[i] = 2e9;
priority_queue <pii> que;
que.push(mp(0, 0));
for (int i=0;i<N;i++) for (int j=1;j<200;j++) A[j][i%j].insert(i);
while (!que.empty()){
int q = que.top().second, d = -que.top().first; que.pop();
if (D[q] != d) continue;
for (int i=1;i<200;i++) A[i][q%i].erase(q);
for (int p: doge[q]){
if (p > 199){
for (int i=q-p;i>=0;i-=p)
if (D[i] > D[q]+(q-i)/p)
D[i] = D[q]+(q-i)/p, que.push(mp(-D[i], i));
}else{
int m = q % p;
for (auto it=A[p][m].begin();it!=A[p][m].end();it++){
if (D[*it] > D[q]+abs(*it-q)/p)
D[*it] = D[q]+abs(*it-q)/p, que.push(mp(-D[*it], *it));
}
}
}
}
printf("%d\n", D[1] < 2e9 ? D[1] : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4420 KB |
Output is correct |
2 |
Correct |
0 ms |
4420 KB |
Output is correct |
3 |
Incorrect |
0 ms |
4420 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4420 KB |
Output is correct |
2 |
Correct |
1 ms |
4420 KB |
Output is correct |
3 |
Incorrect |
0 ms |
4420 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4420 KB |
Output is correct |
2 |
Correct |
0 ms |
4420 KB |
Output is correct |
3 |
Incorrect |
0 ms |
4420 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4420 KB |
Output is correct |
2 |
Correct |
0 ms |
4420 KB |
Output is correct |
3 |
Incorrect |
0 ms |
4420 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4420 KB |
Output is correct |
2 |
Correct |
2 ms |
4420 KB |
Output is correct |
3 |
Incorrect |
0 ms |
4420 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |