#include <bits/stdc++.h>
#define fr first
#define sc second
#define pii pair<int, int>
#define pb push_back
#define szof(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define fastInp ios_base::sync_with_stdio(0); cin.tie(0);
#define int long long
using namespace std;
const int MAXN = 30005;
const int SHIFT = 50000;
const int S = 95;
const int INF = 1e9;
int pos[MAXN], p[MAXN];
int n, m;
struct node {
int v, pw, cost;
node () {
}
node (int v_, int pw_, int cost_) {
v = v_;
pw = pw_;
cost = cost_;
}
};
struct Compare_node {
bool operator () (node const &p1, node const &p2) {
if (p1.cost != p2.cost) {
return p1.cost < p2.cost;
}
if (p1.v != p2.v) {
return p1.v < p2.v;
}
return p1.pw < p2.pw;
}
};
vector <int> big[MAXN];
vector <int> small[MAXN];
vector <node> g[MAXN][S];
int dist[MAXN][S];
bool in(int pos) {
return 0 <= pos && pos < n;
}
signed main() {
fastInp;
cin >> n >> m;
set <int> all_small;
for (int i = 0; i < m; i++) {
cin >> pos[i] >> p[i];
if (p[i] >= S) {
big[pos[i]].pb(p[i]);
} else {
small[pos[i]].pb(p[i]);
all_small.insert(p[i]);
}
}
for (int i = 0; i < n; i++) {
sort(all(small[i]));
small[i].erase(unique(all(small[i])), small[i].end());
for (int val : small[i]) {
g[i][0].pb(node(i, val, 0));
}
}
set <node, Compare_node> pq;
vector <bool> used(MAXN, false);
memset(dist, -1, sizeof(dist));
pq.insert(node(pos[0], 0, 0));
dist[pos[0]][0] = 0;
while (!pq.empty()) {
int v = pq.begin()->v;
int pw = pq.begin()->pw;
int cost = pq.begin()->cost;
pq.erase(pq.begin());
if (cost > dist[v][pw]) {
continue;
}
if (!used[v]) { // впервые добрались до v
used[v] = 1;
for (int el : big[v]) {
for (int to = v % el; to < n; to += el) {
int c = abs(v - to) / el;
if (dist[to][0] == -1 || dist[to][0] > cost + c) {
dist[to][0] = cost + c;
pq.insert(node(to, 0, dist[to][0]));
}
}
}
}
// change our pw
if (dist[v][0] == -1 || dist[v][0] > dist[v][pw]) {
dist[v][0] = dist[v][pw];
pq.insert(node(v, 0, dist[v][pw]));
}
if (pw == 0) {
for (int el : small[v]) {
int new_v = v;
int new_pw = el;
if (!in(new_v)) {
continue;
}
int c = 0;
if (dist[new_v][new_pw] == -1 || dist[new_v][new_pw] > dist[v][pw] + c) {
dist[new_v][new_pw] = dist[v][pw] + c;
pq.insert(node(new_v, new_pw, dist[new_v][new_pw]));
}
}
}
for (int type = -1; type <= 1; type++) {
if (type == 0) {
continue;
}
int new_v = v + type * pw;
int new_pw = pw;
if (!in(new_v)) {
continue;
}
int c = 1;
if (dist[new_v][new_pw] == -1 || dist[new_v][new_pw] > dist[v][pw] + c) {
dist[new_v][new_pw] = dist[v][pw] + c;
pq.insert(node(new_v, new_pw, dist[new_v][new_pw]));
}
}
}
cout << dist[pos[1]][0] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
90924 KB |
Output is correct |
2 |
Correct |
51 ms |
90920 KB |
Output is correct |
3 |
Correct |
51 ms |
90940 KB |
Output is correct |
4 |
Correct |
52 ms |
90912 KB |
Output is correct |
5 |
Correct |
51 ms |
90956 KB |
Output is correct |
6 |
Correct |
53 ms |
90908 KB |
Output is correct |
7 |
Correct |
51 ms |
90852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
90968 KB |
Output is correct |
2 |
Correct |
50 ms |
90896 KB |
Output is correct |
3 |
Correct |
51 ms |
90928 KB |
Output is correct |
4 |
Correct |
52 ms |
90952 KB |
Output is correct |
5 |
Correct |
51 ms |
90928 KB |
Output is correct |
6 |
Correct |
50 ms |
90948 KB |
Output is correct |
7 |
Correct |
51 ms |
90944 KB |
Output is correct |
8 |
Correct |
58 ms |
90948 KB |
Output is correct |
9 |
Correct |
51 ms |
90964 KB |
Output is correct |
10 |
Correct |
51 ms |
91016 KB |
Output is correct |
11 |
Correct |
52 ms |
91076 KB |
Output is correct |
12 |
Correct |
53 ms |
91052 KB |
Output is correct |
13 |
Correct |
52 ms |
90968 KB |
Output is correct |
14 |
Correct |
52 ms |
91208 KB |
Output is correct |
15 |
Correct |
53 ms |
91156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
90944 KB |
Output is correct |
2 |
Correct |
51 ms |
90844 KB |
Output is correct |
3 |
Correct |
51 ms |
90972 KB |
Output is correct |
4 |
Correct |
51 ms |
90948 KB |
Output is correct |
5 |
Correct |
52 ms |
90948 KB |
Output is correct |
6 |
Correct |
58 ms |
90960 KB |
Output is correct |
7 |
Correct |
52 ms |
90948 KB |
Output is correct |
8 |
Correct |
50 ms |
90884 KB |
Output is correct |
9 |
Correct |
52 ms |
90988 KB |
Output is correct |
10 |
Correct |
51 ms |
90988 KB |
Output is correct |
11 |
Correct |
53 ms |
91148 KB |
Output is correct |
12 |
Correct |
51 ms |
90920 KB |
Output is correct |
13 |
Correct |
51 ms |
90940 KB |
Output is correct |
14 |
Correct |
53 ms |
91116 KB |
Output is correct |
15 |
Correct |
53 ms |
91208 KB |
Output is correct |
16 |
Correct |
52 ms |
90976 KB |
Output is correct |
17 |
Correct |
54 ms |
91084 KB |
Output is correct |
18 |
Correct |
59 ms |
90900 KB |
Output is correct |
19 |
Correct |
52 ms |
90992 KB |
Output is correct |
20 |
Correct |
50 ms |
91076 KB |
Output is correct |
21 |
Correct |
51 ms |
90952 KB |
Output is correct |
22 |
Correct |
51 ms |
90992 KB |
Output is correct |
23 |
Correct |
53 ms |
91000 KB |
Output is correct |
24 |
Correct |
53 ms |
91060 KB |
Output is correct |
25 |
Correct |
52 ms |
90956 KB |
Output is correct |
26 |
Correct |
54 ms |
90956 KB |
Output is correct |
27 |
Correct |
52 ms |
90952 KB |
Output is correct |
28 |
Correct |
53 ms |
90976 KB |
Output is correct |
29 |
Correct |
59 ms |
90948 KB |
Output is correct |
30 |
Correct |
54 ms |
90932 KB |
Output is correct |
31 |
Correct |
56 ms |
91076 KB |
Output is correct |
32 |
Correct |
54 ms |
90948 KB |
Output is correct |
33 |
Correct |
68 ms |
91336 KB |
Output is correct |
34 |
Correct |
70 ms |
91208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
90948 KB |
Output is correct |
2 |
Correct |
52 ms |
90948 KB |
Output is correct |
3 |
Correct |
57 ms |
90920 KB |
Output is correct |
4 |
Correct |
52 ms |
90944 KB |
Output is correct |
5 |
Correct |
55 ms |
90948 KB |
Output is correct |
6 |
Correct |
50 ms |
90932 KB |
Output is correct |
7 |
Correct |
52 ms |
90856 KB |
Output is correct |
8 |
Correct |
51 ms |
90868 KB |
Output is correct |
9 |
Correct |
51 ms |
90880 KB |
Output is correct |
10 |
Correct |
51 ms |
91036 KB |
Output is correct |
11 |
Correct |
54 ms |
91248 KB |
Output is correct |
12 |
Correct |
51 ms |
90976 KB |
Output is correct |
13 |
Correct |
51 ms |
90904 KB |
Output is correct |
14 |
Correct |
52 ms |
91148 KB |
Output is correct |
15 |
Correct |
53 ms |
91184 KB |
Output is correct |
16 |
Correct |
51 ms |
90956 KB |
Output is correct |
17 |
Correct |
55 ms |
91080 KB |
Output is correct |
18 |
Correct |
51 ms |
90948 KB |
Output is correct |
19 |
Correct |
51 ms |
90928 KB |
Output is correct |
20 |
Correct |
54 ms |
91076 KB |
Output is correct |
21 |
Correct |
52 ms |
91004 KB |
Output is correct |
22 |
Correct |
51 ms |
90972 KB |
Output is correct |
23 |
Correct |
50 ms |
90948 KB |
Output is correct |
24 |
Correct |
54 ms |
91076 KB |
Output is correct |
25 |
Correct |
53 ms |
90956 KB |
Output is correct |
26 |
Correct |
53 ms |
91000 KB |
Output is correct |
27 |
Correct |
52 ms |
91032 KB |
Output is correct |
28 |
Correct |
54 ms |
91024 KB |
Output is correct |
29 |
Correct |
59 ms |
90996 KB |
Output is correct |
30 |
Correct |
54 ms |
91000 KB |
Output is correct |
31 |
Correct |
57 ms |
90948 KB |
Output is correct |
32 |
Correct |
54 ms |
90948 KB |
Output is correct |
33 |
Correct |
69 ms |
91156 KB |
Output is correct |
34 |
Correct |
68 ms |
91228 KB |
Output is correct |
35 |
Correct |
66 ms |
91824 KB |
Output is correct |
36 |
Correct |
55 ms |
91156 KB |
Output is correct |
37 |
Correct |
76 ms |
91848 KB |
Output is correct |
38 |
Correct |
74 ms |
92228 KB |
Output is correct |
39 |
Correct |
73 ms |
92112 KB |
Output is correct |
40 |
Correct |
74 ms |
92056 KB |
Output is correct |
41 |
Correct |
73 ms |
92100 KB |
Output is correct |
42 |
Correct |
58 ms |
91792 KB |
Output is correct |
43 |
Correct |
59 ms |
91740 KB |
Output is correct |
44 |
Correct |
57 ms |
91808 KB |
Output is correct |
45 |
Correct |
87 ms |
92516 KB |
Output is correct |
46 |
Correct |
88 ms |
92548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
90880 KB |
Output is correct |
2 |
Correct |
51 ms |
90948 KB |
Output is correct |
3 |
Correct |
51 ms |
90880 KB |
Output is correct |
4 |
Correct |
50 ms |
90976 KB |
Output is correct |
5 |
Correct |
53 ms |
90896 KB |
Output is correct |
6 |
Correct |
51 ms |
90888 KB |
Output is correct |
7 |
Correct |
51 ms |
90896 KB |
Output is correct |
8 |
Correct |
52 ms |
90888 KB |
Output is correct |
9 |
Correct |
51 ms |
90952 KB |
Output is correct |
10 |
Correct |
51 ms |
90948 KB |
Output is correct |
11 |
Correct |
52 ms |
91084 KB |
Output is correct |
12 |
Correct |
52 ms |
90948 KB |
Output is correct |
13 |
Correct |
51 ms |
90948 KB |
Output is correct |
14 |
Correct |
52 ms |
91204 KB |
Output is correct |
15 |
Correct |
53 ms |
91116 KB |
Output is correct |
16 |
Correct |
52 ms |
90912 KB |
Output is correct |
17 |
Correct |
54 ms |
90984 KB |
Output is correct |
18 |
Correct |
51 ms |
90908 KB |
Output is correct |
19 |
Correct |
52 ms |
90888 KB |
Output is correct |
20 |
Correct |
52 ms |
91076 KB |
Output is correct |
21 |
Correct |
50 ms |
90992 KB |
Output is correct |
22 |
Correct |
52 ms |
90948 KB |
Output is correct |
23 |
Correct |
52 ms |
90948 KB |
Output is correct |
24 |
Correct |
54 ms |
91148 KB |
Output is correct |
25 |
Correct |
52 ms |
90944 KB |
Output is correct |
26 |
Correct |
52 ms |
90916 KB |
Output is correct |
27 |
Correct |
52 ms |
90948 KB |
Output is correct |
28 |
Correct |
53 ms |
90996 KB |
Output is correct |
29 |
Correct |
60 ms |
91008 KB |
Output is correct |
30 |
Correct |
53 ms |
90928 KB |
Output is correct |
31 |
Correct |
56 ms |
90956 KB |
Output is correct |
32 |
Correct |
54 ms |
90948 KB |
Output is correct |
33 |
Correct |
69 ms |
91144 KB |
Output is correct |
34 |
Correct |
68 ms |
91204 KB |
Output is correct |
35 |
Correct |
67 ms |
91844 KB |
Output is correct |
36 |
Correct |
54 ms |
91136 KB |
Output is correct |
37 |
Correct |
73 ms |
91828 KB |
Output is correct |
38 |
Correct |
74 ms |
92036 KB |
Output is correct |
39 |
Correct |
73 ms |
92100 KB |
Output is correct |
40 |
Correct |
74 ms |
92148 KB |
Output is correct |
41 |
Correct |
73 ms |
92044 KB |
Output is correct |
42 |
Correct |
58 ms |
91692 KB |
Output is correct |
43 |
Correct |
57 ms |
91688 KB |
Output is correct |
44 |
Correct |
57 ms |
91752 KB |
Output is correct |
45 |
Correct |
89 ms |
92612 KB |
Output is correct |
46 |
Correct |
88 ms |
92476 KB |
Output is correct |
47 |
Correct |
145 ms |
93272 KB |
Output is correct |
48 |
Correct |
58 ms |
91764 KB |
Output is correct |
49 |
Correct |
59 ms |
91708 KB |
Output is correct |
50 |
Correct |
56 ms |
91568 KB |
Output is correct |
51 |
Correct |
113 ms |
93564 KB |
Output is correct |
52 |
Correct |
118 ms |
93536 KB |
Output is correct |
53 |
Correct |
69 ms |
91972 KB |
Output is correct |
54 |
Correct |
54 ms |
90924 KB |
Output is correct |
55 |
Correct |
55 ms |
90944 KB |
Output is correct |
56 |
Correct |
68 ms |
93200 KB |
Output is correct |
57 |
Correct |
145 ms |
91204 KB |
Output is correct |
58 |
Correct |
65 ms |
91784 KB |
Output is correct |
59 |
Correct |
68 ms |
91716 KB |
Output is correct |
60 |
Correct |
71 ms |
91840 KB |
Output is correct |
61 |
Correct |
68 ms |
91712 KB |
Output is correct |
62 |
Correct |
101 ms |
92400 KB |
Output is correct |
63 |
Correct |
141 ms |
98076 KB |
Output is correct |
64 |
Correct |
147 ms |
98372 KB |
Output is correct |
65 |
Correct |
176 ms |
100020 KB |
Output is correct |
66 |
Correct |
618 ms |
101284 KB |
Output is correct |
67 |
Correct |
634 ms |
101292 KB |
Output is correct |