#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P2;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define index(xs, x) (int)(lower_bound(all(xs), x) - xs.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007
const int B = 200;
const int LIM = 1000000;
int N, M;
vector<int> G[30000];
int D[30000][B+1];
queue<P> Q[LIM];
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> N >> M;
rep(i, N) rep(j, B+1) D[i][j] = INF;
int sx = 0, tx = 0;
rep(i, M) {
int b, p;
cin >> b >> p;
if (i == 0) sx = b;
else if (i == 1) tx = b;
G[b].pb(p);
}
rep(i, N) sort(all(G[i])), uniq(G[i]);
D[sx][0] = 0;
Q[0].push(P(sx, 0));
rep(r, LIM) {
while (!Q[r].empty()) {
int x = Q[r].front()._1, p = Q[r].front()._2;
Q[r].pop();
if (D[x][p] < r) continue;
if (p == 0) {
for (int np : G[x]) {
if (np <= B) {
if (D[x][np] > r) {
D[x][np] = r;
Q[r].push(P(x, np));
}
}
else {
for (int t=x%np; t<N; t+=np) {
int cost = abs(x-t)/np;
if (D[t][0] > r+cost) {
D[t][0] = r+cost;
Q[r+cost].push(P(t, 0));
}
}
}
}
}
else {
if (D[x][0] > r) {
D[x][0] = r;
Q[r].push(P(x, 0));
}
if (x-p >= 0) {
if (D[x-p][p] > r+1) {
D[x-p][p] = r+1;
Q[r+1].push(P(x-p, p));
}
}
if (x+p < N) {
if (D[x+p][p] > r+1) {
D[x+p][p] = r+1;
Q[r+1].push(P(x+p, p));
}
}
}
}
}
int m = D[tx][0];
if (m == INF) m = -1;
cout << m << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Memory limit exceeded |
93 ms |
262144 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Memory limit exceeded |
93 ms |
262144 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Memory limit exceeded |
79 ms |
262144 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Memory limit exceeded |
83 ms |
262144 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Memory limit exceeded |
69 ms |
262144 KB |
Memory limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |