#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 100005;
const int inf = 1e9 + 5;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
int n, m;
set<int> skok[maxn];
vector<pii> g[maxn];
bool pripada(int X) {
return (X >= 0 && X < n);
}
ll dist[maxn];
void dij(int s) {
ff(i,0,n - 1)dist[i] = inf;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push({dist[s] = 0, s});
while(!pq.empty()) {
pii v = pq.top();
pq.pop();
if(dist[v.se] < v.fi)continue;
for(auto c : g[v.se]) {
int u = c.fi;
int w = c.se;
if(dist[u] > w + v.fi) {
pq.push({dist[u] = w + v.fi, u});
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> n >> m;
int start = 0, end = 0;
ff(i,0,m - 1) {
int X, Y;
cin >> X >> Y;
if(i != 1)skok[X].insert(Y);
if(i == 0)start = X;
if(i == 1)end = X;
}
ff(i,0,n - 1) {
for(auto c : skok[i]) {
int j = 1;
while(1) {
int nw = i + j * c;
if(!pripada(nw))break;
g[i].pb({nw, j});
if(skok[nw].count(c))break;
j += 1;
}
}
}
ff(i,0,n - 1) {
for(auto c : skok[i]) {
int j = 1;
while(1) {
int nw = i - j * c;
if(!pripada(nw))break;
g[i].pb({nw, j});
if(skok[nw].count(c))break;
j += 1;
}
}
}
dij(start);
cout << (dist[end] == inf ? -1 : dist[end]) << endl;
return 0;
}
/**
5 3
0 2
1 1
4 1
// probati bojenje sahovski ili slicno
**/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7244 KB |
Output is correct |
2 |
Correct |
6 ms |
7324 KB |
Output is correct |
3 |
Correct |
5 ms |
7364 KB |
Output is correct |
4 |
Correct |
6 ms |
7264 KB |
Output is correct |
5 |
Correct |
5 ms |
7364 KB |
Output is correct |
6 |
Correct |
10 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7244 KB |
Output is correct |
3 |
Correct |
5 ms |
7248 KB |
Output is correct |
4 |
Correct |
5 ms |
7244 KB |
Output is correct |
5 |
Correct |
6 ms |
7368 KB |
Output is correct |
6 |
Correct |
6 ms |
7328 KB |
Output is correct |
7 |
Correct |
6 ms |
7364 KB |
Output is correct |
8 |
Correct |
6 ms |
7372 KB |
Output is correct |
9 |
Correct |
5 ms |
7372 KB |
Output is correct |
10 |
Correct |
7 ms |
7376 KB |
Output is correct |
11 |
Correct |
7 ms |
7520 KB |
Output is correct |
12 |
Correct |
6 ms |
7372 KB |
Output is correct |
13 |
Correct |
5 ms |
7372 KB |
Output is correct |
14 |
Correct |
7 ms |
7500 KB |
Output is correct |
15 |
Correct |
6 ms |
7500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7244 KB |
Output is correct |
2 |
Correct |
5 ms |
7368 KB |
Output is correct |
3 |
Correct |
5 ms |
7244 KB |
Output is correct |
4 |
Correct |
6 ms |
7276 KB |
Output is correct |
5 |
Correct |
5 ms |
7252 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
5 ms |
7244 KB |
Output is correct |
8 |
Correct |
5 ms |
7364 KB |
Output is correct |
9 |
Correct |
5 ms |
7372 KB |
Output is correct |
10 |
Correct |
6 ms |
7368 KB |
Output is correct |
11 |
Correct |
6 ms |
7500 KB |
Output is correct |
12 |
Correct |
7 ms |
7376 KB |
Output is correct |
13 |
Correct |
6 ms |
7376 KB |
Output is correct |
14 |
Correct |
6 ms |
7508 KB |
Output is correct |
15 |
Correct |
6 ms |
7436 KB |
Output is correct |
16 |
Correct |
7 ms |
7372 KB |
Output is correct |
17 |
Correct |
7 ms |
7628 KB |
Output is correct |
18 |
Correct |
6 ms |
7416 KB |
Output is correct |
19 |
Correct |
5 ms |
7372 KB |
Output is correct |
20 |
Correct |
6 ms |
7500 KB |
Output is correct |
21 |
Correct |
6 ms |
7372 KB |
Output is correct |
22 |
Correct |
5 ms |
7372 KB |
Output is correct |
23 |
Correct |
6 ms |
7488 KB |
Output is correct |
24 |
Correct |
8 ms |
7640 KB |
Output is correct |
25 |
Correct |
6 ms |
7500 KB |
Output is correct |
26 |
Correct |
6 ms |
7500 KB |
Output is correct |
27 |
Correct |
7 ms |
7504 KB |
Output is correct |
28 |
Correct |
7 ms |
7640 KB |
Output is correct |
29 |
Correct |
10 ms |
8136 KB |
Output is correct |
30 |
Correct |
7 ms |
7628 KB |
Output is correct |
31 |
Correct |
7 ms |
7756 KB |
Output is correct |
32 |
Correct |
6 ms |
7704 KB |
Output is correct |
33 |
Correct |
11 ms |
8908 KB |
Output is correct |
34 |
Correct |
12 ms |
8908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7244 KB |
Output is correct |
2 |
Correct |
6 ms |
7364 KB |
Output is correct |
3 |
Correct |
6 ms |
7372 KB |
Output is correct |
4 |
Correct |
6 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7372 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
6 ms |
7264 KB |
Output is correct |
8 |
Correct |
5 ms |
7372 KB |
Output is correct |
9 |
Correct |
6 ms |
7372 KB |
Output is correct |
10 |
Correct |
5 ms |
7372 KB |
Output is correct |
11 |
Correct |
6 ms |
7500 KB |
Output is correct |
12 |
Correct |
6 ms |
7376 KB |
Output is correct |
13 |
Correct |
6 ms |
7372 KB |
Output is correct |
14 |
Correct |
6 ms |
7504 KB |
Output is correct |
15 |
Correct |
7 ms |
7500 KB |
Output is correct |
16 |
Correct |
6 ms |
7368 KB |
Output is correct |
17 |
Correct |
8 ms |
7628 KB |
Output is correct |
18 |
Correct |
6 ms |
7500 KB |
Output is correct |
19 |
Correct |
5 ms |
7372 KB |
Output is correct |
20 |
Correct |
6 ms |
7444 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
22 |
Correct |
6 ms |
7372 KB |
Output is correct |
23 |
Correct |
7 ms |
7500 KB |
Output is correct |
24 |
Correct |
7 ms |
7628 KB |
Output is correct |
25 |
Correct |
6 ms |
7500 KB |
Output is correct |
26 |
Correct |
6 ms |
7516 KB |
Output is correct |
27 |
Correct |
8 ms |
7400 KB |
Output is correct |
28 |
Correct |
7 ms |
7628 KB |
Output is correct |
29 |
Correct |
9 ms |
8232 KB |
Output is correct |
30 |
Correct |
9 ms |
7628 KB |
Output is correct |
31 |
Correct |
7 ms |
7756 KB |
Output is correct |
32 |
Correct |
7 ms |
7628 KB |
Output is correct |
33 |
Correct |
11 ms |
8908 KB |
Output is correct |
34 |
Correct |
11 ms |
8912 KB |
Output is correct |
35 |
Correct |
22 ms |
9820 KB |
Output is correct |
36 |
Correct |
8 ms |
7780 KB |
Output is correct |
37 |
Correct |
24 ms |
10436 KB |
Output is correct |
38 |
Correct |
33 ms |
10832 KB |
Output is correct |
39 |
Correct |
31 ms |
10828 KB |
Output is correct |
40 |
Correct |
35 ms |
10812 KB |
Output is correct |
41 |
Correct |
28 ms |
10820 KB |
Output is correct |
42 |
Correct |
10 ms |
7628 KB |
Output is correct |
43 |
Correct |
10 ms |
7628 KB |
Output is correct |
44 |
Correct |
10 ms |
7736 KB |
Output is correct |
45 |
Correct |
32 ms |
13416 KB |
Output is correct |
46 |
Correct |
33 ms |
13448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7244 KB |
Output is correct |
2 |
Correct |
6 ms |
7344 KB |
Output is correct |
3 |
Correct |
6 ms |
7308 KB |
Output is correct |
4 |
Correct |
5 ms |
7244 KB |
Output is correct |
5 |
Correct |
5 ms |
7372 KB |
Output is correct |
6 |
Correct |
5 ms |
7244 KB |
Output is correct |
7 |
Correct |
6 ms |
7244 KB |
Output is correct |
8 |
Correct |
5 ms |
7372 KB |
Output is correct |
9 |
Correct |
5 ms |
7372 KB |
Output is correct |
10 |
Correct |
6 ms |
7372 KB |
Output is correct |
11 |
Correct |
6 ms |
7504 KB |
Output is correct |
12 |
Correct |
5 ms |
7376 KB |
Output is correct |
13 |
Correct |
6 ms |
7372 KB |
Output is correct |
14 |
Correct |
6 ms |
7500 KB |
Output is correct |
15 |
Correct |
7 ms |
7452 KB |
Output is correct |
16 |
Correct |
6 ms |
7372 KB |
Output is correct |
17 |
Correct |
7 ms |
7628 KB |
Output is correct |
18 |
Correct |
6 ms |
7476 KB |
Output is correct |
19 |
Correct |
5 ms |
7372 KB |
Output is correct |
20 |
Correct |
6 ms |
7512 KB |
Output is correct |
21 |
Correct |
5 ms |
7372 KB |
Output is correct |
22 |
Correct |
5 ms |
7440 KB |
Output is correct |
23 |
Correct |
6 ms |
7504 KB |
Output is correct |
24 |
Correct |
7 ms |
7640 KB |
Output is correct |
25 |
Correct |
6 ms |
7500 KB |
Output is correct |
26 |
Correct |
6 ms |
7500 KB |
Output is correct |
27 |
Correct |
6 ms |
7500 KB |
Output is correct |
28 |
Correct |
6 ms |
7628 KB |
Output is correct |
29 |
Correct |
8 ms |
8140 KB |
Output is correct |
30 |
Correct |
6 ms |
7628 KB |
Output is correct |
31 |
Correct |
7 ms |
7752 KB |
Output is correct |
32 |
Correct |
6 ms |
7628 KB |
Output is correct |
33 |
Correct |
12 ms |
8908 KB |
Output is correct |
34 |
Correct |
13 ms |
8920 KB |
Output is correct |
35 |
Correct |
23 ms |
9764 KB |
Output is correct |
36 |
Correct |
7 ms |
7756 KB |
Output is correct |
37 |
Correct |
26 ms |
10468 KB |
Output is correct |
38 |
Correct |
29 ms |
10836 KB |
Output is correct |
39 |
Correct |
28 ms |
10868 KB |
Output is correct |
40 |
Correct |
28 ms |
10720 KB |
Output is correct |
41 |
Correct |
28 ms |
10820 KB |
Output is correct |
42 |
Correct |
10 ms |
7628 KB |
Output is correct |
43 |
Correct |
10 ms |
7628 KB |
Output is correct |
44 |
Correct |
10 ms |
7640 KB |
Output is correct |
45 |
Correct |
32 ms |
13396 KB |
Output is correct |
46 |
Correct |
32 ms |
13444 KB |
Output is correct |
47 |
Correct |
54 ms |
18068 KB |
Output is correct |
48 |
Correct |
23 ms |
11200 KB |
Output is correct |
49 |
Correct |
20 ms |
10248 KB |
Output is correct |
50 |
Correct |
16 ms |
9940 KB |
Output is correct |
51 |
Correct |
49 ms |
13180 KB |
Output is correct |
52 |
Correct |
61 ms |
13644 KB |
Output is correct |
53 |
Correct |
22 ms |
10056 KB |
Output is correct |
54 |
Correct |
7 ms |
7884 KB |
Output is correct |
55 |
Correct |
9 ms |
8264 KB |
Output is correct |
56 |
Correct |
19 ms |
10108 KB |
Output is correct |
57 |
Correct |
44 ms |
16924 KB |
Output is correct |
58 |
Correct |
14 ms |
8452 KB |
Output is correct |
59 |
Correct |
15 ms |
9160 KB |
Output is correct |
60 |
Correct |
16 ms |
9628 KB |
Output is correct |
61 |
Correct |
17 ms |
9224 KB |
Output is correct |
62 |
Correct |
34 ms |
14184 KB |
Output is correct |
63 |
Correct |
100 ms |
31060 KB |
Output is correct |
64 |
Correct |
103 ms |
34544 KB |
Output is correct |
65 |
Correct |
135 ms |
43932 KB |
Output is correct |
66 |
Correct |
248 ms |
70596 KB |
Output is correct |
67 |
Correct |
249 ms |
70576 KB |
Output is correct |