#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e4 + 21, MOD = (int)1e9+7;
short n,m,b[N];
int ft[N];
vector<pair<int,int>> g[N * 2];
vector<pair<short,short>> x[N];
void add(int v,int u,int w) {
g[v].push_back({u,w});
}
bool sgn(int x) {
return (x < n);
}
int d[N];
void test() {
cin >> n >> m;
for(int i = 0;i < m;i++) {
int p;
cin >> b[i] >> p;
int d = b[i] % p;
x[p].emplace_back(d,i);
}
int it = n;
for(int i = 1;i < n;i++){
sort(x[i].begin(),x[i].end());
for(int j = 0;j < (int)x[i].size();j++){
vector<int> f;
for(int k = j;k < (int)x[i].size() && x[i][k].first == x[i][j].first;k++){
j = k;
f.push_back(b[x[i][k].second]);
}
sort(f.begin(),f.end());
for(int t = 0;t < (int)f.size();t++){
int prev = (t ? f[t - 1] : 0);
for(int k = f[t],_=0;k >= prev;k -= i){
g[f[t]].push_back({k,_});
_++;
}
int nx = (t < (int)f.size() - 1 ? f[t + 1] : n - 1);
for(int k = f[t],_=0;k <= nx;k += i){
g[f[t]].push_back({k,_});
_++;
}
}
}
x[i].clear();
}
set<pair<int,int>> st;
for(int i = 0;i <= it;i++){
d[i] = 1e9;
}
st.insert({0,b[0]});
d[b[0]] = 0;
while(!st.empty()){
auto [c,v] = (*st.begin());
st.erase(st.begin());
for(auto [to,w]:g[v]){
if(d[to] > d[v] + w){
st.erase({d[to],to});
d[to] = d[v] + w;
st.insert({d[to],to});
}
}
}
if(d[b[1]] == 1e9){
d[b[1]] = -1;
}
cout << d[b[1]];
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
2 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2852 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2648 KB |
Output is correct |
25 |
Correct |
2 ms |
2648 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
3340 KB |
Output is correct |
30 |
Correct |
2 ms |
2840 KB |
Output is correct |
31 |
Correct |
3 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
5 ms |
3676 KB |
Output is correct |
34 |
Correct |
5 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2652 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2648 KB |
Output is correct |
15 |
Correct |
1 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2652 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2704 KB |
Output is correct |
24 |
Correct |
2 ms |
2856 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
3164 KB |
Output is correct |
30 |
Correct |
2 ms |
2840 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
6 ms |
3676 KB |
Output is correct |
34 |
Correct |
5 ms |
3932 KB |
Output is correct |
35 |
Correct |
7 ms |
4184 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
9 ms |
5212 KB |
Output is correct |
38 |
Correct |
10 ms |
5208 KB |
Output is correct |
39 |
Correct |
9 ms |
5212 KB |
Output is correct |
40 |
Correct |
9 ms |
5156 KB |
Output is correct |
41 |
Correct |
10 ms |
5388 KB |
Output is correct |
42 |
Correct |
5 ms |
3544 KB |
Output is correct |
43 |
Correct |
5 ms |
3508 KB |
Output is correct |
44 |
Correct |
6 ms |
3544 KB |
Output is correct |
45 |
Correct |
14 ms |
7516 KB |
Output is correct |
46 |
Correct |
14 ms |
7836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2472 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
2 ms |
2652 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Correct |
2 ms |
2652 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
2 ms |
2552 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
3160 KB |
Output is correct |
29 |
Correct |
4 ms |
3164 KB |
Output is correct |
30 |
Correct |
2 ms |
2884 KB |
Output is correct |
31 |
Correct |
3 ms |
3036 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
6 ms |
3908 KB |
Output is correct |
34 |
Correct |
6 ms |
3940 KB |
Output is correct |
35 |
Correct |
7 ms |
4200 KB |
Output is correct |
36 |
Correct |
3 ms |
2920 KB |
Output is correct |
37 |
Correct |
8 ms |
5224 KB |
Output is correct |
38 |
Correct |
10 ms |
5224 KB |
Output is correct |
39 |
Correct |
10 ms |
5380 KB |
Output is correct |
40 |
Correct |
10 ms |
5408 KB |
Output is correct |
41 |
Correct |
11 ms |
5224 KB |
Output is correct |
42 |
Correct |
6 ms |
3556 KB |
Output is correct |
43 |
Correct |
5 ms |
3556 KB |
Output is correct |
44 |
Correct |
5 ms |
3556 KB |
Output is correct |
45 |
Correct |
14 ms |
7748 KB |
Output is correct |
46 |
Correct |
14 ms |
7784 KB |
Output is correct |
47 |
Correct |
29 ms |
12388 KB |
Output is correct |
48 |
Correct |
12 ms |
5860 KB |
Output is correct |
49 |
Correct |
8 ms |
5224 KB |
Output is correct |
50 |
Correct |
8 ms |
4968 KB |
Output is correct |
51 |
Correct |
37 ms |
8844 KB |
Output is correct |
52 |
Correct |
42 ms |
9112 KB |
Output is correct |
53 |
Correct |
18 ms |
6096 KB |
Output is correct |
54 |
Correct |
3 ms |
3420 KB |
Output is correct |
55 |
Correct |
4 ms |
3932 KB |
Output is correct |
56 |
Correct |
9 ms |
4444 KB |
Output is correct |
57 |
Correct |
37 ms |
11624 KB |
Output is correct |
58 |
Correct |
9 ms |
4952 KB |
Output is correct |
59 |
Correct |
10 ms |
5216 KB |
Output is correct |
60 |
Correct |
11 ms |
5968 KB |
Output is correct |
61 |
Correct |
10 ms |
5168 KB |
Output is correct |
62 |
Correct |
14 ms |
8916 KB |
Output is correct |
63 |
Correct |
65 ms |
26032 KB |
Output is correct |
64 |
Correct |
72 ms |
33116 KB |
Output is correct |
65 |
Correct |
93 ms |
43848 KB |
Output is correct |
66 |
Correct |
178 ms |
68060 KB |
Output is correct |
67 |
Correct |
182 ms |
68056 KB |
Output is correct |