#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define debug(x) cout << #x << " => " << x << "\n";
const int mod = 1e9 + 7;
int n, m, b[30005], p[30005];
vector <pair<pair<int,int>,int>> g[30005][180];
int dp[30005][180];
bool doge[30005][180];
priority_queue <pair<int,pair<int,int>>> pq;
void solve () {
cin >> n >> m;
for(int i = 0; i < m; i++) {
cin >> b[i] >> p[i];
doge[b[i]][p[i]] = true;
if(p[i] * p[i] > n) {
for(int j = b[i] - p[i], c = 1; j >= 0; j -= p[i], c++)
g[i][0].pb({{j, 0}, c});
for(int j = b[i] + p[i], c = 1; j < n; j += p[i], c++)
g[i][0].pb({{j, 0}, c});
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j * j <= n; j++) {
dp[i][j] = 1e18 + 7;
}
}
dp[b[0]][0] = 0;
pq.push({0, {b[0], 0}});
while(!pq.empty()) {
int cost = -pq.top().fi;
int pos = pq.top().se.fi;
int pow = pq.top().se.se;
pq.pop();
if(cost > dp[pos][pow]) continue;
if(pos == b[1]) {
cout << cost << "\n";
return;
}
if(pow == 0) {
for(auto c : g[pos][pow]) {
if(dp[c.fi.fi][c.fi.se] > cost + c.se) {
dp[c.fi.fi][c.fi.se] = cost + c.se;
pq.push({-dp[c.fi.fi][c.fi.se], {c.fi.fi, c.fi.se}});
}
}
}
else {
if(pos + pow < n) {
if(dp[pos + pow][pow] > cost + 1) {
dp[pos + pow][pow] = cost + 1;
pq.push({-dp[pos + pow][pow], {pos + pow, pow}});
}
}
if(pos - pow >= 0) {
if(dp[pos - pow][pow] > cost + 1) {
dp[pos - pow][pow] = cost + 1;
pq.push({-dp[pos - pow][pow], {pos - pow, pow}});
}
}
}
for(int i = 0; i * i <= n; i++) {
if(i == 0 || doge[pos][i] == true) {
if(dp[pos][i] > cost) {
dp[pos][i] = cost;
pq.push({-dp[pos][i], {pos, i}});
}
}
}
}
// for(int i = 0; i < n; i++) {
// for(int j = 0; j * j <= n; j++) {
// cout << dp[i][j] << " ";
// }
// cout << "\n";
// }
cout << -1 << "\n";
}
signed main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve ();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
127052 KB |
Output is correct |
2 |
Correct |
62 ms |
127060 KB |
Output is correct |
3 |
Correct |
62 ms |
127048 KB |
Output is correct |
4 |
Correct |
63 ms |
127112 KB |
Output is correct |
5 |
Correct |
70 ms |
127052 KB |
Output is correct |
6 |
Correct |
63 ms |
127084 KB |
Output is correct |
7 |
Correct |
63 ms |
127068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
127152 KB |
Output is correct |
2 |
Correct |
79 ms |
127080 KB |
Output is correct |
3 |
Correct |
67 ms |
127088 KB |
Output is correct |
4 |
Correct |
61 ms |
127092 KB |
Output is correct |
5 |
Correct |
62 ms |
127052 KB |
Output is correct |
6 |
Correct |
68 ms |
127220 KB |
Output is correct |
7 |
Correct |
64 ms |
127060 KB |
Output is correct |
8 |
Correct |
62 ms |
127144 KB |
Output is correct |
9 |
Incorrect |
62 ms |
127260 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
127168 KB |
Output is correct |
2 |
Correct |
65 ms |
127156 KB |
Output is correct |
3 |
Correct |
69 ms |
127084 KB |
Output is correct |
4 |
Correct |
69 ms |
127096 KB |
Output is correct |
5 |
Correct |
63 ms |
127172 KB |
Output is correct |
6 |
Correct |
61 ms |
127144 KB |
Output is correct |
7 |
Correct |
62 ms |
127148 KB |
Output is correct |
8 |
Correct |
64 ms |
127144 KB |
Output is correct |
9 |
Incorrect |
77 ms |
127248 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
127116 KB |
Output is correct |
2 |
Correct |
64 ms |
127056 KB |
Output is correct |
3 |
Correct |
65 ms |
127168 KB |
Output is correct |
4 |
Correct |
65 ms |
127176 KB |
Output is correct |
5 |
Correct |
64 ms |
127124 KB |
Output is correct |
6 |
Correct |
69 ms |
127084 KB |
Output is correct |
7 |
Correct |
71 ms |
127072 KB |
Output is correct |
8 |
Correct |
63 ms |
127152 KB |
Output is correct |
9 |
Incorrect |
60 ms |
127236 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
127072 KB |
Output is correct |
2 |
Correct |
68 ms |
127156 KB |
Output is correct |
3 |
Correct |
73 ms |
127176 KB |
Output is correct |
4 |
Correct |
61 ms |
127048 KB |
Output is correct |
5 |
Correct |
62 ms |
127060 KB |
Output is correct |
6 |
Correct |
61 ms |
127180 KB |
Output is correct |
7 |
Correct |
61 ms |
127052 KB |
Output is correct |
8 |
Correct |
85 ms |
127128 KB |
Output is correct |
9 |
Incorrect |
65 ms |
127256 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |