#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a); i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"debug"<<endl
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int mxN = 30001;
ttgl arr[mxN];
set<int> st[mxN];
vector<ttgl> adj[mxN];
int dist[mxN];
int n, m;
int main()
{
//freopen("filename.in", "r", stdin);
//freopen("filename.out", "w", stdout);
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m;
owo(i, 0, m) {
cin>>arr[i].first>>arr[i].second;
st[arr[i].first].insert(arr[i].second);
}
owo(i, 0, n) {
auto it = st[i].begin();
while(it!=st[i].end()) {
int pos = i;
int diff = *it;
while(pos+diff<n) {
adj[i].senpai({pos+diff, (pos+diff-i)/diff});
if(st[pos+diff].count(diff)) break;
pos+=diff;
}
pos = i;
while(pos-diff>=0) {
adj[i].senpai({pos-diff, (i-pos+diff)/diff});
if(st[pos-diff].count(diff))break;
pos-=diff;
}
it++;
}
}
memset(dist, INF, sizeof(dist));
int start = arr[0].first;
priority_queue<ttgl, vector<ttgl>, greater<ttgl>> pq;
pq.push({0, start});
dist[start] = 0;
while(!pq.empty()) {
auto u = pq.top();
pq.pop();
if(u.first!=dist[u.second])continue;
for(auto nxt: adj[u.second]) {
if(dist[u.second]+nxt.second<dist[nxt.first]) {
dist[nxt.first] = dist[u.second]+nxt.second;
pq.push({dist[nxt.first], nxt.first});
}
}
}
if(dist[arr[1].first]==INF) {
cout<<"-1\n";
}else {
cout<<dist[arr[1].first]<<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2560 KB |
Output is correct |
2 |
Correct |
6 ms |
2432 KB |
Output is correct |
3 |
Correct |
6 ms |
2560 KB |
Output is correct |
4 |
Correct |
6 ms |
2432 KB |
Output is correct |
5 |
Correct |
6 ms |
2560 KB |
Output is correct |
6 |
Correct |
6 ms |
2516 KB |
Output is correct |
7 |
Correct |
6 ms |
2560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2560 KB |
Output is correct |
2 |
Correct |
6 ms |
2560 KB |
Output is correct |
3 |
Correct |
6 ms |
2560 KB |
Output is correct |
4 |
Correct |
7 ms |
2560 KB |
Output is correct |
5 |
Correct |
6 ms |
2560 KB |
Output is correct |
6 |
Correct |
6 ms |
2560 KB |
Output is correct |
7 |
Correct |
6 ms |
2560 KB |
Output is correct |
8 |
Correct |
6 ms |
2560 KB |
Output is correct |
9 |
Correct |
6 ms |
2560 KB |
Output is correct |
10 |
Correct |
6 ms |
2560 KB |
Output is correct |
11 |
Correct |
7 ms |
2816 KB |
Output is correct |
12 |
Correct |
6 ms |
2560 KB |
Output is correct |
13 |
Correct |
6 ms |
2560 KB |
Output is correct |
14 |
Correct |
7 ms |
2560 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2560 KB |
Output is correct |
2 |
Correct |
6 ms |
2560 KB |
Output is correct |
3 |
Correct |
6 ms |
2560 KB |
Output is correct |
4 |
Correct |
6 ms |
2560 KB |
Output is correct |
5 |
Correct |
6 ms |
2560 KB |
Output is correct |
6 |
Correct |
6 ms |
2560 KB |
Output is correct |
7 |
Correct |
6 ms |
2560 KB |
Output is correct |
8 |
Correct |
6 ms |
2560 KB |
Output is correct |
9 |
Correct |
6 ms |
2560 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2560 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
8 ms |
2688 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
6 ms |
2688 KB |
Output is correct |
17 |
Correct |
8 ms |
2816 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
20 |
Correct |
7 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2688 KB |
Output is correct |
22 |
Correct |
7 ms |
2688 KB |
Output is correct |
23 |
Correct |
7 ms |
2688 KB |
Output is correct |
24 |
Correct |
8 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
7 ms |
2688 KB |
Output is correct |
27 |
Correct |
7 ms |
2688 KB |
Output is correct |
28 |
Correct |
8 ms |
2944 KB |
Output is correct |
29 |
Correct |
9 ms |
3456 KB |
Output is correct |
30 |
Correct |
8 ms |
2944 KB |
Output is correct |
31 |
Correct |
7 ms |
3072 KB |
Output is correct |
32 |
Correct |
7 ms |
2944 KB |
Output is correct |
33 |
Correct |
12 ms |
4220 KB |
Output is correct |
34 |
Correct |
12 ms |
4220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2560 KB |
Output is correct |
2 |
Correct |
6 ms |
2560 KB |
Output is correct |
3 |
Correct |
6 ms |
2560 KB |
Output is correct |
4 |
Correct |
6 ms |
2560 KB |
Output is correct |
5 |
Correct |
6 ms |
2560 KB |
Output is correct |
6 |
Correct |
6 ms |
2560 KB |
Output is correct |
7 |
Correct |
6 ms |
2560 KB |
Output is correct |
8 |
Correct |
6 ms |
2560 KB |
Output is correct |
9 |
Correct |
6 ms |
2560 KB |
Output is correct |
10 |
Correct |
6 ms |
2560 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2560 KB |
Output is correct |
13 |
Correct |
6 ms |
2560 KB |
Output is correct |
14 |
Correct |
7 ms |
2688 KB |
Output is correct |
15 |
Correct |
7 ms |
2688 KB |
Output is correct |
16 |
Correct |
6 ms |
2688 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
8 ms |
2688 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
20 |
Correct |
6 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2688 KB |
Output is correct |
22 |
Correct |
6 ms |
2688 KB |
Output is correct |
23 |
Correct |
7 ms |
2688 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
8 ms |
2816 KB |
Output is correct |
26 |
Correct |
7 ms |
2688 KB |
Output is correct |
27 |
Correct |
6 ms |
2688 KB |
Output is correct |
28 |
Correct |
7 ms |
2944 KB |
Output is correct |
29 |
Correct |
9 ms |
3456 KB |
Output is correct |
30 |
Correct |
7 ms |
2944 KB |
Output is correct |
31 |
Correct |
7 ms |
3072 KB |
Output is correct |
32 |
Correct |
7 ms |
2944 KB |
Output is correct |
33 |
Correct |
11 ms |
4220 KB |
Output is correct |
34 |
Correct |
12 ms |
4220 KB |
Output is correct |
35 |
Correct |
25 ms |
5248 KB |
Output is correct |
36 |
Correct |
9 ms |
3072 KB |
Output is correct |
37 |
Correct |
24 ms |
5888 KB |
Output is correct |
38 |
Correct |
28 ms |
6272 KB |
Output is correct |
39 |
Correct |
30 ms |
6272 KB |
Output is correct |
40 |
Correct |
32 ms |
6240 KB |
Output is correct |
41 |
Correct |
29 ms |
6272 KB |
Output is correct |
42 |
Correct |
12 ms |
3196 KB |
Output is correct |
43 |
Correct |
11 ms |
3072 KB |
Output is correct |
44 |
Correct |
11 ms |
3200 KB |
Output is correct |
45 |
Correct |
33 ms |
8960 KB |
Output is correct |
46 |
Correct |
30 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2560 KB |
Output is correct |
2 |
Correct |
6 ms |
2560 KB |
Output is correct |
3 |
Correct |
6 ms |
2560 KB |
Output is correct |
4 |
Correct |
6 ms |
2560 KB |
Output is correct |
5 |
Correct |
8 ms |
2560 KB |
Output is correct |
6 |
Correct |
6 ms |
2560 KB |
Output is correct |
7 |
Correct |
7 ms |
2560 KB |
Output is correct |
8 |
Correct |
6 ms |
2560 KB |
Output is correct |
9 |
Correct |
6 ms |
2560 KB |
Output is correct |
10 |
Correct |
6 ms |
2560 KB |
Output is correct |
11 |
Correct |
7 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2560 KB |
Output is correct |
13 |
Correct |
6 ms |
2560 KB |
Output is correct |
14 |
Correct |
8 ms |
2688 KB |
Output is correct |
15 |
Correct |
7 ms |
2688 KB |
Output is correct |
16 |
Correct |
6 ms |
2688 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
20 |
Correct |
7 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2688 KB |
Output is correct |
22 |
Correct |
6 ms |
2688 KB |
Output is correct |
23 |
Correct |
7 ms |
2688 KB |
Output is correct |
24 |
Correct |
8 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
6 ms |
2688 KB |
Output is correct |
27 |
Correct |
6 ms |
2688 KB |
Output is correct |
28 |
Correct |
8 ms |
2944 KB |
Output is correct |
29 |
Correct |
10 ms |
3516 KB |
Output is correct |
30 |
Correct |
7 ms |
2944 KB |
Output is correct |
31 |
Correct |
8 ms |
3072 KB |
Output is correct |
32 |
Correct |
7 ms |
2944 KB |
Output is correct |
33 |
Correct |
12 ms |
4220 KB |
Output is correct |
34 |
Correct |
13 ms |
4220 KB |
Output is correct |
35 |
Correct |
24 ms |
5248 KB |
Output is correct |
36 |
Correct |
8 ms |
3072 KB |
Output is correct |
37 |
Correct |
25 ms |
5888 KB |
Output is correct |
38 |
Correct |
28 ms |
6264 KB |
Output is correct |
39 |
Correct |
28 ms |
6272 KB |
Output is correct |
40 |
Correct |
28 ms |
6272 KB |
Output is correct |
41 |
Correct |
28 ms |
6272 KB |
Output is correct |
42 |
Correct |
11 ms |
3072 KB |
Output is correct |
43 |
Correct |
11 ms |
3200 KB |
Output is correct |
44 |
Correct |
13 ms |
3164 KB |
Output is correct |
45 |
Correct |
29 ms |
8960 KB |
Output is correct |
46 |
Correct |
32 ms |
8960 KB |
Output is correct |
47 |
Correct |
54 ms |
13300 KB |
Output is correct |
48 |
Correct |
23 ms |
6396 KB |
Output is correct |
49 |
Correct |
19 ms |
5376 KB |
Output is correct |
50 |
Correct |
16 ms |
4992 KB |
Output is correct |
51 |
Correct |
47 ms |
8452 KB |
Output is correct |
52 |
Correct |
48 ms |
8952 KB |
Output is correct |
53 |
Correct |
25 ms |
6140 KB |
Output is correct |
54 |
Correct |
8 ms |
3072 KB |
Output is correct |
55 |
Correct |
9 ms |
3452 KB |
Output is correct |
56 |
Correct |
18 ms |
5376 KB |
Output is correct |
57 |
Correct |
47 ms |
11644 KB |
Output is correct |
58 |
Correct |
14 ms |
3960 KB |
Output is correct |
59 |
Correct |
17 ms |
4348 KB |
Output is correct |
60 |
Correct |
17 ms |
5116 KB |
Output is correct |
61 |
Correct |
18 ms |
4480 KB |
Output is correct |
62 |
Correct |
30 ms |
9724 KB |
Output is correct |
63 |
Correct |
84 ms |
26968 KB |
Output is correct |
64 |
Correct |
95 ms |
30188 KB |
Output is correct |
65 |
Correct |
127 ms |
39792 KB |
Output is correct |
66 |
Correct |
223 ms |
68064 KB |
Output is correct |
67 |
Correct |
222 ms |
68064 KB |
Output is correct |