#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf 0x3F3F3F3F
const int MXN = 3e4 + 5;
int b[MXN], p[MXN], dist[MXN];
vector<int> adj[MXN];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) dist[i] = inf;
for (int i = 0; i < m; i++)
{
cin >> b[i] >> p[i];
adj[--b[i]].push_back(p[i]);
}
priority_queue<array<int, 2>, vector<array<int, 2>>, greater<array<int, 2>>> pq;
dist[b[0]] = 0;
pq.push({0, b[0]});
while (!pq.empty())
{
array<int, 2> f = pq.top();
pq.pop();
if (f[0] > dist[f[1]]) continue;
for (int v : adj[f[1]])
{
for (int i = f[1] - v; i >= 0; i -= v)
{
if (dist[i] > dist[f[1]] + (f[1] - i) / v)
{
dist[i] = dist[f[1]] + (f[1] - i) / v;
pq.push({dist[i], i});
}
}
for (int i = f[1] + v; i < n; i += v)
{
if (dist[i] > dist[f[1]] + (i - f[1]) / v)
{
dist[i] = dist[f[1]] + (i - f[1]) / v;
pq.push({dist[i], i});
}
}
}
}
cout << dist[b[1]] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1268 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1164 KB |
Output is correct |
3 |
Correct |
1 ms |
1272 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1168 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1168 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1368 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1164 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |