#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
#define f(i,a,b) for(ll i = a; i < b; i++)
#define fa(i,a,b) for(ll i = a; i >= b; i--)
const int N = 30005;
const ll inf = 1e18;
int n, m, b[N];
ll p[N], d[N];
vector <pair<ll,ll>> adj[N], v[N];
set <int> s[N];
void dij(int x){
f(i,0,m) d[i] = inf;
d[x] = 0;
priority_queue <ii, vector <ii>, greater<ii>> q;
q.push({0, x});
while(!q.empty()){
ii p = q.top();
q.pop();
ll d_v = p.first, v = p.second;
if(d_v != d[v]) continue;
for(auto wi: adj[v]){
ll u = wi.first, w = wi.second;
if(d[u] > d[v] + w){
d[u] = d[v] + w;
q.push({d[u], u});
}
}
}
}
int main(){
cin >> n >> m;
f(i,0,m){
cin >> b[i] >> p[i];
if(s[b[i]].count(p[i])) continue;
s[b[i]].insert(p[i]);
v[b[i]].push_back({p[i], i});
}
f(i,0,n){
int l = v[i].size();
f(j,0,l){
adj[v[i][j].second].push_back({v[i][(j+1)%l].second, 0});
}
}
f(i,0,n){
map <int, bool> us;
for(auto p: v[i]){
int pi = p.first, node = p.second;
if(us[pi]) continue;
us[pi] = 1;
for(ll x = i-pi; x >= 0; x -= pi){
for(auto u: v[x]){
adj[node].push_back({u.second, (i-x)/pi});
}
if(s[x].count(pi)) break;
}
}
}
fa(i,n-1,0){
map <int, bool> us;
for(auto p: v[i]){
int pi = p.first, node = p.second;
if(us[pi]) continue;
us[pi] = 1;
for(ll x = i+pi; x < n; x += pi){
for(auto u: v[x]){
adj[node].push_back({u.second, (x-i)/pi});
}
if(s[x].count(pi)) break;
}
}
}
dij(0);
cout << ((d[1] == inf) ? -1 : d[1]) << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Incorrect |
2 ms |
3028 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3100 KB |
Output is correct |
2 |
Incorrect |
2 ms |
3028 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Incorrect |
2 ms |
3028 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Incorrect |
2 ms |
3028 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Incorrect |
2 ms |
3028 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |