#include <algorithm>
#include <iostream>
#include <cstring>
#include <numeric>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int maxn = 3e4+10;
const int mxsq = 210;
const int sq = 200;
const int maxt = maxn*mxsq;
const ll inf = 1e16;
int len[maxn];
int pos[maxn];
ll dist[maxt];
vector<pli> G[maxt];
inline void add_edge(int a, int b, ll w, bool nd)
{
G[a].push_back({w, b});
if(nd)
G[b].push_back({w, a});
}
inline void dijkstra(int s)
{
set<pli> pq;
fill(dist, dist+maxt, inf);
auto mark = [&](int v, ll d)
{
if(dist[v] <= d) return;
pq.erase({dist[v], v});
dist[v] = d;
pq.insert({dist[v], v});
};
mark(s, 0);
while(!pq.empty())
{
int t = pq.begin()->second;
ll d = pq.begin()->first;
pq.erase(pq.begin());
for(pli e: G[t])
{
int u = e.second;
ll w = e.first;
mark(u, d+w);
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> m >> n;
for(int p = 1; p < sq; p++)
{
for(int i = 0; i < m; i++)
{
if(i + p < m)
add_edge(p*m+i, p*m+i+p, 1, true);
add_edge(p*m+i, i, 0, false);
}
}
for(int i = 0; i < n; i++)
{
cin >> pos[i] >> len[i];
int b = pos[i];
int p = len[i];
if(p >= sq)
{
for(int j = b%p; j < m; j += p)
{
add_edge(b, j, abs(b-j)/p, false);
}
}
else
{
add_edge(b, p*m+b, 0, false);
}
}
dijkstra(pos[0]);
ll x = dist[pos[1]];
if(x == inf) x = -1;
cout << x << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
197636 KB |
Output is correct |
2 |
Correct |
85 ms |
197600 KB |
Output is correct |
3 |
Correct |
84 ms |
197640 KB |
Output is correct |
4 |
Correct |
86 ms |
197664 KB |
Output is correct |
5 |
Correct |
85 ms |
197648 KB |
Output is correct |
6 |
Correct |
84 ms |
197672 KB |
Output is correct |
7 |
Correct |
85 ms |
197668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
197572 KB |
Output is correct |
2 |
Correct |
88 ms |
197624 KB |
Output is correct |
3 |
Correct |
90 ms |
197740 KB |
Output is correct |
4 |
Correct |
85 ms |
197596 KB |
Output is correct |
5 |
Correct |
92 ms |
197684 KB |
Output is correct |
6 |
Correct |
85 ms |
197572 KB |
Output is correct |
7 |
Correct |
86 ms |
197620 KB |
Output is correct |
8 |
Correct |
86 ms |
197828 KB |
Output is correct |
9 |
Correct |
86 ms |
197956 KB |
Output is correct |
10 |
Correct |
88 ms |
198384 KB |
Output is correct |
11 |
Correct |
99 ms |
198572 KB |
Output is correct |
12 |
Correct |
90 ms |
198456 KB |
Output is correct |
13 |
Correct |
87 ms |
198468 KB |
Output is correct |
14 |
Correct |
92 ms |
198664 KB |
Output is correct |
15 |
Correct |
88 ms |
198620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
197576 KB |
Output is correct |
2 |
Correct |
88 ms |
197580 KB |
Output is correct |
3 |
Correct |
86 ms |
197628 KB |
Output is correct |
4 |
Correct |
87 ms |
197644 KB |
Output is correct |
5 |
Correct |
85 ms |
197644 KB |
Output is correct |
6 |
Correct |
88 ms |
197684 KB |
Output is correct |
7 |
Correct |
87 ms |
197628 KB |
Output is correct |
8 |
Correct |
86 ms |
197848 KB |
Output is correct |
9 |
Correct |
86 ms |
197992 KB |
Output is correct |
10 |
Correct |
94 ms |
198452 KB |
Output is correct |
11 |
Correct |
91 ms |
198596 KB |
Output is correct |
12 |
Correct |
87 ms |
198508 KB |
Output is correct |
13 |
Correct |
87 ms |
198468 KB |
Output is correct |
14 |
Correct |
89 ms |
198544 KB |
Output is correct |
15 |
Correct |
89 ms |
198576 KB |
Output is correct |
16 |
Correct |
90 ms |
199632 KB |
Output is correct |
17 |
Correct |
110 ms |
208080 KB |
Output is correct |
18 |
Correct |
125 ms |
223556 KB |
Output is correct |
19 |
Correct |
129 ms |
227524 KB |
Output is correct |
20 |
Correct |
144 ms |
227680 KB |
Output is correct |
21 |
Correct |
94 ms |
202436 KB |
Output is correct |
22 |
Correct |
122 ms |
224068 KB |
Output is correct |
23 |
Correct |
121 ms |
221292 KB |
Output is correct |
24 |
Correct |
129 ms |
226048 KB |
Output is correct |
25 |
Correct |
136 ms |
227596 KB |
Output is correct |
26 |
Correct |
134 ms |
227512 KB |
Output is correct |
27 |
Correct |
133 ms |
227672 KB |
Output is correct |
28 |
Correct |
130 ms |
227632 KB |
Output is correct |
29 |
Correct |
136 ms |
227496 KB |
Output is correct |
30 |
Correct |
132 ms |
227472 KB |
Output is correct |
31 |
Correct |
131 ms |
227496 KB |
Output is correct |
32 |
Correct |
132 ms |
227520 KB |
Output is correct |
33 |
Correct |
144 ms |
227724 KB |
Output is correct |
34 |
Correct |
145 ms |
227688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
197620 KB |
Output is correct |
2 |
Correct |
85 ms |
197612 KB |
Output is correct |
3 |
Correct |
86 ms |
197576 KB |
Output is correct |
4 |
Correct |
85 ms |
197652 KB |
Output is correct |
5 |
Correct |
97 ms |
197608 KB |
Output is correct |
6 |
Correct |
98 ms |
197612 KB |
Output is correct |
7 |
Correct |
90 ms |
197676 KB |
Output is correct |
8 |
Correct |
87 ms |
197756 KB |
Output is correct |
9 |
Correct |
88 ms |
197984 KB |
Output is correct |
10 |
Correct |
87 ms |
198436 KB |
Output is correct |
11 |
Correct |
88 ms |
198532 KB |
Output is correct |
12 |
Correct |
87 ms |
198468 KB |
Output is correct |
13 |
Correct |
94 ms |
198436 KB |
Output is correct |
14 |
Correct |
92 ms |
198576 KB |
Output is correct |
15 |
Correct |
91 ms |
198628 KB |
Output is correct |
16 |
Correct |
91 ms |
199568 KB |
Output is correct |
17 |
Correct |
106 ms |
208060 KB |
Output is correct |
18 |
Correct |
126 ms |
223556 KB |
Output is correct |
19 |
Correct |
129 ms |
227472 KB |
Output is correct |
20 |
Correct |
131 ms |
227524 KB |
Output is correct |
21 |
Correct |
94 ms |
202420 KB |
Output is correct |
22 |
Correct |
126 ms |
224168 KB |
Output is correct |
23 |
Correct |
127 ms |
221328 KB |
Output is correct |
24 |
Correct |
130 ms |
226028 KB |
Output is correct |
25 |
Correct |
131 ms |
227588 KB |
Output is correct |
26 |
Correct |
134 ms |
227564 KB |
Output is correct |
27 |
Correct |
133 ms |
227572 KB |
Output is correct |
28 |
Correct |
139 ms |
227652 KB |
Output is correct |
29 |
Correct |
137 ms |
227608 KB |
Output is correct |
30 |
Correct |
130 ms |
227548 KB |
Output is correct |
31 |
Correct |
131 ms |
227528 KB |
Output is correct |
32 |
Correct |
135 ms |
227532 KB |
Output is correct |
33 |
Correct |
141 ms |
227644 KB |
Output is correct |
34 |
Correct |
142 ms |
227628 KB |
Output is correct |
35 |
Correct |
143 ms |
220304 KB |
Output is correct |
36 |
Correct |
111 ms |
213584 KB |
Output is correct |
37 |
Correct |
189 ms |
228920 KB |
Output is correct |
38 |
Correct |
167 ms |
229792 KB |
Output is correct |
39 |
Correct |
173 ms |
229828 KB |
Output is correct |
40 |
Correct |
167 ms |
229780 KB |
Output is correct |
41 |
Correct |
179 ms |
229884 KB |
Output is correct |
42 |
Correct |
132 ms |
228596 KB |
Output is correct |
43 |
Correct |
133 ms |
228556 KB |
Output is correct |
44 |
Correct |
131 ms |
228556 KB |
Output is correct |
45 |
Correct |
203 ms |
231756 KB |
Output is correct |
46 |
Correct |
202 ms |
231740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
197616 KB |
Output is correct |
2 |
Correct |
87 ms |
197580 KB |
Output is correct |
3 |
Correct |
88 ms |
197620 KB |
Output is correct |
4 |
Correct |
85 ms |
197672 KB |
Output is correct |
5 |
Correct |
85 ms |
197588 KB |
Output is correct |
6 |
Correct |
87 ms |
197608 KB |
Output is correct |
7 |
Correct |
86 ms |
197600 KB |
Output is correct |
8 |
Correct |
86 ms |
197828 KB |
Output is correct |
9 |
Correct |
88 ms |
197956 KB |
Output is correct |
10 |
Correct |
88 ms |
198384 KB |
Output is correct |
11 |
Correct |
90 ms |
198540 KB |
Output is correct |
12 |
Correct |
87 ms |
198468 KB |
Output is correct |
13 |
Correct |
88 ms |
198444 KB |
Output is correct |
14 |
Correct |
97 ms |
198528 KB |
Output is correct |
15 |
Correct |
92 ms |
198696 KB |
Output is correct |
16 |
Correct |
89 ms |
199620 KB |
Output is correct |
17 |
Correct |
108 ms |
208068 KB |
Output is correct |
18 |
Correct |
127 ms |
223620 KB |
Output is correct |
19 |
Correct |
132 ms |
227524 KB |
Output is correct |
20 |
Correct |
131 ms |
227580 KB |
Output is correct |
21 |
Correct |
93 ms |
202436 KB |
Output is correct |
22 |
Correct |
121 ms |
224120 KB |
Output is correct |
23 |
Correct |
121 ms |
221288 KB |
Output is correct |
24 |
Correct |
131 ms |
226116 KB |
Output is correct |
25 |
Correct |
134 ms |
227768 KB |
Output is correct |
26 |
Correct |
134 ms |
227516 KB |
Output is correct |
27 |
Correct |
135 ms |
227628 KB |
Output is correct |
28 |
Correct |
130 ms |
227652 KB |
Output is correct |
29 |
Correct |
143 ms |
227592 KB |
Output is correct |
30 |
Correct |
131 ms |
227540 KB |
Output is correct |
31 |
Correct |
132 ms |
227512 KB |
Output is correct |
32 |
Correct |
130 ms |
227500 KB |
Output is correct |
33 |
Correct |
144 ms |
227652 KB |
Output is correct |
34 |
Correct |
148 ms |
227696 KB |
Output is correct |
35 |
Correct |
145 ms |
220320 KB |
Output is correct |
36 |
Correct |
112 ms |
213624 KB |
Output is correct |
37 |
Correct |
174 ms |
228656 KB |
Output is correct |
38 |
Correct |
180 ms |
229716 KB |
Output is correct |
39 |
Correct |
171 ms |
229756 KB |
Output is correct |
40 |
Correct |
171 ms |
229792 KB |
Output is correct |
41 |
Correct |
172 ms |
229816 KB |
Output is correct |
42 |
Correct |
137 ms |
228484 KB |
Output is correct |
43 |
Correct |
140 ms |
228556 KB |
Output is correct |
44 |
Correct |
132 ms |
228580 KB |
Output is correct |
45 |
Correct |
212 ms |
231764 KB |
Output is correct |
46 |
Correct |
209 ms |
231748 KB |
Output is correct |
47 |
Runtime error |
233 ms |
262148 KB |
Execution killed with signal 9 |
48 |
Halted |
0 ms |
0 KB |
- |