#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<queue>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> Pi;
const int MX = 30003;
const int SX = 170;
priority_queue <Pi, vector<Pi>, greater<Pi> > pq;
vector <Pi> E[MX];
int n, m;
int p[MX][2];
int chk[MX][SX+4];
int len[MX];
int main(){
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++){
scanf("%d%d", p[i], p[i] + 1);
if (p[i][1] < SX)chk[p[i][0]][p[i][1]] = 1;
}
for (int i = 0; i < m; i++){
for (int j = p[i][0] + p[i][1], l = 1; j < n; j += p[i][1], l++){
E[p[i][0]].push_back(Pi(j, l));
if (p[i][1] < SX && chk[j][p[i][1]])break;
}
}
for (int i = 0; i < m; i++){
for (int j = p[i][0] - p[i][1], l = 1; j >= 0; j -= p[i][1], l++){
E[p[i][0]].push_back(Pi(j, l));
if (p[i][1] < SX && chk[j][p[i][1]])break;
}
}
pq.push(Pi(p[0][0], 0));
for (int i = 0; i < n; i++)len[i] = 1e9;
len[p[0][0]] = 0;
while (!pq.empty()){
Pi t = pq.top();
pq.pop();
if (len[t.Y] != t.X)continue;
if (t.Y == 1)break;
for (int i = 0; i < (int)E[t.Y].size(); i++){
Pi &w = E[t.Y][i];
if (len[w.X] > len[t.Y] + w.Y){
len[w.X] = len[t.Y] + w.Y;
pq.push(Pi(len[w.X], w.X));
}
}
}
printf("%d", len[p[1][0]] == 1e9 ? -1 : len[p[1][0]]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
23028 KB |
Output is correct |
2 |
Correct |
0 ms |
23028 KB |
Output is correct |
3 |
Correct |
0 ms |
23028 KB |
Output is correct |
4 |
Correct |
0 ms |
23028 KB |
Output is correct |
5 |
Correct |
0 ms |
23028 KB |
Output is correct |
6 |
Correct |
0 ms |
23028 KB |
Output is correct |
7 |
Incorrect |
0 ms |
23028 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
23028 KB |
Output is correct |
2 |
Correct |
0 ms |
23028 KB |
Output is correct |
3 |
Correct |
0 ms |
23028 KB |
Output is correct |
4 |
Correct |
0 ms |
23028 KB |
Output is correct |
5 |
Correct |
0 ms |
23028 KB |
Output is correct |
6 |
Correct |
0 ms |
23028 KB |
Output is correct |
7 |
Incorrect |
0 ms |
23028 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
23028 KB |
Output is correct |
2 |
Correct |
0 ms |
23028 KB |
Output is correct |
3 |
Correct |
0 ms |
23028 KB |
Output is correct |
4 |
Correct |
0 ms |
23028 KB |
Output is correct |
5 |
Correct |
0 ms |
23028 KB |
Output is correct |
6 |
Correct |
0 ms |
23028 KB |
Output is correct |
7 |
Incorrect |
0 ms |
23028 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
23028 KB |
Output is correct |
2 |
Correct |
0 ms |
23028 KB |
Output is correct |
3 |
Correct |
0 ms |
23028 KB |
Output is correct |
4 |
Correct |
0 ms |
23028 KB |
Output is correct |
5 |
Correct |
0 ms |
23028 KB |
Output is correct |
6 |
Correct |
0 ms |
23028 KB |
Output is correct |
7 |
Incorrect |
0 ms |
23028 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
23028 KB |
Output is correct |
2 |
Correct |
0 ms |
23028 KB |
Output is correct |
3 |
Correct |
0 ms |
23028 KB |
Output is correct |
4 |
Correct |
0 ms |
23028 KB |
Output is correct |
5 |
Correct |
0 ms |
23028 KB |
Output is correct |
6 |
Correct |
0 ms |
23028 KB |
Output is correct |
7 |
Incorrect |
0 ms |
23028 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |