#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 ll R=1e3+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],ch[N][R];
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(step<R&&ch[j][step])break;
if(vis[j]==ti)continue;
if(step<R)ch[j][step]=1;
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(step<R&&ch[j][step])
if(vis[j]==ti)continue;
if(step<R)ch[j][step]=1;
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 |
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 |
5 ms |
2040 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 |
1912 KB |
Output is correct |
4 |
Correct |
6 ms |
2040 KB |
Output is correct |
5 |
Correct |
5 ms |
2044 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 |
2168 KB |
Output is correct |
9 |
Correct |
5 ms |
2168 KB |
Output is correct |
10 |
Correct |
6 ms |
2424 KB |
Output is correct |
11 |
Correct |
6 ms |
2424 KB |
Output is correct |
12 |
Correct |
6 ms |
2424 KB |
Output is correct |
13 |
Correct |
6 ms |
2424 KB |
Output is correct |
14 |
Correct |
7 ms |
2552 KB |
Output is correct |
15 |
Correct |
6 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2040 KB |
Output is correct |
2 |
Correct |
5 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 |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2168 KB |
Output is correct |
10 |
Correct |
6 ms |
2424 KB |
Output is correct |
11 |
Correct |
6 ms |
2552 KB |
Output is correct |
12 |
Correct |
6 ms |
2424 KB |
Output is correct |
13 |
Correct |
6 ms |
2424 KB |
Output is correct |
14 |
Correct |
7 ms |
2552 KB |
Output is correct |
15 |
Correct |
7 ms |
2552 KB |
Output is correct |
16 |
Correct |
6 ms |
2040 KB |
Output is correct |
17 |
Correct |
11 ms |
6904 KB |
Output is correct |
18 |
Correct |
6 ms |
2040 KB |
Output is correct |
19 |
Correct |
6 ms |
2200 KB |
Output is correct |
20 |
Correct |
26 ms |
10232 KB |
Output is correct |
21 |
Correct |
6 ms |
2040 KB |
Output is correct |
22 |
Correct |
6 ms |
2040 KB |
Output is correct |
23 |
Correct |
13 ms |
10360 KB |
Output is correct |
24 |
Correct |
15 ms |
13944 KB |
Output is correct |
25 |
Correct |
13 ms |
10744 KB |
Output is correct |
26 |
Correct |
14 ms |
10232 KB |
Output is correct |
27 |
Correct |
14 ms |
11384 KB |
Output is correct |
28 |
Correct |
15 ms |
14200 KB |
Output is correct |
29 |
Correct |
17 ms |
11128 KB |
Output is correct |
30 |
Correct |
14 ms |
10744 KB |
Output is correct |
31 |
Correct |
15 ms |
11000 KB |
Output is correct |
32 |
Correct |
14 ms |
11128 KB |
Output is correct |
33 |
Correct |
23 ms |
12024 KB |
Output is correct |
34 |
Correct |
22 ms |
12144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 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 |
5 ms |
2040 KB |
Output is correct |
5 |
Correct |
5 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 |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2168 KB |
Output is correct |
10 |
Correct |
6 ms |
2424 KB |
Output is correct |
11 |
Correct |
6 ms |
2552 KB |
Output is correct |
12 |
Correct |
6 ms |
2424 KB |
Output is correct |
13 |
Correct |
6 ms |
2424 KB |
Output is correct |
14 |
Correct |
7 ms |
2556 KB |
Output is correct |
15 |
Correct |
7 ms |
2552 KB |
Output is correct |
16 |
Correct |
6 ms |
2040 KB |
Output is correct |
17 |
Correct |
10 ms |
6904 KB |
Output is correct |
18 |
Correct |
6 ms |
2040 KB |
Output is correct |
19 |
Correct |
6 ms |
2040 KB |
Output is correct |
20 |
Correct |
27 ms |
10156 KB |
Output is correct |
21 |
Correct |
6 ms |
2040 KB |
Output is correct |
22 |
Correct |
6 ms |
2040 KB |
Output is correct |
23 |
Correct |
13 ms |
10360 KB |
Output is correct |
24 |
Correct |
15 ms |
13944 KB |
Output is correct |
25 |
Correct |
12 ms |
10744 KB |
Output is correct |
26 |
Correct |
12 ms |
10232 KB |
Output is correct |
27 |
Correct |
12 ms |
11384 KB |
Output is correct |
28 |
Correct |
15 ms |
14200 KB |
Output is correct |
29 |
Correct |
16 ms |
11128 KB |
Output is correct |
30 |
Correct |
13 ms |
10748 KB |
Output is correct |
31 |
Correct |
18 ms |
11128 KB |
Output is correct |
32 |
Correct |
14 ms |
11128 KB |
Output is correct |
33 |
Correct |
21 ms |
12152 KB |
Output is correct |
34 |
Correct |
20 ms |
12016 KB |
Output is correct |
35 |
Correct |
25 ms |
14960 KB |
Output is correct |
36 |
Correct |
11 ms |
8184 KB |
Output is correct |
37 |
Correct |
30 ms |
19952 KB |
Output is correct |
38 |
Correct |
30 ms |
19056 KB |
Output is correct |
39 |
Correct |
14 ms |
3568 KB |
Output is correct |
40 |
Correct |
20 ms |
12156 KB |
Output is correct |
41 |
Correct |
25 ms |
17520 KB |
Output is correct |
42 |
Correct |
17 ms |
11000 KB |
Output is correct |
43 |
Correct |
19 ms |
12152 KB |
Output is correct |
44 |
Correct |
32 ms |
10908 KB |
Output is correct |
45 |
Correct |
73 ms |
18152 KB |
Output is correct |
46 |
Correct |
64 ms |
18276 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 |
5 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 |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2168 KB |
Output is correct |
10 |
Correct |
6 ms |
2424 KB |
Output is correct |
11 |
Correct |
6 ms |
2428 KB |
Output is correct |
12 |
Correct |
6 ms |
2424 KB |
Output is correct |
13 |
Correct |
6 ms |
2424 KB |
Output is correct |
14 |
Correct |
7 ms |
2552 KB |
Output is correct |
15 |
Correct |
6 ms |
2552 KB |
Output is correct |
16 |
Correct |
6 ms |
2040 KB |
Output is correct |
17 |
Correct |
10 ms |
6776 KB |
Output is correct |
18 |
Correct |
6 ms |
2040 KB |
Output is correct |
19 |
Correct |
6 ms |
2168 KB |
Output is correct |
20 |
Correct |
26 ms |
10232 KB |
Output is correct |
21 |
Correct |
6 ms |
2040 KB |
Output is correct |
22 |
Correct |
7 ms |
2040 KB |
Output is correct |
23 |
Correct |
12 ms |
10232 KB |
Output is correct |
24 |
Correct |
16 ms |
13816 KB |
Output is correct |
25 |
Correct |
14 ms |
10744 KB |
Output is correct |
26 |
Correct |
12 ms |
10232 KB |
Output is correct |
27 |
Correct |
13 ms |
11384 KB |
Output is correct |
28 |
Correct |
16 ms |
14200 KB |
Output is correct |
29 |
Correct |
17 ms |
11128 KB |
Output is correct |
30 |
Correct |
15 ms |
10744 KB |
Output is correct |
31 |
Correct |
15 ms |
11000 KB |
Output is correct |
32 |
Correct |
15 ms |
11128 KB |
Output is correct |
33 |
Correct |
23 ms |
12024 KB |
Output is correct |
34 |
Correct |
22 ms |
12024 KB |
Output is correct |
35 |
Correct |
24 ms |
14960 KB |
Output is correct |
36 |
Correct |
11 ms |
8184 KB |
Output is correct |
37 |
Correct |
28 ms |
19952 KB |
Output is correct |
38 |
Correct |
29 ms |
19184 KB |
Output is correct |
39 |
Correct |
14 ms |
3448 KB |
Output is correct |
40 |
Correct |
19 ms |
12152 KB |
Output is correct |
41 |
Correct |
25 ms |
17524 KB |
Output is correct |
42 |
Correct |
18 ms |
10872 KB |
Output is correct |
43 |
Correct |
18 ms |
12152 KB |
Output is correct |
44 |
Correct |
32 ms |
10908 KB |
Output is correct |
45 |
Correct |
70 ms |
18148 KB |
Output is correct |
46 |
Correct |
66 ms |
18276 KB |
Output is correct |
47 |
Correct |
107 ms |
106596 KB |
Output is correct |
48 |
Correct |
12 ms |
2808 KB |
Output is correct |
49 |
Correct |
12 ms |
2808 KB |
Output is correct |
50 |
Correct |
10 ms |
2680 KB |
Output is correct |
51 |
Correct |
158 ms |
188520 KB |
Output is correct |
52 |
Correct |
166 ms |
190412 KB |
Output is correct |
53 |
Correct |
97 ms |
124784 KB |
Output is correct |
54 |
Correct |
47 ms |
63096 KB |
Output is correct |
55 |
Correct |
62 ms |
83064 KB |
Output is correct |
56 |
Execution timed out |
1097 ms |
124932 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |