#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll MOD=1e9+7;
const ll N=3e4+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll)a.size()
ll n,m,b[N],p[N],vis[N],ti,x,y,dis[N];
vector<ll> v[N];
vector<pll> ed[N];
priority_queue<pll,vector<pll>,greater<pll> > pq;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
REP(i,m){
cin>>b[i]>>p[i];
v[b[i]].pb(p[i]);
}
REP(i,N)sort(v[i].rbegin(),v[i].rend()),v[i].resize(unique(v[i].begin(),v[i].end())-v[i].begin());
REP(i,N)dis[i]=INF;
pq.push(mp(0,b[0]));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(dis[x]<y)continue;
dis[x]=y;
++ti;
for(ll step:v[x]){
for(ll j=x+step,tt=1;j<n;j+=step,++tt){
if(vis[j]==ti)continue;
vis[j]=ti;
if(tt+dis[x]<dis[j])pq.push(mp(tt+dis[x],j));
}
for(ll j=x-step,tt=1;j>=0;j-=step,++tt){
if(vis[j]==ti)continue;
vis[j]=ti;
if(tt+dis[x]<dis[j])pq.push(mp(tt+dis[x],j));
}
}
if(dis[b[1]]!=INF){cout<<dis[b[1]]<<"\n";return 0;}
}
cout<<(dis[b[1]]==INF?-1:dis[b[1]])<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2040 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
1912 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
6 ms |
2040 KB |
Output is correct |
6 |
Correct |
6 ms |
2044 KB |
Output is correct |
7 |
Correct |
6 ms |
2040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2040 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
6 ms |
2040 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
6 ms |
2040 KB |
Output is correct |
8 |
Correct |
6 ms |
2040 KB |
Output is correct |
9 |
Correct |
6 ms |
2040 KB |
Output is correct |
10 |
Correct |
6 ms |
2040 KB |
Output is correct |
11 |
Correct |
6 ms |
2040 KB |
Output is correct |
12 |
Correct |
6 ms |
2040 KB |
Output is correct |
13 |
Runtime error |
390 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2040 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
6 ms |
2040 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
6 ms |
2044 KB |
Output is correct |
8 |
Correct |
6 ms |
2040 KB |
Output is correct |
9 |
Correct |
6 ms |
2040 KB |
Output is correct |
10 |
Correct |
6 ms |
2044 KB |
Output is correct |
11 |
Correct |
6 ms |
2040 KB |
Output is correct |
12 |
Correct |
6 ms |
2040 KB |
Output is correct |
13 |
Runtime error |
383 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2044 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
6 ms |
2040 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
6 ms |
2040 KB |
Output is correct |
8 |
Correct |
6 ms |
2044 KB |
Output is correct |
9 |
Correct |
6 ms |
2040 KB |
Output is correct |
10 |
Correct |
6 ms |
2040 KB |
Output is correct |
11 |
Correct |
6 ms |
2040 KB |
Output is correct |
12 |
Correct |
6 ms |
2040 KB |
Output is correct |
13 |
Runtime error |
396 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2040 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
6 ms |
2040 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
6 ms |
2040 KB |
Output is correct |
6 |
Correct |
6 ms |
2040 KB |
Output is correct |
7 |
Correct |
6 ms |
2040 KB |
Output is correct |
8 |
Correct |
6 ms |
2044 KB |
Output is correct |
9 |
Correct |
6 ms |
2040 KB |
Output is correct |
10 |
Correct |
6 ms |
2040 KB |
Output is correct |
11 |
Correct |
6 ms |
2040 KB |
Output is correct |
12 |
Correct |
6 ms |
2040 KB |
Output is correct |
13 |
Runtime error |
389 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
14 |
Halted |
0 ms |
0 KB |
- |