#include <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
using namespace std;
//#define RDEBUG 1
#ifdef RDEBUG
#define D(x) x
#else
#define D(x)
#endif
#define inf 0x7fffffff
#define MOD 1000000007
int add(int a, int b) {
a += b;
if(a >= MOD) {
a -= MOD;
}
return a;
}
int sub(int a, int b) {
a -= b;
if(a < 0) {
a += MOD;
}
return a;
}
int mul(int a, int b) {
return (a * b)%MOD;
}
void add_self(int& a, int b) {
a = add(a, b);
}
void sub_self(int& a, int b) {
a = sub(a, b);
}
void mul_self(int& a, int b) {
a = mul(a, b);
}
const int MAXN = 30010;
int sp[MAXN][175];
int N, M;
pair<int, int> doge[MAXN];
vector<pair<pair<int, int>, int>> adj[MAXN][175];
void dij(int start) {
for (int i = 0; i<N; i++) {
for (int j = 0; j<=sqrt(N); j++) {
sp[i][j] = 1e9;
}
}
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
pq.push({0, {doge[start].first, 0}});
sp[doge[start].first][0] = 0;
while (pq.size() != 0) {
int d = pq.top().first, u = pq.top().second.first, p = pq.top().second.second;
pq.pop();
if (sp[u][p] != d) {
continue;
}
if (p != 0) {
if (u-p >= 0) {
adj[u][p].push_back({{u-p, p}, 1});
}
if (u+p < N) {
adj[u][p].push_back({{u+p, p}, 1});
}
adj[u][p].push_back({{u, 0}, 0});
}
for (auto edge : adj[u][p]) {
int v = edge.first.first, np = edge.first.second, w = edge.second;
if (d+w < sp[v][np]) {
sp[v][np] = d+w;
pq.push({d+w, {v, np}});
}
}
if (p != 0) {
if (u-p >= 0) {
adj[u][p].pop_back();
}
if (u+p < N) {
adj[u][p].pop_back();
}
adj[u][p].push_back({{u, 0}, 0});
}
}
}
int main() {
ios_base :: sync_with_stdio(false);
cin >> N >> M;
for (int i = 0; i<M; i++) {
int a, b;
cin >> a >> b;
doge[i] = {a, b};
if (b<=sqrt(N)) {
adj[a][0].push_back({{a, b}, 0});
} else {
for (int j = 1; j<=N; j++) {
int hold = 0;
if (a-b*j >= 0) {
hold++;
adj[a][0].push_back({{a-b*j, 0}, j});
}
if (a+b*j < N) {
hold++;
adj[a][0].push_back({{a+b*j, 0}, j});
}
if (!hold) {
break;
}
}
}
}
dij(0);
cout << (sp[doge[1].first][0] == 1e9 ? -1 : sp[doge[1].first][0]) << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
123640 KB |
Output is correct |
2 |
Correct |
74 ms |
123640 KB |
Output is correct |
3 |
Correct |
86 ms |
123640 KB |
Output is correct |
4 |
Correct |
75 ms |
123640 KB |
Output is correct |
5 |
Correct |
88 ms |
123640 KB |
Output is correct |
6 |
Correct |
76 ms |
123640 KB |
Output is correct |
7 |
Correct |
74 ms |
123608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
123640 KB |
Output is correct |
2 |
Correct |
76 ms |
123640 KB |
Output is correct |
3 |
Correct |
89 ms |
123640 KB |
Output is correct |
4 |
Correct |
91 ms |
123640 KB |
Output is correct |
5 |
Correct |
73 ms |
123640 KB |
Output is correct |
6 |
Correct |
74 ms |
123640 KB |
Output is correct |
7 |
Correct |
75 ms |
123640 KB |
Output is correct |
8 |
Correct |
78 ms |
123640 KB |
Output is correct |
9 |
Correct |
74 ms |
123768 KB |
Output is correct |
10 |
Correct |
75 ms |
123768 KB |
Output is correct |
11 |
Correct |
87 ms |
123896 KB |
Output is correct |
12 |
Correct |
75 ms |
123772 KB |
Output is correct |
13 |
Correct |
89 ms |
123824 KB |
Output is correct |
14 |
Correct |
89 ms |
123896 KB |
Output is correct |
15 |
Correct |
84 ms |
124024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
123640 KB |
Output is correct |
2 |
Correct |
75 ms |
123640 KB |
Output is correct |
3 |
Correct |
87 ms |
123640 KB |
Output is correct |
4 |
Correct |
76 ms |
123640 KB |
Output is correct |
5 |
Correct |
75 ms |
123640 KB |
Output is correct |
6 |
Correct |
87 ms |
123640 KB |
Output is correct |
7 |
Correct |
76 ms |
123640 KB |
Output is correct |
8 |
Correct |
76 ms |
123640 KB |
Output is correct |
9 |
Correct |
89 ms |
123768 KB |
Output is correct |
10 |
Correct |
87 ms |
123768 KB |
Output is correct |
11 |
Correct |
75 ms |
123896 KB |
Output is correct |
12 |
Correct |
78 ms |
123768 KB |
Output is correct |
13 |
Correct |
76 ms |
123768 KB |
Output is correct |
14 |
Correct |
75 ms |
123896 KB |
Output is correct |
15 |
Correct |
77 ms |
124024 KB |
Output is correct |
16 |
Correct |
75 ms |
123896 KB |
Output is correct |
17 |
Correct |
78 ms |
124772 KB |
Output is correct |
18 |
Correct |
76 ms |
124944 KB |
Output is correct |
19 |
Correct |
79 ms |
125048 KB |
Output is correct |
20 |
Correct |
82 ms |
125304 KB |
Output is correct |
21 |
Correct |
79 ms |
124024 KB |
Output is correct |
22 |
Correct |
90 ms |
124972 KB |
Output is correct |
23 |
Correct |
79 ms |
125048 KB |
Output is correct |
24 |
Correct |
80 ms |
125432 KB |
Output is correct |
25 |
Correct |
83 ms |
125304 KB |
Output is correct |
26 |
Correct |
93 ms |
125560 KB |
Output is correct |
27 |
Correct |
81 ms |
125484 KB |
Output is correct |
28 |
Correct |
83 ms |
126072 KB |
Output is correct |
29 |
Correct |
112 ms |
128888 KB |
Output is correct |
30 |
Correct |
82 ms |
126072 KB |
Output is correct |
31 |
Correct |
86 ms |
126968 KB |
Output is correct |
32 |
Correct |
82 ms |
125944 KB |
Output is correct |
33 |
Correct |
115 ms |
130856 KB |
Output is correct |
34 |
Correct |
115 ms |
130936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
123640 KB |
Output is correct |
2 |
Correct |
75 ms |
123768 KB |
Output is correct |
3 |
Correct |
89 ms |
123640 KB |
Output is correct |
4 |
Correct |
75 ms |
123640 KB |
Output is correct |
5 |
Correct |
78 ms |
123764 KB |
Output is correct |
6 |
Correct |
75 ms |
123640 KB |
Output is correct |
7 |
Correct |
92 ms |
123640 KB |
Output is correct |
8 |
Correct |
76 ms |
123768 KB |
Output is correct |
9 |
Correct |
74 ms |
123768 KB |
Output is correct |
10 |
Correct |
88 ms |
123768 KB |
Output is correct |
11 |
Correct |
78 ms |
123896 KB |
Output is correct |
12 |
Correct |
90 ms |
123768 KB |
Output is correct |
13 |
Correct |
78 ms |
123768 KB |
Output is correct |
14 |
Correct |
84 ms |
123896 KB |
Output is correct |
15 |
Correct |
76 ms |
123896 KB |
Output is correct |
16 |
Correct |
75 ms |
123896 KB |
Output is correct |
17 |
Correct |
80 ms |
124792 KB |
Output is correct |
18 |
Correct |
76 ms |
124920 KB |
Output is correct |
19 |
Correct |
77 ms |
125048 KB |
Output is correct |
20 |
Correct |
76 ms |
125304 KB |
Output is correct |
21 |
Correct |
74 ms |
124024 KB |
Output is correct |
22 |
Correct |
76 ms |
124920 KB |
Output is correct |
23 |
Correct |
77 ms |
125048 KB |
Output is correct |
24 |
Correct |
95 ms |
125432 KB |
Output is correct |
25 |
Correct |
81 ms |
125432 KB |
Output is correct |
26 |
Correct |
91 ms |
125560 KB |
Output is correct |
27 |
Correct |
81 ms |
125432 KB |
Output is correct |
28 |
Correct |
83 ms |
125976 KB |
Output is correct |
29 |
Correct |
95 ms |
128888 KB |
Output is correct |
30 |
Correct |
81 ms |
126072 KB |
Output is correct |
31 |
Correct |
87 ms |
126968 KB |
Output is correct |
32 |
Correct |
81 ms |
125944 KB |
Output is correct |
33 |
Correct |
109 ms |
130808 KB |
Output is correct |
34 |
Correct |
109 ms |
130812 KB |
Output is correct |
35 |
Correct |
95 ms |
127860 KB |
Output is correct |
36 |
Correct |
82 ms |
125048 KB |
Output is correct |
37 |
Correct |
106 ms |
130680 KB |
Output is correct |
38 |
Correct |
111 ms |
130168 KB |
Output is correct |
39 |
Correct |
108 ms |
130168 KB |
Output is correct |
40 |
Correct |
108 ms |
130044 KB |
Output is correct |
41 |
Correct |
108 ms |
130040 KB |
Output is correct |
42 |
Correct |
92 ms |
126200 KB |
Output is correct |
43 |
Correct |
93 ms |
126244 KB |
Output is correct |
44 |
Correct |
86 ms |
126112 KB |
Output is correct |
45 |
Correct |
124 ms |
136056 KB |
Output is correct |
46 |
Correct |
121 ms |
136184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
123768 KB |
Output is correct |
2 |
Correct |
76 ms |
123640 KB |
Output is correct |
3 |
Correct |
75 ms |
123640 KB |
Output is correct |
4 |
Correct |
76 ms |
123640 KB |
Output is correct |
5 |
Correct |
82 ms |
123768 KB |
Output is correct |
6 |
Correct |
76 ms |
123640 KB |
Output is correct |
7 |
Correct |
75 ms |
123640 KB |
Output is correct |
8 |
Correct |
77 ms |
123652 KB |
Output is correct |
9 |
Correct |
90 ms |
123768 KB |
Output is correct |
10 |
Correct |
81 ms |
123768 KB |
Output is correct |
11 |
Correct |
82 ms |
123900 KB |
Output is correct |
12 |
Correct |
90 ms |
123768 KB |
Output is correct |
13 |
Correct |
76 ms |
123896 KB |
Output is correct |
14 |
Correct |
75 ms |
123896 KB |
Output is correct |
15 |
Correct |
89 ms |
123896 KB |
Output is correct |
16 |
Correct |
92 ms |
123896 KB |
Output is correct |
17 |
Correct |
81 ms |
124684 KB |
Output is correct |
18 |
Correct |
80 ms |
125048 KB |
Output is correct |
19 |
Correct |
78 ms |
125048 KB |
Output is correct |
20 |
Correct |
78 ms |
125304 KB |
Output is correct |
21 |
Correct |
77 ms |
123896 KB |
Output is correct |
22 |
Correct |
76 ms |
124920 KB |
Output is correct |
23 |
Correct |
78 ms |
125048 KB |
Output is correct |
24 |
Correct |
95 ms |
125432 KB |
Output is correct |
25 |
Correct |
76 ms |
125304 KB |
Output is correct |
26 |
Correct |
81 ms |
125560 KB |
Output is correct |
27 |
Correct |
78 ms |
125432 KB |
Output is correct |
28 |
Correct |
96 ms |
126072 KB |
Output is correct |
29 |
Correct |
116 ms |
128888 KB |
Output is correct |
30 |
Correct |
96 ms |
126176 KB |
Output is correct |
31 |
Correct |
101 ms |
126968 KB |
Output is correct |
32 |
Correct |
82 ms |
125944 KB |
Output is correct |
33 |
Correct |
123 ms |
130808 KB |
Output is correct |
34 |
Correct |
113 ms |
130808 KB |
Output is correct |
35 |
Correct |
99 ms |
127832 KB |
Output is correct |
36 |
Correct |
81 ms |
125048 KB |
Output is correct |
37 |
Correct |
109 ms |
130680 KB |
Output is correct |
38 |
Correct |
125 ms |
130168 KB |
Output is correct |
39 |
Correct |
127 ms |
130168 KB |
Output is correct |
40 |
Correct |
107 ms |
130080 KB |
Output is correct |
41 |
Correct |
107 ms |
130040 KB |
Output is correct |
42 |
Correct |
89 ms |
126288 KB |
Output is correct |
43 |
Correct |
85 ms |
126200 KB |
Output is correct |
44 |
Correct |
83 ms |
126088 KB |
Output is correct |
45 |
Correct |
118 ms |
136056 KB |
Output is correct |
46 |
Correct |
122 ms |
136184 KB |
Output is correct |
47 |
Correct |
331 ms |
161272 KB |
Output is correct |
48 |
Correct |
144 ms |
141560 KB |
Output is correct |
49 |
Correct |
124 ms |
142840 KB |
Output is correct |
50 |
Correct |
127 ms |
144120 KB |
Output is correct |
51 |
Correct |
264 ms |
157436 KB |
Output is correct |
52 |
Correct |
265 ms |
158976 KB |
Output is correct |
53 |
Correct |
158 ms |
148216 KB |
Output is correct |
54 |
Correct |
132 ms |
145144 KB |
Output is correct |
55 |
Correct |
137 ms |
145784 KB |
Output is correct |
56 |
Correct |
149 ms |
147448 KB |
Output is correct |
57 |
Correct |
361 ms |
171456 KB |
Output is correct |
58 |
Correct |
157 ms |
149108 KB |
Output is correct |
59 |
Correct |
174 ms |
151088 KB |
Output is correct |
60 |
Correct |
191 ms |
152436 KB |
Output is correct |
61 |
Correct |
167 ms |
150776 KB |
Output is correct |
62 |
Correct |
271 ms |
165112 KB |
Output is correct |
63 |
Correct |
227 ms |
180428 KB |
Output is correct |
64 |
Correct |
269 ms |
193460 KB |
Output is correct |
65 |
Correct |
897 ms |
261420 KB |
Output is correct |
66 |
Runtime error |
805 ms |
262148 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
67 |
Halted |
0 ms |
0 KB |
- |