This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
void solve()
{
ll n, m;
cin >> n >> m;
ll b[m], p[m];
vector<ll> eq[n];
for (ll i = 0; i < m; i++)
cin >> b[i] >> p[i], eq[b[i]].push_back(p[i]);
for (ll i = 0; i < n; i++)
sort(eq[i].begin(), eq[i].end(), greater());
ll dist[n];
for (ll i = 0; i < n; i++)
dist[i] = 1e18;
dist[b[0]] = 0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q;
q.push(make_pair(0, b[0]));
while (!q.empty())
{
auto [w, i] = q.top();
q.pop();
if (dist[i] < w)
continue;
for (ll j : eq[i])
{
for (ll k = i - j, cur = 0; k >= 0; k -= j)
{
cur++;
if (dist[k] > w + cur)
q.push(make_pair(dist[k] = w + cur, k));
}
for (ll k = i + j, cur = 0; k < n; k += j)
{
cur++;
if (dist[k] > w + cur)
q.push(make_pair(dist[k] = w + cur, k));
}
}
eq[i].clear();
}
if (dist[b[1]] == 1e18)
dist[b[1]] = -1;
cout << dist[b[1]] << endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
// precomp();
// cin >> t;
for (ll i = 1; i <= t; i++)
solve();
cerr << "\nTime elapsed: " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |