#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define pp push
#define MOD 1000000007
#define INF 1e18
#define N 30005
#define M 5000000
typedef long long ll;
const ll c=174;
using namespace std;
ll b[N],p[N],path[N];
ll pos[N];
map<ll,bool>pp[N];
vector<pair<ll,ll> >adj[N];
bool vis[N],visited[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset(path,-1,sizeof(path));
ll n,m;
cin >> n >> m;
for(ll i=1;i<=m;i++){
cin >> b[i] >> p[i];
if(pp[b[i]][p[i]]!=0){
vis[i]=1;
}
else{
pos[b[i]]++;
pp[b[i]][p[i]]=1;
}
}
for(ll i=1;i<=m;i++){
if(vis[i]==1) continue;
for(ll j=b[i]+p[i];j<n;j+=p[i]){
if(pos[j]) adj[b[i]].pb({j,(j-b[i])/p[i]});
if(pp[j][p[i]]==1) break;
}
for(ll j=b[i]-p[i];j>=0;j-=p[i]){
if(pos[j]) adj[b[i]].pb({j,(b[i]-j)/p[i]});
if(pp[j][p[i]]==1) break;
}
}
priority_queue<pair<ll,ll> >pq;
pq.push({0,b[1]});
while(!pq.empty()){
ll cost=-pq.top().ff;
ll doge_id=pq.top().ss;
// cout << doge_id <<" " << cost << '\n';
path[doge_id]=cost;
pq.pop();
visited[doge_id]=1;
for(pair<int,int>i:adj[doge_id]){
pq.push({-(cost+i.ss),i.ff});
}
while(!pq.empty() && visited[pq.top().ss]==1) pq.pop();
}
cout << path[b[2]];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2924 KB |
Output is correct |
11 |
Correct |
4 ms |
3180 KB |
Output is correct |
12 |
Correct |
3 ms |
2796 KB |
Output is correct |
13 |
Correct |
2 ms |
2812 KB |
Output is correct |
14 |
Correct |
4 ms |
3180 KB |
Output is correct |
15 |
Correct |
4 ms |
3180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2924 KB |
Output is correct |
11 |
Correct |
4 ms |
3180 KB |
Output is correct |
12 |
Correct |
2 ms |
2796 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
14 |
Correct |
4 ms |
3180 KB |
Output is correct |
15 |
Correct |
4 ms |
3180 KB |
Output is correct |
16 |
Correct |
4 ms |
3052 KB |
Output is correct |
17 |
Correct |
8 ms |
4336 KB |
Output is correct |
18 |
Correct |
3 ms |
3180 KB |
Output is correct |
19 |
Correct |
3 ms |
3052 KB |
Output is correct |
20 |
Correct |
3 ms |
2924 KB |
Output is correct |
21 |
Correct |
3 ms |
2924 KB |
Output is correct |
22 |
Correct |
4 ms |
3180 KB |
Output is correct |
23 |
Correct |
4 ms |
3180 KB |
Output is correct |
24 |
Correct |
6 ms |
3820 KB |
Output is correct |
25 |
Correct |
4 ms |
3436 KB |
Output is correct |
26 |
Correct |
3 ms |
3308 KB |
Output is correct |
27 |
Correct |
3 ms |
3180 KB |
Output is correct |
28 |
Correct |
8 ms |
4716 KB |
Output is correct |
29 |
Correct |
16 ms |
6508 KB |
Output is correct |
30 |
Correct |
5 ms |
3820 KB |
Output is correct |
31 |
Correct |
8 ms |
4844 KB |
Output is correct |
32 |
Correct |
6 ms |
4204 KB |
Output is correct |
33 |
Correct |
41 ms |
10348 KB |
Output is correct |
34 |
Correct |
40 ms |
10348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2700 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2668 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
2 ms |
2924 KB |
Output is correct |
11 |
Correct |
4 ms |
3180 KB |
Output is correct |
12 |
Correct |
2 ms |
2796 KB |
Output is correct |
13 |
Correct |
3 ms |
2796 KB |
Output is correct |
14 |
Correct |
4 ms |
3180 KB |
Output is correct |
15 |
Correct |
4 ms |
3180 KB |
Output is correct |
16 |
Correct |
3 ms |
3052 KB |
Output is correct |
17 |
Correct |
8 ms |
4336 KB |
Output is correct |
18 |
Correct |
4 ms |
3180 KB |
Output is correct |
19 |
Correct |
3 ms |
3052 KB |
Output is correct |
20 |
Correct |
3 ms |
2924 KB |
Output is correct |
21 |
Correct |
3 ms |
3072 KB |
Output is correct |
22 |
Correct |
3 ms |
3180 KB |
Output is correct |
23 |
Correct |
4 ms |
3180 KB |
Output is correct |
24 |
Correct |
6 ms |
3820 KB |
Output is correct |
25 |
Correct |
4 ms |
3436 KB |
Output is correct |
26 |
Correct |
4 ms |
3180 KB |
Output is correct |
27 |
Correct |
3 ms |
3180 KB |
Output is correct |
28 |
Correct |
8 ms |
4716 KB |
Output is correct |
29 |
Correct |
15 ms |
6508 KB |
Output is correct |
30 |
Correct |
5 ms |
3820 KB |
Output is correct |
31 |
Correct |
10 ms |
4844 KB |
Output is correct |
32 |
Correct |
6 ms |
4204 KB |
Output is correct |
33 |
Correct |
40 ms |
10348 KB |
Output is correct |
34 |
Correct |
39 ms |
10348 KB |
Output is correct |
35 |
Correct |
66 ms |
13664 KB |
Output is correct |
36 |
Correct |
9 ms |
4588 KB |
Output is correct |
37 |
Correct |
138 ms |
21988 KB |
Output is correct |
38 |
Correct |
130 ms |
21104 KB |
Output is correct |
39 |
Correct |
123 ms |
18916 KB |
Output is correct |
40 |
Correct |
127 ms |
18784 KB |
Output is correct |
41 |
Correct |
121 ms |
18784 KB |
Output is correct |
42 |
Correct |
7 ms |
3692 KB |
Output is correct |
43 |
Correct |
7 ms |
3564 KB |
Output is correct |
44 |
Correct |
7 ms |
3436 KB |
Output is correct |
45 |
Correct |
378 ms |
34796 KB |
Output is correct |
46 |
Correct |
392 ms |
34664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
2 ms |
2688 KB |
Output is correct |
8 |
Correct |
2 ms |
2668 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2924 KB |
Output is correct |
11 |
Correct |
4 ms |
3180 KB |
Output is correct |
12 |
Correct |
2 ms |
2796 KB |
Output is correct |
13 |
Correct |
3 ms |
2796 KB |
Output is correct |
14 |
Correct |
4 ms |
3180 KB |
Output is correct |
15 |
Correct |
4 ms |
3180 KB |
Output is correct |
16 |
Correct |
3 ms |
3052 KB |
Output is correct |
17 |
Correct |
8 ms |
4332 KB |
Output is correct |
18 |
Correct |
3 ms |
3180 KB |
Output is correct |
19 |
Correct |
3 ms |
3052 KB |
Output is correct |
20 |
Correct |
3 ms |
2924 KB |
Output is correct |
21 |
Correct |
3 ms |
2924 KB |
Output is correct |
22 |
Correct |
3 ms |
3180 KB |
Output is correct |
23 |
Correct |
4 ms |
3200 KB |
Output is correct |
24 |
Correct |
6 ms |
3832 KB |
Output is correct |
25 |
Correct |
4 ms |
3436 KB |
Output is correct |
26 |
Correct |
4 ms |
3180 KB |
Output is correct |
27 |
Correct |
3 ms |
3180 KB |
Output is correct |
28 |
Correct |
8 ms |
4716 KB |
Output is correct |
29 |
Correct |
15 ms |
6508 KB |
Output is correct |
30 |
Correct |
5 ms |
3820 KB |
Output is correct |
31 |
Correct |
8 ms |
4844 KB |
Output is correct |
32 |
Correct |
6 ms |
4352 KB |
Output is correct |
33 |
Correct |
43 ms |
10348 KB |
Output is correct |
34 |
Correct |
43 ms |
10348 KB |
Output is correct |
35 |
Correct |
72 ms |
13664 KB |
Output is correct |
36 |
Correct |
9 ms |
4608 KB |
Output is correct |
37 |
Correct |
145 ms |
21988 KB |
Output is correct |
38 |
Correct |
127 ms |
20828 KB |
Output is correct |
39 |
Correct |
122 ms |
18916 KB |
Output is correct |
40 |
Correct |
121 ms |
18784 KB |
Output is correct |
41 |
Correct |
125 ms |
18932 KB |
Output is correct |
42 |
Correct |
7 ms |
3692 KB |
Output is correct |
43 |
Correct |
9 ms |
3564 KB |
Output is correct |
44 |
Correct |
7 ms |
3436 KB |
Output is correct |
45 |
Correct |
375 ms |
34536 KB |
Output is correct |
46 |
Correct |
387 ms |
34536 KB |
Output is correct |
47 |
Correct |
786 ms |
80212 KB |
Output is correct |
48 |
Correct |
107 ms |
20716 KB |
Output is correct |
49 |
Correct |
72 ms |
13804 KB |
Output is correct |
50 |
Correct |
53 ms |
13292 KB |
Output is correct |
51 |
Correct |
204 ms |
29280 KB |
Output is correct |
52 |
Correct |
236 ms |
33892 KB |
Output is correct |
53 |
Correct |
35 ms |
11120 KB |
Output is correct |
54 |
Correct |
5 ms |
4588 KB |
Output is correct |
55 |
Correct |
7 ms |
6124 KB |
Output is correct |
56 |
Correct |
22 ms |
6764 KB |
Output is correct |
57 |
Correct |
240 ms |
31980 KB |
Output is correct |
58 |
Correct |
13 ms |
7404 KB |
Output is correct |
59 |
Correct |
20 ms |
9196 KB |
Output is correct |
60 |
Correct |
35 ms |
11244 KB |
Output is correct |
61 |
Correct |
27 ms |
10220 KB |
Output is correct |
62 |
Correct |
345 ms |
44816 KB |
Output is correct |
63 |
Execution timed out |
1083 ms |
93804 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |