#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl ;
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(),(v).end()
const int MOD = 1000000007;
const int N = 1000005;
const double PI =4*atan(1);
const double eps = 1e-7;
ll n,m;
ll x,y;
vl tab[N];
ll p[N];
ll b[N];
bool vis[N];
unordered_map<ll ,ll> maa;
set <pair<ll,ll>> sss;
ll ans=-1;
void bfs(ll u){
priority_queue<pair<ll,ll> > pq;
for(auto t:tab[u]){
pq.push(mp(0,u*50000 + p[t]));
}
vis[u]=1;
pair<ll,ll> ras;
while(!pq.empty()){
ras=pq.top();
pq.pop();
ll x=ras.s/50000;
ll y=ras.s%50000;
if(x==b[1]){
ans=-ras.f;
return ;
}
if(maa[ras.s])continue;
maa[ras.s]=1;
//cout << x << " " << y << endl;
if(x+y <n && maa[50000*(x+y)+y]==0){
pq.push(mp(ras.f-1,50000*(x+y)+y));
}
if(x-y >=0 && maa[50000*(x-y)+y]==0){
pq.push(mp(ras.f-1,50000*(x-y)+y));
}
//cout << x << endl;
if(vis[x])continue;
for(auto t: tab[x]){
pq.push(mp(ras.f,x*50000+p[t] ));
}
vis[x]=1;
}
}
int main(){
ios::sync_with_stdio(0);
//freopen("easy.txt","r",stdin);
cin >> n >>m ;
for(int i=0;i<m;i++){
cin >> x >> y;
p[i]=y;
b[i]=x;
tab[x].pb(i);
}
//cout << b[0]<< endl;
bfs(b[0]);
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
23 ms |
23800 KB |
Output is correct |
3 |
Correct |
22 ms |
23800 KB |
Output is correct |
4 |
Correct |
22 ms |
23800 KB |
Output is correct |
5 |
Correct |
22 ms |
23800 KB |
Output is correct |
6 |
Correct |
22 ms |
23800 KB |
Output is correct |
7 |
Correct |
22 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23800 KB |
Output is correct |
3 |
Correct |
22 ms |
23800 KB |
Output is correct |
4 |
Correct |
22 ms |
23772 KB |
Output is correct |
5 |
Correct |
22 ms |
23800 KB |
Output is correct |
6 |
Correct |
22 ms |
23800 KB |
Output is correct |
7 |
Correct |
22 ms |
23800 KB |
Output is correct |
8 |
Correct |
22 ms |
23800 KB |
Output is correct |
9 |
Correct |
27 ms |
23800 KB |
Output is correct |
10 |
Correct |
58 ms |
23928 KB |
Output is correct |
11 |
Correct |
22 ms |
23928 KB |
Output is correct |
12 |
Correct |
23 ms |
23928 KB |
Output is correct |
13 |
Correct |
23 ms |
24040 KB |
Output is correct |
14 |
Correct |
25 ms |
24184 KB |
Output is correct |
15 |
Correct |
24 ms |
24056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23800 KB |
Output is correct |
3 |
Correct |
22 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
22 ms |
23800 KB |
Output is correct |
6 |
Correct |
22 ms |
23800 KB |
Output is correct |
7 |
Correct |
22 ms |
23800 KB |
Output is correct |
8 |
Correct |
23 ms |
23788 KB |
Output is correct |
9 |
Correct |
22 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23800 KB |
Output is correct |
11 |
Correct |
22 ms |
23928 KB |
Output is correct |
12 |
Correct |
27 ms |
23928 KB |
Output is correct |
13 |
Correct |
28 ms |
23928 KB |
Output is correct |
14 |
Correct |
29 ms |
24184 KB |
Output is correct |
15 |
Correct |
28 ms |
24104 KB |
Output is correct |
16 |
Correct |
27 ms |
23800 KB |
Output is correct |
17 |
Correct |
25 ms |
24056 KB |
Output is correct |
18 |
Correct |
23 ms |
23928 KB |
Output is correct |
19 |
Correct |
22 ms |
23928 KB |
Output is correct |
20 |
Correct |
23 ms |
24056 KB |
Output is correct |
21 |
Correct |
22 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23844 KB |
Output is correct |
23 |
Correct |
25 ms |
24056 KB |
Output is correct |
24 |
Correct |
25 ms |
24228 KB |
Output is correct |
25 |
Correct |
23 ms |
23928 KB |
Output is correct |
26 |
Correct |
23 ms |
24056 KB |
Output is correct |
27 |
Correct |
24 ms |
24056 KB |
Output is correct |
28 |
Correct |
28 ms |
24824 KB |
Output is correct |
29 |
Correct |
44 ms |
26792 KB |
Output is correct |
30 |
Correct |
28 ms |
24568 KB |
Output is correct |
31 |
Correct |
35 ms |
25336 KB |
Output is correct |
32 |
Correct |
31 ms |
24824 KB |
Output is correct |
33 |
Correct |
67 ms |
29956 KB |
Output is correct |
34 |
Correct |
45 ms |
26916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23800 KB |
Output is correct |
3 |
Correct |
22 ms |
23800 KB |
Output is correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
22 ms |
23804 KB |
Output is correct |
6 |
Correct |
21 ms |
23800 KB |
Output is correct |
7 |
Correct |
22 ms |
23772 KB |
Output is correct |
8 |
Correct |
22 ms |
23800 KB |
Output is correct |
9 |
Correct |
22 ms |
23800 KB |
Output is correct |
10 |
Correct |
23 ms |
23928 KB |
Output is correct |
11 |
Correct |
23 ms |
23932 KB |
Output is correct |
12 |
Correct |
23 ms |
23928 KB |
Output is correct |
13 |
Correct |
23 ms |
23928 KB |
Output is correct |
14 |
Correct |
25 ms |
24184 KB |
Output is correct |
15 |
Correct |
24 ms |
24056 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
24 ms |
24056 KB |
Output is correct |
18 |
Correct |
23 ms |
23928 KB |
Output is correct |
19 |
Correct |
22 ms |
23800 KB |
Output is correct |
20 |
Correct |
24 ms |
24056 KB |
Output is correct |
21 |
Correct |
22 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23928 KB |
Output is correct |
23 |
Correct |
24 ms |
24056 KB |
Output is correct |
24 |
Correct |
26 ms |
24312 KB |
Output is correct |
25 |
Correct |
23 ms |
23928 KB |
Output is correct |
26 |
Correct |
23 ms |
24056 KB |
Output is correct |
27 |
Correct |
24 ms |
24060 KB |
Output is correct |
28 |
Correct |
28 ms |
24824 KB |
Output is correct |
29 |
Correct |
45 ms |
26788 KB |
Output is correct |
30 |
Correct |
29 ms |
24568 KB |
Output is correct |
31 |
Correct |
35 ms |
25336 KB |
Output is correct |
32 |
Correct |
31 ms |
24824 KB |
Output is correct |
33 |
Correct |
64 ms |
29832 KB |
Output is correct |
34 |
Correct |
46 ms |
26920 KB |
Output is correct |
35 |
Correct |
47 ms |
26532 KB |
Output is correct |
36 |
Correct |
25 ms |
24056 KB |
Output is correct |
37 |
Correct |
34 ms |
25464 KB |
Output is correct |
38 |
Correct |
59 ms |
28056 KB |
Output is correct |
39 |
Correct |
31 ms |
24828 KB |
Output is correct |
40 |
Correct |
34 ms |
25336 KB |
Output is correct |
41 |
Correct |
52 ms |
27168 KB |
Output is correct |
42 |
Correct |
34 ms |
24756 KB |
Output is correct |
43 |
Correct |
29 ms |
24952 KB |
Output is correct |
44 |
Correct |
33 ms |
25520 KB |
Output is correct |
45 |
Correct |
276 ms |
49808 KB |
Output is correct |
46 |
Correct |
166 ms |
37400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
23800 KB |
Output is correct |
2 |
Correct |
22 ms |
23772 KB |
Output is correct |
3 |
Correct |
22 ms |
23800 KB |
Output is correct |
4 |
Correct |
22 ms |
23800 KB |
Output is correct |
5 |
Correct |
22 ms |
23800 KB |
Output is correct |
6 |
Correct |
22 ms |
23800 KB |
Output is correct |
7 |
Correct |
23 ms |
23800 KB |
Output is correct |
8 |
Correct |
22 ms |
23800 KB |
Output is correct |
9 |
Correct |
22 ms |
23800 KB |
Output is correct |
10 |
Correct |
22 ms |
23800 KB |
Output is correct |
11 |
Correct |
22 ms |
23928 KB |
Output is correct |
12 |
Correct |
22 ms |
23928 KB |
Output is correct |
13 |
Correct |
22 ms |
23928 KB |
Output is correct |
14 |
Correct |
25 ms |
24184 KB |
Output is correct |
15 |
Correct |
23 ms |
24060 KB |
Output is correct |
16 |
Correct |
23 ms |
23800 KB |
Output is correct |
17 |
Correct |
24 ms |
24060 KB |
Output is correct |
18 |
Correct |
22 ms |
23928 KB |
Output is correct |
19 |
Correct |
22 ms |
23800 KB |
Output is correct |
20 |
Correct |
23 ms |
24056 KB |
Output is correct |
21 |
Correct |
22 ms |
23928 KB |
Output is correct |
22 |
Correct |
23 ms |
23928 KB |
Output is correct |
23 |
Correct |
24 ms |
24056 KB |
Output is correct |
24 |
Correct |
26 ms |
24216 KB |
Output is correct |
25 |
Correct |
24 ms |
23928 KB |
Output is correct |
26 |
Correct |
23 ms |
24056 KB |
Output is correct |
27 |
Correct |
23 ms |
24056 KB |
Output is correct |
28 |
Correct |
28 ms |
24824 KB |
Output is correct |
29 |
Correct |
44 ms |
26792 KB |
Output is correct |
30 |
Correct |
29 ms |
24624 KB |
Output is correct |
31 |
Correct |
35 ms |
25336 KB |
Output is correct |
32 |
Correct |
32 ms |
24824 KB |
Output is correct |
33 |
Correct |
64 ms |
29832 KB |
Output is correct |
34 |
Correct |
44 ms |
26792 KB |
Output is correct |
35 |
Correct |
47 ms |
26528 KB |
Output is correct |
36 |
Correct |
24 ms |
24056 KB |
Output is correct |
37 |
Correct |
34 ms |
25464 KB |
Output is correct |
38 |
Correct |
57 ms |
28192 KB |
Output is correct |
39 |
Correct |
32 ms |
24824 KB |
Output is correct |
40 |
Correct |
34 ms |
25180 KB |
Output is correct |
41 |
Correct |
52 ms |
27172 KB |
Output is correct |
42 |
Correct |
29 ms |
24828 KB |
Output is correct |
43 |
Correct |
30 ms |
24824 KB |
Output is correct |
44 |
Correct |
34 ms |
25496 KB |
Output is correct |
45 |
Correct |
275 ms |
49676 KB |
Output is correct |
46 |
Correct |
174 ms |
37428 KB |
Output is correct |
47 |
Correct |
37 ms |
25644 KB |
Output is correct |
48 |
Correct |
32 ms |
24824 KB |
Output is correct |
49 |
Correct |
31 ms |
24696 KB |
Output is correct |
50 |
Correct |
28 ms |
24680 KB |
Output is correct |
51 |
Correct |
55 ms |
27048 KB |
Output is correct |
52 |
Correct |
68 ms |
28200 KB |
Output is correct |
53 |
Correct |
37 ms |
25416 KB |
Output is correct |
54 |
Correct |
25 ms |
24572 KB |
Output is correct |
55 |
Correct |
27 ms |
24824 KB |
Output is correct |
56 |
Correct |
46 ms |
26804 KB |
Output is correct |
57 |
Correct |
23 ms |
24072 KB |
Output is correct |
58 |
Correct |
49 ms |
28068 KB |
Output is correct |
59 |
Correct |
44 ms |
27688 KB |
Output is correct |
60 |
Correct |
43 ms |
27688 KB |
Output is correct |
61 |
Correct |
42 ms |
26792 KB |
Output is correct |
62 |
Correct |
260 ms |
50056 KB |
Output is correct |
63 |
Execution timed out |
1069 ms |
74912 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |