#include <bits/stdc++.h>
#define ll long long
using namespace std;
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<" - ", _do(__VA_ARGS__)
template<typename T> void _do(T && x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y){cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define bug(...)
#define IOS() ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#endif // BALBIT
#define pii pair<int, int>
#define f first
#define s second
#define ALL(x) x.begin(), x.end()
#define SZ(x) (int)(x.size())
#define pb push_back
const int maxn = 3e4+5;
vector<pii> g[maxn];
int d[maxn];
set<pii> bp;
set<int> hv[maxn];
signed main(){
IOS();
int n,m; cin>>n>>m;
int S = -1, T = -1;
for (int i = 0; i<m; ++i) {
int b,p; cin>>b>>p;
if (bp.count({b,p})) continue;
bp.insert({b,p});
if (i == 0) S = b;
if (i == 1) T = b;
hv[b].insert(p);
}
for(int b = 0; b<n; ++b){
for (int p : hv[b]) {
for (int df = 1; b + df * p < n; ++df ) {
g[b].pb({b+df*p, abs(df)});
if (hv[b+df*p].count(p)) break;
}
for (int df = -1; b + df * p >=0; --df ) {
g[b].pb({b+df*p, abs(df)});
if (hv[b+df*p].count(p)) break;
}
}
}
memset(d, 0x3f, sizeof d);
d[S] = 0;
priority_queue<pii, vector<pii>, greater<pii> > pq;
pq.push({0,S});
while (!pq.empty()) {
int v = pq.top().s; int w = pq.top().f; pq.pop();
bug(v, d[v]);
if (d[v] != w) {
continue;
}
for (pii u : g[v]) {
if (d[u.f] > d[v] + u.s) {
d[u.f] = d[v] + u.s;
pq.push({d[u.f], u.f});
}
}
}
if (d[T] != 0x3f3f3f3f)
cout<<d[T]<<endl;
else cout<<-1<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2560 KB |
Output is correct |
2 |
Correct |
6 ms |
2560 KB |
Output is correct |
3 |
Correct |
5 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
2816 KB |
Output is correct |
12 |
Correct |
6 ms |
2560 KB |
Output is correct |
13 |
Correct |
6 ms |
2592 KB |
Output is correct |
14 |
Correct |
7 ms |
2816 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 |
2688 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 |
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 |
2816 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 |
2944 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 |
7 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 |
2944 KB |
Output is correct |
25 |
Correct |
8 ms |
2944 KB |
Output is correct |
26 |
Correct |
6 ms |
2688 KB |
Output is correct |
27 |
Correct |
6 ms |
2688 KB |
Output is correct |
28 |
Correct |
9 ms |
3072 KB |
Output is correct |
29 |
Correct |
9 ms |
3456 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 |
4224 KB |
Output is correct |
34 |
Correct |
12 ms |
4224 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 |
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 |
2816 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 |
2944 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 |
2816 KB |
Output is correct |
24 |
Correct |
8 ms |
2944 KB |
Output is correct |
25 |
Correct |
8 ms |
2944 KB |
Output is correct |
26 |
Correct |
6 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 |
10 ms |
3456 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 |
4224 KB |
Output is correct |
34 |
Correct |
12 ms |
4224 KB |
Output is correct |
35 |
Correct |
34 ms |
6016 KB |
Output is correct |
36 |
Correct |
10 ms |
3200 KB |
Output is correct |
37 |
Correct |
34 ms |
6520 KB |
Output is correct |
38 |
Correct |
41 ms |
7168 KB |
Output is correct |
39 |
Correct |
43 ms |
7160 KB |
Output is correct |
40 |
Correct |
42 ms |
7160 KB |
Output is correct |
41 |
Correct |
42 ms |
7160 KB |
Output is correct |
42 |
Correct |
10 ms |
2688 KB |
Output is correct |
43 |
Correct |
10 ms |
2688 KB |
Output is correct |
44 |
Correct |
12 ms |
2816 KB |
Output is correct |
45 |
Correct |
43 ms |
9984 KB |
Output is correct |
46 |
Correct |
44 ms |
9976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2556 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 |
2816 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
7 ms |
2816 KB |
Output is correct |
15 |
Correct |
8 ms |
2816 KB |
Output is correct |
16 |
Correct |
6 ms |
2688 KB |
Output is correct |
17 |
Correct |
8 ms |
2944 KB |
Output is correct |
18 |
Correct |
9 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 |
7 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 |
2944 KB |
Output is correct |
25 |
Correct |
8 ms |
2944 KB |
Output is correct |
26 |
Correct |
6 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 |
8 ms |
3072 KB |
Output is correct |
32 |
Correct |
7 ms |
2944 KB |
Output is correct |
33 |
Correct |
12 ms |
4224 KB |
Output is correct |
34 |
Correct |
12 ms |
4224 KB |
Output is correct |
35 |
Correct |
33 ms |
6016 KB |
Output is correct |
36 |
Correct |
10 ms |
3200 KB |
Output is correct |
37 |
Correct |
33 ms |
6520 KB |
Output is correct |
38 |
Correct |
47 ms |
7160 KB |
Output is correct |
39 |
Correct |
45 ms |
7160 KB |
Output is correct |
40 |
Correct |
43 ms |
7160 KB |
Output is correct |
41 |
Correct |
41 ms |
7168 KB |
Output is correct |
42 |
Correct |
10 ms |
2688 KB |
Output is correct |
43 |
Correct |
10 ms |
2688 KB |
Output is correct |
44 |
Correct |
11 ms |
2816 KB |
Output is correct |
45 |
Correct |
49 ms |
9976 KB |
Output is correct |
46 |
Correct |
45 ms |
9976 KB |
Output is correct |
47 |
Correct |
65 ms |
14196 KB |
Output is correct |
48 |
Correct |
33 ms |
7284 KB |
Output is correct |
49 |
Correct |
29 ms |
6264 KB |
Output is correct |
50 |
Correct |
22 ms |
5632 KB |
Output is correct |
51 |
Correct |
62 ms |
9844 KB |
Output is correct |
52 |
Correct |
64 ms |
10232 KB |
Output is correct |
53 |
Correct |
40 ms |
7292 KB |
Output is correct |
54 |
Correct |
8 ms |
3072 KB |
Output is correct |
55 |
Correct |
9 ms |
3452 KB |
Output is correct |
56 |
Correct |
32 ms |
6528 KB |
Output is correct |
57 |
Correct |
43 ms |
11780 KB |
Output is correct |
58 |
Correct |
14 ms |
3704 KB |
Output is correct |
59 |
Correct |
15 ms |
4220 KB |
Output is correct |
60 |
Correct |
17 ms |
4860 KB |
Output is correct |
61 |
Correct |
18 ms |
4352 KB |
Output is correct |
62 |
Correct |
49 ms |
10868 KB |
Output is correct |
63 |
Correct |
102 ms |
28012 KB |
Output is correct |
64 |
Correct |
107 ms |
31340 KB |
Output is correct |
65 |
Correct |
135 ms |
41068 KB |
Output is correct |
66 |
Correct |
234 ms |
69088 KB |
Output is correct |
67 |
Correct |
238 ms |
69216 KB |
Output is correct |