#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (int)(b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 30100
#define SQRT 100
#define BLK (MAXN / SQRT) + 10
int n, m;
int B[MAXN], p[MAXN];
vector<int> hab[MAXN];
bool mark[MAXN][SQRT];
int dist[MAXN][SQRT + 1];
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>> s;
inline void relax(int v, int p, int u, int up, int w)
{
if (dist[v][p] + w < dist[u][up])
{
dist[u][up] = dist[v][p] + w;
s.push({dist[u][up], {u, up}});
}
}
void dij(void)
{
FOR(i, 0, MAXN) fill(dist[i], dist[i] + SQRT + 1, INF);
dist[B[1]][0] = 0;
s.push({0, {B[1], 0}});
while (!s.empty())
{
auto pr = s.top();
s.pop();
int v = pr.sc.fr;
int pw = pr.sc.sc;
if (dist[v][pw] != pr.fr) continue;
// relax others
if (pw == SQRT) // people
{
for (int j = B[v]; j <= n; j += p[v])
{
relax(v, pw, j, 0, (j - B[v]) / p[v]);
}
for (int j = B[v] - p[v]; j >= 1; j -= p[v])
{
relax(v, pw, j, 0, (B[v] - j) / p[v]);
}
}
else // building
{
if (pw == 0)
{
FOR(j, 1, SQRT) if (mark[v][j])
relax(v, pw, v, j, 0);
for (int u : hab[v]) if (p[u] >= SQRT)
relax(v, pw, u, SQRT, 0);
}
else
{
relax(v, pw, v, 0, 0);
if (v + pw <= n) relax(v, pw, v + pw, pw, 1);
if (v - pw >= 1) relax(v, pw, v - pw, pw, 1);
}
}
}
}
int32_t main(void)
{
fast_io;
cin >> n >> m;
FOR(i, 1, m + 1)
{
cin >> B[i] >> p[i];
B[i]++;
hab[B[i]].pb(i);
if (p[i] < SQRT) mark[B[i]][p[i]] = true;
}
dij(); // from (B[1], 0)
if (dist[B[2]][0] >= INF) cout << "-1\n";
else cout << dist[B[2]][0] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12856 KB |
Output is correct |
2 |
Correct |
8 ms |
12876 KB |
Output is correct |
3 |
Correct |
9 ms |
12880 KB |
Output is correct |
4 |
Correct |
9 ms |
12876 KB |
Output is correct |
5 |
Correct |
10 ms |
12876 KB |
Output is correct |
6 |
Correct |
8 ms |
12892 KB |
Output is correct |
7 |
Correct |
8 ms |
12876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12876 KB |
Output is correct |
2 |
Correct |
8 ms |
12876 KB |
Output is correct |
3 |
Correct |
9 ms |
12876 KB |
Output is correct |
4 |
Correct |
9 ms |
12876 KB |
Output is correct |
5 |
Correct |
10 ms |
12876 KB |
Output is correct |
6 |
Correct |
9 ms |
12908 KB |
Output is correct |
7 |
Correct |
9 ms |
12876 KB |
Output is correct |
8 |
Correct |
9 ms |
12876 KB |
Output is correct |
9 |
Correct |
9 ms |
12876 KB |
Output is correct |
10 |
Correct |
9 ms |
12876 KB |
Output is correct |
11 |
Correct |
10 ms |
13016 KB |
Output is correct |
12 |
Correct |
9 ms |
12940 KB |
Output is correct |
13 |
Correct |
9 ms |
12872 KB |
Output is correct |
14 |
Correct |
9 ms |
13004 KB |
Output is correct |
15 |
Correct |
10 ms |
12928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
12808 KB |
Output is correct |
2 |
Correct |
8 ms |
12888 KB |
Output is correct |
3 |
Correct |
10 ms |
12876 KB |
Output is correct |
4 |
Correct |
8 ms |
12876 KB |
Output is correct |
5 |
Correct |
11 ms |
12844 KB |
Output is correct |
6 |
Correct |
9 ms |
12876 KB |
Output is correct |
7 |
Correct |
9 ms |
12876 KB |
Output is correct |
8 |
Correct |
9 ms |
12876 KB |
Output is correct |
9 |
Correct |
9 ms |
12876 KB |
Output is correct |
10 |
Correct |
8 ms |
12840 KB |
Output is correct |
11 |
Correct |
10 ms |
13004 KB |
Output is correct |
12 |
Correct |
9 ms |
12876 KB |
Output is correct |
13 |
Correct |
9 ms |
12852 KB |
Output is correct |
14 |
Correct |
9 ms |
13004 KB |
Output is correct |
15 |
Correct |
12 ms |
13016 KB |
Output is correct |
16 |
Correct |
9 ms |
12944 KB |
Output is correct |
17 |
Correct |
11 ms |
13004 KB |
Output is correct |
18 |
Correct |
9 ms |
13004 KB |
Output is correct |
19 |
Correct |
9 ms |
12956 KB |
Output is correct |
20 |
Correct |
9 ms |
13132 KB |
Output is correct |
21 |
Correct |
9 ms |
13004 KB |
Output is correct |
22 |
Correct |
9 ms |
13004 KB |
Output is correct |
23 |
Correct |
10 ms |
13004 KB |
Output is correct |
24 |
Correct |
11 ms |
13168 KB |
Output is correct |
25 |
Correct |
10 ms |
12876 KB |
Output is correct |
26 |
Correct |
11 ms |
12988 KB |
Output is correct |
27 |
Correct |
10 ms |
13028 KB |
Output is correct |
28 |
Correct |
11 ms |
12980 KB |
Output is correct |
29 |
Correct |
19 ms |
12876 KB |
Output is correct |
30 |
Correct |
14 ms |
12884 KB |
Output is correct |
31 |
Correct |
14 ms |
12940 KB |
Output is correct |
32 |
Correct |
11 ms |
12876 KB |
Output is correct |
33 |
Correct |
26 ms |
13020 KB |
Output is correct |
34 |
Correct |
24 ms |
13020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12924 KB |
Output is correct |
2 |
Correct |
9 ms |
12876 KB |
Output is correct |
3 |
Correct |
8 ms |
12876 KB |
Output is correct |
4 |
Correct |
9 ms |
12824 KB |
Output is correct |
5 |
Correct |
8 ms |
12876 KB |
Output is correct |
6 |
Correct |
9 ms |
12876 KB |
Output is correct |
7 |
Correct |
9 ms |
12876 KB |
Output is correct |
8 |
Correct |
9 ms |
12900 KB |
Output is correct |
9 |
Correct |
9 ms |
12860 KB |
Output is correct |
10 |
Correct |
10 ms |
12876 KB |
Output is correct |
11 |
Correct |
10 ms |
13004 KB |
Output is correct |
12 |
Correct |
9 ms |
12876 KB |
Output is correct |
13 |
Correct |
9 ms |
12924 KB |
Output is correct |
14 |
Correct |
10 ms |
13004 KB |
Output is correct |
15 |
Correct |
11 ms |
12980 KB |
Output is correct |
16 |
Correct |
9 ms |
12876 KB |
Output is correct |
17 |
Correct |
11 ms |
13004 KB |
Output is correct |
18 |
Correct |
9 ms |
13004 KB |
Output is correct |
19 |
Correct |
9 ms |
13004 KB |
Output is correct |
20 |
Correct |
9 ms |
13160 KB |
Output is correct |
21 |
Correct |
8 ms |
12876 KB |
Output is correct |
22 |
Correct |
9 ms |
13004 KB |
Output is correct |
23 |
Correct |
11 ms |
13004 KB |
Output is correct |
24 |
Correct |
11 ms |
13132 KB |
Output is correct |
25 |
Correct |
11 ms |
12876 KB |
Output is correct |
26 |
Correct |
10 ms |
12964 KB |
Output is correct |
27 |
Correct |
10 ms |
13004 KB |
Output is correct |
28 |
Correct |
12 ms |
13032 KB |
Output is correct |
29 |
Correct |
17 ms |
12876 KB |
Output is correct |
30 |
Correct |
11 ms |
12876 KB |
Output is correct |
31 |
Correct |
13 ms |
12976 KB |
Output is correct |
32 |
Correct |
12 ms |
12876 KB |
Output is correct |
33 |
Correct |
24 ms |
12944 KB |
Output is correct |
34 |
Correct |
26 ms |
13016 KB |
Output is correct |
35 |
Correct |
29 ms |
13928 KB |
Output is correct |
36 |
Correct |
16 ms |
13132 KB |
Output is correct |
37 |
Correct |
35 ms |
13688 KB |
Output is correct |
38 |
Correct |
39 ms |
13988 KB |
Output is correct |
39 |
Correct |
36 ms |
14000 KB |
Output is correct |
40 |
Correct |
37 ms |
14096 KB |
Output is correct |
41 |
Correct |
36 ms |
14096 KB |
Output is correct |
42 |
Correct |
14 ms |
13472 KB |
Output is correct |
43 |
Correct |
19 ms |
13388 KB |
Output is correct |
44 |
Correct |
18 ms |
13584 KB |
Output is correct |
45 |
Correct |
52 ms |
13896 KB |
Output is correct |
46 |
Correct |
54 ms |
13856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12876 KB |
Output is correct |
2 |
Correct |
8 ms |
12876 KB |
Output is correct |
3 |
Correct |
9 ms |
12876 KB |
Output is correct |
4 |
Correct |
9 ms |
12888 KB |
Output is correct |
5 |
Correct |
10 ms |
12876 KB |
Output is correct |
6 |
Correct |
8 ms |
12876 KB |
Output is correct |
7 |
Correct |
10 ms |
12876 KB |
Output is correct |
8 |
Correct |
10 ms |
12876 KB |
Output is correct |
9 |
Correct |
10 ms |
12888 KB |
Output is correct |
10 |
Correct |
10 ms |
12836 KB |
Output is correct |
11 |
Correct |
10 ms |
13004 KB |
Output is correct |
12 |
Correct |
8 ms |
12876 KB |
Output is correct |
13 |
Correct |
8 ms |
12876 KB |
Output is correct |
14 |
Correct |
9 ms |
13008 KB |
Output is correct |
15 |
Correct |
9 ms |
13012 KB |
Output is correct |
16 |
Correct |
9 ms |
12876 KB |
Output is correct |
17 |
Correct |
11 ms |
13032 KB |
Output is correct |
18 |
Correct |
11 ms |
13000 KB |
Output is correct |
19 |
Correct |
10 ms |
13004 KB |
Output is correct |
20 |
Correct |
11 ms |
13132 KB |
Output is correct |
21 |
Correct |
9 ms |
12876 KB |
Output is correct |
22 |
Correct |
9 ms |
13004 KB |
Output is correct |
23 |
Correct |
10 ms |
13004 KB |
Output is correct |
24 |
Correct |
15 ms |
13056 KB |
Output is correct |
25 |
Correct |
10 ms |
12876 KB |
Output is correct |
26 |
Correct |
10 ms |
13004 KB |
Output is correct |
27 |
Correct |
9 ms |
13004 KB |
Output is correct |
28 |
Correct |
12 ms |
13004 KB |
Output is correct |
29 |
Correct |
16 ms |
12876 KB |
Output is correct |
30 |
Correct |
11 ms |
12876 KB |
Output is correct |
31 |
Correct |
13 ms |
12960 KB |
Output is correct |
32 |
Correct |
12 ms |
12876 KB |
Output is correct |
33 |
Correct |
30 ms |
13024 KB |
Output is correct |
34 |
Correct |
25 ms |
13012 KB |
Output is correct |
35 |
Correct |
29 ms |
13936 KB |
Output is correct |
36 |
Correct |
12 ms |
13076 KB |
Output is correct |
37 |
Correct |
34 ms |
13740 KB |
Output is correct |
38 |
Correct |
41 ms |
14108 KB |
Output is correct |
39 |
Correct |
37 ms |
14036 KB |
Output is correct |
40 |
Correct |
37 ms |
14032 KB |
Output is correct |
41 |
Correct |
35 ms |
14096 KB |
Output is correct |
42 |
Correct |
16 ms |
13472 KB |
Output is correct |
43 |
Correct |
14 ms |
13388 KB |
Output is correct |
44 |
Correct |
14 ms |
13496 KB |
Output is correct |
45 |
Correct |
51 ms |
13888 KB |
Output is correct |
46 |
Correct |
52 ms |
13908 KB |
Output is correct |
47 |
Correct |
98 ms |
15112 KB |
Output is correct |
48 |
Correct |
15 ms |
13772 KB |
Output is correct |
49 |
Correct |
15 ms |
13728 KB |
Output is correct |
50 |
Correct |
13 ms |
13504 KB |
Output is correct |
51 |
Correct |
77 ms |
14588 KB |
Output is correct |
52 |
Correct |
82 ms |
14704 KB |
Output is correct |
53 |
Correct |
39 ms |
13800 KB |
Output is correct |
54 |
Correct |
11 ms |
12876 KB |
Output is correct |
55 |
Correct |
15 ms |
12936 KB |
Output is correct |
56 |
Correct |
32 ms |
16972 KB |
Output is correct |
57 |
Correct |
106 ms |
15820 KB |
Output is correct |
58 |
Correct |
24 ms |
13416 KB |
Output is correct |
59 |
Correct |
24 ms |
13416 KB |
Output is correct |
60 |
Correct |
30 ms |
13436 KB |
Output is correct |
61 |
Correct |
31 ms |
13452 KB |
Output is correct |
62 |
Correct |
57 ms |
14112 KB |
Output is correct |
63 |
Correct |
95 ms |
15048 KB |
Output is correct |
64 |
Correct |
112 ms |
15020 KB |
Output is correct |
65 |
Correct |
112 ms |
15028 KB |
Output is correct |
66 |
Correct |
586 ms |
16568 KB |
Output is correct |
67 |
Correct |
597 ms |
16700 KB |
Output is correct |