#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double doublel;
#define f first
#define s second
const int N = 30000 + 100;
const ll base = 313;
const ll mod = 1e9 + 9;
const ll inf = 1e17;
set<pair<ll, int>> s;
ll dis[N];
vector<pair<int, int>> adj[N];
int n, m, b, p;
void dij(int u)
{
for(int i = 0; i < n; i++)
dis[i] = inf;
dis[u] = 0;
for(int i = 0; i < n; i++)
s.insert({dis[i], i});
while(!s.empty())
{
int x = s.begin() -> s;
s.erase(s.begin());
for(auto y : adj[x])
{
if(dis[y.f] > dis[x] + y.s)
{
s.erase({dis[y.f], y.f});
dis[y.f] = dis[x] + y.s;
s.insert({dis[y.f], y.f});
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++)
{
cin >> b >> p;
for(int i = b+p; i <= n; i += p)
{
adj[b].push_back({i, (i-b)/p});
//cout << i << ' ' << b << ' ' << (i-b)/p << endl;
}
for(int i = b-p; i >= 0; i -= p)
{
adj[b].push_back({i, (b-i)/p});
//cout << i << ' ' << b << ' ' << (b-i)/p << endl;
}
}
dij(0);
if(dis[1] >= inf)
dis[1] = -1;
cout << dis[1] << endl;
return 0;
}
/*
5 3
0 2
1 1
4 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1132 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
1004 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |