#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
typedef long long ll;
using namespace std;
const int sqrtn = 180, inf = 1e9 + 5;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
int k = sqrtn * n;
vector<vector<pair<int, int> > > g(k);
vector<int> b(m), p(m);
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
if (p[i] < sqrtn)
{
g[b[i]].push_back({ p[i] * n + b[i], 0 });
}
else
{
for (int j = b[i] % p[i]; j < n; j += p[i])
{
g[b[i]].push_back({ j, abs(b[i] - j) / p[i] });
}
}
}
for (int step = 1; step < sqrtn; step++) for (int i = 0; i < n; i++)
{
if (i - step >= 0)
{
g[step * n + i].push_back({ step * n + i - step, 1 });
g[step * n + i].push_back({ i - step, 1 });
}
if (i + step < n)
{
g[step * n + i].push_back({ step * n + i + step, 1 });
g[step * n + i].push_back({ i + step, 1 });
}
}
//cout << "\n";
//for (int i = 0; i < k; i++) for (pair<int, int> j : g[i]) cout << i << " " << j.first << " " << j.second << "\n";
priority_queue<pair<int, int> > pq;
pq.push({ 0, b[0] });
vector<int> dist(k, inf);
while (!pq.empty())
{
int u = pq.top().first, d = -pq.top().second;
pq.pop();
if (dist[u] != inf) continue;
dist[u] = d;
for (pair<int, int> i : g[u]) if (dist[i.first] == inf)
{
pq.push({ i.first, -(d + i.second) });
}
}
cout << (dist[b[1]] == inf ? -1 : dist[b[1]]) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Incorrect |
1 ms |
440 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
316 KB |
Output is correct |
8 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |