#include<bits/stdc++.h>
using namespace std;
const int MAXN=3e4+2;
const int INF=2e9;
int n,m;
struct doge
{
int p,pos;
bool operator<(const doge& other)
{
if(pos!=other.pos)return pos<other.pos;
return p<=other.p;
}
}d[MAXN];
struct state
{
int pos,p,d;
bool operator<(state const& other)const
{
if(d!=other.d)return d>other.d;
return make_pair(p,pos)<make_pair(other.p,other.pos);
}
};
set<pair<int,int> >doges;
set<int>doges1[MAXN];
vector<int>states;
unordered_map<int,int>dist;
int main()
{
srand(65435);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>m;
for(int i=0;i<m;++i)
{
cin>>d[i].pos>>d[i].p;
doges.insert({d[i].p,d[i].pos%d[i].p});
doges1[d[i].pos].insert(d[i].p);
}
for(auto t:doges)
{
for(int i=t.second;i<n;i+=t.first)
{
states.push_back(i*MAXN+t.first);
}
}
for(auto s:states)
{
dist[s]=INF;
}
priority_queue<state>pq;
pq.push({d[0].pos,d[0].p,0});
dist[d[0].pos*MAXN+d[0].p]=0;
while(!pq.empty())
{
state u=pq.top();pq.pop();
if(dist[u.pos*MAXN+u.p]<u.d)continue;
if(u.pos==d[1].pos&&u.p==d[1].p)break;
doges1[u.pos].erase(u.p);
for(auto d1:doges1[u.pos])
{
if(dist[u.pos*MAXN+d1]>u.d)
{
dist[u.pos*MAXN+d1]=u.d;
pq.push({u.pos,d1,u.d});
}
}
state v=u;
if(v.pos-v.p>=0)
{
v.pos-=v.p;
if(dist[v.pos*MAXN+v.p]>u.d+1)
{
v.d=u.d+1;
dist[v.pos*MAXN+v.p]=v.d;
pq.push(v);
}
}
v=u;
if(v.pos+v.p<n)
{
v.pos+=v.p;
if(dist[v.pos*MAXN+v.p]>u.d+1)
{
v.d=u.d+1;
dist[v.pos*MAXN+v.p]=v.d;
pq.push(v);
}
}
}
if(dist[d[1].pos*MAXN+d[1].p]==INF)
{
cout<<-1<<"\n";
}
else cout<<dist[d[1].pos*MAXN+d[1].p]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1900 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1900 KB |
Output is correct |
11 |
Correct |
3 ms |
2156 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1772 KB |
Output is correct |
14 |
Correct |
5 ms |
2284 KB |
Output is correct |
15 |
Correct |
5 ms |
2284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
2 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1900 KB |
Output is correct |
11 |
Correct |
3 ms |
2156 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1772 KB |
Output is correct |
14 |
Correct |
5 ms |
2284 KB |
Output is correct |
15 |
Correct |
5 ms |
2284 KB |
Output is correct |
16 |
Correct |
2 ms |
2028 KB |
Output is correct |
17 |
Correct |
5 ms |
2668 KB |
Output is correct |
18 |
Correct |
3 ms |
2156 KB |
Output is correct |
19 |
Correct |
2 ms |
2028 KB |
Output is correct |
20 |
Correct |
2 ms |
2028 KB |
Output is correct |
21 |
Correct |
2 ms |
2028 KB |
Output is correct |
22 |
Correct |
2 ms |
2028 KB |
Output is correct |
23 |
Correct |
4 ms |
2156 KB |
Output is correct |
24 |
Correct |
6 ms |
2540 KB |
Output is correct |
25 |
Correct |
4 ms |
2412 KB |
Output is correct |
26 |
Correct |
3 ms |
2284 KB |
Output is correct |
27 |
Correct |
3 ms |
2156 KB |
Output is correct |
28 |
Correct |
7 ms |
3052 KB |
Output is correct |
29 |
Correct |
21 ms |
4468 KB |
Output is correct |
30 |
Correct |
6 ms |
2668 KB |
Output is correct |
31 |
Correct |
12 ms |
3688 KB |
Output is correct |
32 |
Correct |
8 ms |
2924 KB |
Output is correct |
33 |
Correct |
46 ms |
7164 KB |
Output is correct |
34 |
Correct |
34 ms |
7164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1900 KB |
Output is correct |
11 |
Correct |
3 ms |
2156 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1772 KB |
Output is correct |
14 |
Correct |
5 ms |
2284 KB |
Output is correct |
15 |
Correct |
5 ms |
2284 KB |
Output is correct |
16 |
Correct |
2 ms |
2028 KB |
Output is correct |
17 |
Correct |
5 ms |
2668 KB |
Output is correct |
18 |
Correct |
3 ms |
2156 KB |
Output is correct |
19 |
Correct |
2 ms |
2028 KB |
Output is correct |
20 |
Correct |
2 ms |
2028 KB |
Output is correct |
21 |
Correct |
2 ms |
2028 KB |
Output is correct |
22 |
Correct |
2 ms |
2028 KB |
Output is correct |
23 |
Correct |
3 ms |
2156 KB |
Output is correct |
24 |
Correct |
6 ms |
2540 KB |
Output is correct |
25 |
Correct |
4 ms |
2412 KB |
Output is correct |
26 |
Correct |
3 ms |
2284 KB |
Output is correct |
27 |
Correct |
3 ms |
2156 KB |
Output is correct |
28 |
Correct |
7 ms |
3052 KB |
Output is correct |
29 |
Correct |
21 ms |
4468 KB |
Output is correct |
30 |
Correct |
6 ms |
2668 KB |
Output is correct |
31 |
Correct |
12 ms |
3688 KB |
Output is correct |
32 |
Correct |
8 ms |
2924 KB |
Output is correct |
33 |
Correct |
45 ms |
7164 KB |
Output is correct |
34 |
Correct |
34 ms |
7164 KB |
Output is correct |
35 |
Correct |
61 ms |
9344 KB |
Output is correct |
36 |
Correct |
7 ms |
3180 KB |
Output is correct |
37 |
Correct |
59 ms |
12164 KB |
Output is correct |
38 |
Correct |
69 ms |
12816 KB |
Output is correct |
39 |
Correct |
62 ms |
12424 KB |
Output is correct |
40 |
Correct |
63 ms |
12556 KB |
Output is correct |
41 |
Correct |
88 ms |
13064 KB |
Output is correct |
42 |
Correct |
7 ms |
2412 KB |
Output is correct |
43 |
Correct |
7 ms |
2284 KB |
Output is correct |
44 |
Correct |
6 ms |
2156 KB |
Output is correct |
45 |
Correct |
338 ms |
26504 KB |
Output is correct |
46 |
Correct |
277 ms |
26508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1772 KB |
Output is correct |
2 |
Correct |
1 ms |
1772 KB |
Output is correct |
3 |
Correct |
1 ms |
1772 KB |
Output is correct |
4 |
Correct |
2 ms |
1772 KB |
Output is correct |
5 |
Correct |
1 ms |
1772 KB |
Output is correct |
6 |
Correct |
1 ms |
1772 KB |
Output is correct |
7 |
Correct |
1 ms |
1772 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1772 KB |
Output is correct |
10 |
Correct |
2 ms |
1900 KB |
Output is correct |
11 |
Correct |
3 ms |
2156 KB |
Output is correct |
12 |
Correct |
2 ms |
1772 KB |
Output is correct |
13 |
Correct |
2 ms |
1772 KB |
Output is correct |
14 |
Correct |
5 ms |
2284 KB |
Output is correct |
15 |
Correct |
5 ms |
2284 KB |
Output is correct |
16 |
Correct |
2 ms |
2028 KB |
Output is correct |
17 |
Correct |
5 ms |
2668 KB |
Output is correct |
18 |
Correct |
3 ms |
2156 KB |
Output is correct |
19 |
Correct |
2 ms |
2028 KB |
Output is correct |
20 |
Correct |
2 ms |
2028 KB |
Output is correct |
21 |
Correct |
2 ms |
2028 KB |
Output is correct |
22 |
Correct |
3 ms |
2028 KB |
Output is correct |
23 |
Correct |
4 ms |
2156 KB |
Output is correct |
24 |
Correct |
6 ms |
2540 KB |
Output is correct |
25 |
Correct |
4 ms |
2412 KB |
Output is correct |
26 |
Correct |
3 ms |
2284 KB |
Output is correct |
27 |
Correct |
3 ms |
2156 KB |
Output is correct |
28 |
Correct |
9 ms |
3052 KB |
Output is correct |
29 |
Correct |
21 ms |
4468 KB |
Output is correct |
30 |
Correct |
8 ms |
2668 KB |
Output is correct |
31 |
Correct |
12 ms |
3688 KB |
Output is correct |
32 |
Correct |
9 ms |
2924 KB |
Output is correct |
33 |
Correct |
48 ms |
7164 KB |
Output is correct |
34 |
Correct |
35 ms |
7164 KB |
Output is correct |
35 |
Correct |
67 ms |
9344 KB |
Output is correct |
36 |
Correct |
7 ms |
3180 KB |
Output is correct |
37 |
Correct |
61 ms |
12164 KB |
Output is correct |
38 |
Correct |
72 ms |
12808 KB |
Output is correct |
39 |
Correct |
56 ms |
12424 KB |
Output is correct |
40 |
Correct |
67 ms |
12556 KB |
Output is correct |
41 |
Correct |
95 ms |
13192 KB |
Output is correct |
42 |
Correct |
7 ms |
2412 KB |
Output is correct |
43 |
Correct |
8 ms |
2284 KB |
Output is correct |
44 |
Correct |
7 ms |
2156 KB |
Output is correct |
45 |
Correct |
348 ms |
26504 KB |
Output is correct |
46 |
Correct |
293 ms |
26632 KB |
Output is correct |
47 |
Correct |
276 ms |
39024 KB |
Output is correct |
48 |
Correct |
79 ms |
14348 KB |
Output is correct |
49 |
Correct |
57 ms |
11364 KB |
Output is correct |
50 |
Correct |
47 ms |
10720 KB |
Output is correct |
51 |
Correct |
151 ms |
20232 KB |
Output is correct |
52 |
Correct |
190 ms |
21256 KB |
Output is correct |
53 |
Correct |
40 ms |
8956 KB |
Output is correct |
54 |
Correct |
7 ms |
3100 KB |
Output is correct |
55 |
Correct |
9 ms |
4220 KB |
Output is correct |
56 |
Correct |
15 ms |
4716 KB |
Output is correct |
57 |
Correct |
102 ms |
21260 KB |
Output is correct |
58 |
Correct |
21 ms |
5612 KB |
Output is correct |
59 |
Correct |
26 ms |
6404 KB |
Output is correct |
60 |
Correct |
34 ms |
7428 KB |
Output is correct |
61 |
Correct |
59 ms |
7164 KB |
Output is correct |
62 |
Correct |
283 ms |
26248 KB |
Output is correct |
63 |
Execution timed out |
1097 ms |
131860 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |