#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[0] = 0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q;
q.push(make_pair(0, 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 -= i)
{
cur++;
if (dist[k] > dist[i] + cur)
q.push(make_pair(dist[k] = dist[i] + cur, k));
}
for (ll k = i + j, cur = 0; k < n; k += i)
{
cur++;
if (dist[k] > dist[i] + cur)
q.push(make_pair(dist[k] = dist[i] + cur, k));
}
}
}
if (dist[1] == 1e18)
dist[1] = -1;
cout << dist[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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1034 ms |
344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1059 ms |
348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1037 ms |
344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1038 ms |
348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1057 ms |
348 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |