#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int n,m,s,e;
vector<pair<int,int> > adj[30001];
set<int> pos[30001];
int dist[30001];
bool vis[30001];
stack<int> st;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for (int i=0;i<m;i++){
int b,p;
cin>>b>>p;
if (i==0) s=b;
if (i==1) e=b;
pos[p].insert(b);
}
for (int i=1;i<30001;i++){
for (auto j:pos[i]){
vis[j]=true;
st.push(j);
}
for (auto j:pos[i]){
int dis=1;
for (int k=j+i;k<n;k+=i){
adj[j].pb(mp(k,dis));
dis++;
if (vis[k]) break;
}
dis=1;
for (int k=j-i;k>=0;k-=i){
adj[j].pb(mp(k,dis));
dis++;
if (vis[k]) break;
}
}
while (!st.empty()){
vis[st.top()]=false;
st.pop();
}
}
for (int i=0;i<n;i++) dist[i]=1e9;
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pq;
pq.push(mp(0,s));
dist[s]=0;
while (!pq.empty()){
pair<int,int> cur=pq.top();
pq.pop();
if (dist[cur.second]<cur.first) continue;
for (auto i:adj[cur.second]){
if (dist[i.first]>cur.first+i.second){
dist[i.first]=cur.first+i.second;
pq.push(mp(cur.first+i.second,i.first));
}
}
}
if (dist[e]==1e9) cout<<-1;
else cout<<dist[e];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2424 KB |
Output is correct |
2 |
Correct |
7 ms |
2296 KB |
Output is correct |
3 |
Correct |
6 ms |
2424 KB |
Output is correct |
4 |
Correct |
7 ms |
2424 KB |
Output is correct |
5 |
Correct |
7 ms |
2424 KB |
Output is correct |
6 |
Correct |
7 ms |
2428 KB |
Output is correct |
7 |
Correct |
6 ms |
2424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2424 KB |
Output is correct |
2 |
Correct |
6 ms |
2424 KB |
Output is correct |
3 |
Correct |
6 ms |
2424 KB |
Output is correct |
4 |
Correct |
7 ms |
2424 KB |
Output is correct |
5 |
Correct |
6 ms |
2424 KB |
Output is correct |
6 |
Correct |
6 ms |
2424 KB |
Output is correct |
7 |
Correct |
7 ms |
2424 KB |
Output is correct |
8 |
Correct |
7 ms |
2424 KB |
Output is correct |
9 |
Correct |
8 ms |
2424 KB |
Output is correct |
10 |
Correct |
7 ms |
2424 KB |
Output is correct |
11 |
Correct |
9 ms |
2552 KB |
Output is correct |
12 |
Correct |
7 ms |
2424 KB |
Output is correct |
13 |
Correct |
7 ms |
2424 KB |
Output is correct |
14 |
Correct |
7 ms |
2552 KB |
Output is correct |
15 |
Correct |
8 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2424 KB |
Output is correct |
2 |
Correct |
7 ms |
2424 KB |
Output is correct |
3 |
Correct |
6 ms |
2424 KB |
Output is correct |
4 |
Correct |
7 ms |
2424 KB |
Output is correct |
5 |
Correct |
7 ms |
2424 KB |
Output is correct |
6 |
Correct |
8 ms |
2424 KB |
Output is correct |
7 |
Correct |
7 ms |
2428 KB |
Output is correct |
8 |
Correct |
7 ms |
2424 KB |
Output is correct |
9 |
Correct |
6 ms |
2424 KB |
Output is correct |
10 |
Correct |
7 ms |
2552 KB |
Output is correct |
11 |
Correct |
7 ms |
2556 KB |
Output is correct |
12 |
Correct |
7 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 |
8 ms |
2552 KB |
Output is correct |
16 |
Correct |
7 ms |
2552 KB |
Output is correct |
17 |
Correct |
8 ms |
2808 KB |
Output is correct |
18 |
Correct |
7 ms |
2552 KB |
Output is correct |
19 |
Correct |
7 ms |
2552 KB |
Output is correct |
20 |
Correct |
7 ms |
2680 KB |
Output is correct |
21 |
Correct |
7 ms |
2552 KB |
Output is correct |
22 |
Correct |
7 ms |
2552 KB |
Output is correct |
23 |
Correct |
7 ms |
2552 KB |
Output is correct |
24 |
Correct |
8 ms |
2680 KB |
Output is correct |
25 |
Correct |
8 ms |
2684 KB |
Output is correct |
26 |
Correct |
8 ms |
2552 KB |
Output is correct |
27 |
Correct |
7 ms |
2552 KB |
Output is correct |
28 |
Correct |
8 ms |
2808 KB |
Output is correct |
29 |
Correct |
10 ms |
3320 KB |
Output is correct |
30 |
Correct |
8 ms |
2808 KB |
Output is correct |
31 |
Correct |
8 ms |
2936 KB |
Output is correct |
32 |
Correct |
8 ms |
2808 KB |
Output is correct |
33 |
Correct |
12 ms |
4088 KB |
Output is correct |
34 |
Correct |
14 ms |
4092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2428 KB |
Output is correct |
2 |
Correct |
7 ms |
2424 KB |
Output is correct |
3 |
Correct |
6 ms |
2424 KB |
Output is correct |
4 |
Correct |
6 ms |
2424 KB |
Output is correct |
5 |
Correct |
7 ms |
2424 KB |
Output is correct |
6 |
Correct |
7 ms |
2556 KB |
Output is correct |
7 |
Correct |
7 ms |
2424 KB |
Output is correct |
8 |
Correct |
6 ms |
2424 KB |
Output is correct |
9 |
Correct |
6 ms |
2424 KB |
Output is correct |
10 |
Correct |
7 ms |
2428 KB |
Output is correct |
11 |
Correct |
7 ms |
2552 KB |
Output is correct |
12 |
Correct |
7 ms |
2424 KB |
Output is correct |
13 |
Correct |
7 ms |
2428 KB |
Output is correct |
14 |
Correct |
7 ms |
2552 KB |
Output is correct |
15 |
Correct |
7 ms |
2684 KB |
Output is correct |
16 |
Correct |
7 ms |
2552 KB |
Output is correct |
17 |
Correct |
8 ms |
2680 KB |
Output is correct |
18 |
Correct |
7 ms |
2552 KB |
Output is correct |
19 |
Correct |
6 ms |
2552 KB |
Output is correct |
20 |
Correct |
7 ms |
2680 KB |
Output is correct |
21 |
Correct |
6 ms |
2552 KB |
Output is correct |
22 |
Correct |
8 ms |
2552 KB |
Output is correct |
23 |
Correct |
9 ms |
2552 KB |
Output is correct |
24 |
Correct |
8 ms |
2680 KB |
Output is correct |
25 |
Correct |
8 ms |
2680 KB |
Output is correct |
26 |
Correct |
7 ms |
2552 KB |
Output is correct |
27 |
Correct |
7 ms |
2552 KB |
Output is correct |
28 |
Correct |
8 ms |
2808 KB |
Output is correct |
29 |
Correct |
10 ms |
3320 KB |
Output is correct |
30 |
Correct |
8 ms |
2808 KB |
Output is correct |
31 |
Correct |
9 ms |
2936 KB |
Output is correct |
32 |
Correct |
8 ms |
2808 KB |
Output is correct |
33 |
Correct |
12 ms |
4088 KB |
Output is correct |
34 |
Correct |
12 ms |
4088 KB |
Output is correct |
35 |
Correct |
21 ms |
4728 KB |
Output is correct |
36 |
Correct |
11 ms |
2936 KB |
Output is correct |
37 |
Correct |
22 ms |
5496 KB |
Output is correct |
38 |
Correct |
27 ms |
5624 KB |
Output is correct |
39 |
Correct |
27 ms |
5752 KB |
Output is correct |
40 |
Correct |
26 ms |
5624 KB |
Output is correct |
41 |
Correct |
25 ms |
5624 KB |
Output is correct |
42 |
Correct |
12 ms |
2552 KB |
Output is correct |
43 |
Correct |
11 ms |
2552 KB |
Output is correct |
44 |
Correct |
13 ms |
2680 KB |
Output is correct |
45 |
Correct |
35 ms |
8568 KB |
Output is correct |
46 |
Correct |
33 ms |
8568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2424 KB |
Output is correct |
2 |
Correct |
6 ms |
2424 KB |
Output is correct |
3 |
Correct |
7 ms |
2424 KB |
Output is correct |
4 |
Correct |
7 ms |
2424 KB |
Output is correct |
5 |
Correct |
7 ms |
2424 KB |
Output is correct |
6 |
Correct |
7 ms |
2552 KB |
Output is correct |
7 |
Correct |
7 ms |
2424 KB |
Output is correct |
8 |
Correct |
7 ms |
2424 KB |
Output is correct |
9 |
Correct |
7 ms |
2424 KB |
Output is correct |
10 |
Correct |
7 ms |
2424 KB |
Output is correct |
11 |
Correct |
7 ms |
2552 KB |
Output is correct |
12 |
Correct |
7 ms |
2424 KB |
Output is correct |
13 |
Correct |
7 ms |
2424 KB |
Output is correct |
14 |
Correct |
7 ms |
2552 KB |
Output is correct |
15 |
Correct |
8 ms |
2680 KB |
Output is correct |
16 |
Correct |
7 ms |
2456 KB |
Output is correct |
17 |
Correct |
8 ms |
2680 KB |
Output is correct |
18 |
Correct |
8 ms |
2552 KB |
Output is correct |
19 |
Correct |
7 ms |
2552 KB |
Output is correct |
20 |
Correct |
8 ms |
2680 KB |
Output is correct |
21 |
Correct |
8 ms |
2552 KB |
Output is correct |
22 |
Correct |
7 ms |
2552 KB |
Output is correct |
23 |
Correct |
7 ms |
2552 KB |
Output is correct |
24 |
Correct |
8 ms |
2680 KB |
Output is correct |
25 |
Correct |
9 ms |
2808 KB |
Output is correct |
26 |
Correct |
7 ms |
2552 KB |
Output is correct |
27 |
Correct |
7 ms |
2552 KB |
Output is correct |
28 |
Correct |
8 ms |
2808 KB |
Output is correct |
29 |
Correct |
10 ms |
3320 KB |
Output is correct |
30 |
Correct |
8 ms |
2808 KB |
Output is correct |
31 |
Correct |
8 ms |
2940 KB |
Output is correct |
32 |
Correct |
8 ms |
2808 KB |
Output is correct |
33 |
Correct |
13 ms |
4088 KB |
Output is correct |
34 |
Correct |
13 ms |
4088 KB |
Output is correct |
35 |
Correct |
21 ms |
4728 KB |
Output is correct |
36 |
Correct |
9 ms |
2852 KB |
Output is correct |
37 |
Correct |
22 ms |
5496 KB |
Output is correct |
38 |
Correct |
29 ms |
5624 KB |
Output is correct |
39 |
Correct |
25 ms |
5752 KB |
Output is correct |
40 |
Correct |
27 ms |
5624 KB |
Output is correct |
41 |
Correct |
24 ms |
5624 KB |
Output is correct |
42 |
Correct |
11 ms |
2552 KB |
Output is correct |
43 |
Correct |
11 ms |
2552 KB |
Output is correct |
44 |
Correct |
12 ms |
2680 KB |
Output is correct |
45 |
Correct |
35 ms |
8572 KB |
Output is correct |
46 |
Correct |
35 ms |
8696 KB |
Output is correct |
47 |
Correct |
51 ms |
12920 KB |
Output is correct |
48 |
Correct |
24 ms |
6008 KB |
Output is correct |
49 |
Correct |
19 ms |
4984 KB |
Output is correct |
50 |
Correct |
17 ms |
4732 KB |
Output is correct |
51 |
Correct |
48 ms |
7992 KB |
Output is correct |
52 |
Correct |
50 ms |
8500 KB |
Output is correct |
53 |
Correct |
30 ms |
5500 KB |
Output is correct |
54 |
Correct |
8 ms |
3064 KB |
Output is correct |
55 |
Correct |
11 ms |
3444 KB |
Output is correct |
56 |
Correct |
27 ms |
5112 KB |
Output is correct |
57 |
Correct |
46 ms |
11640 KB |
Output is correct |
58 |
Correct |
14 ms |
3572 KB |
Output is correct |
59 |
Correct |
17 ms |
3960 KB |
Output is correct |
60 |
Correct |
21 ms |
4596 KB |
Output is correct |
61 |
Correct |
18 ms |
3960 KB |
Output is correct |
62 |
Correct |
38 ms |
8948 KB |
Output is correct |
63 |
Correct |
95 ms |
27516 KB |
Output is correct |
64 |
Correct |
110 ms |
36076 KB |
Output is correct |
65 |
Correct |
142 ms |
45680 KB |
Output is correct |
66 |
Correct |
244 ms |
74080 KB |
Output is correct |
67 |
Correct |
250 ms |
74212 KB |
Output is correct |