#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;
//Vは比較可能な値にする
template <typename V>
class Graph {
private:
map<V, vector<pair<V, LL>>>edge;
public:
//from->toへcostの辺を張る
void addEdge(V from, V to, LL cost) {
edge[from].push_back(make_pair(to, cost));
}
//v1とv2間にcostの辺を張る
void addUndirectedEdge(V v1, V v2, LL cost) {
edge[v1].push_back(make_pair(v2, cost));
edge[v2].push_back(make_pair(v1, cost));
}
//最短を求める
void dijkstra(V start, map<V, LL>&dist) {
dist[start] = 0;
priority_queue<pair<LL, V>, vector<pair<LL, V>>, greater<pair<LL, V>>>que;
que.push(make_pair(0LL, start));
while (!que.empty()) {
auto q = que.top();
que.pop();
V now = q.second;
LL dis = q.first;
if (dist[now] < dis) continue;
for (auto e : edge[now]) {
V nxt = e.first;
LL cost = e.second;
if (!dist.count(nxt) || dist[nxt] > dist[now] + cost) {
dist[nxt] = dist[now] + cost;
que.push(make_pair(dist[nxt], nxt));
}
}
}
}
};
int N, M;
int mceil(int a, int b) {
if (a == 0)return 0;
return (a - 1) / b + 1;
}
int main(void)
{
cin >> N >> M;
using P = pair<int, int>;
map<P, vector<int>>mp;
int st = -1;
int enb = -1, enp = -1, enr = -1;
for (int i = 0; i < M; ++i) {
int b, p;
cin >> b >> p;
if (i == 0)st = b;
if (i == 1) {
enb = b;
enp = p;
enr = b % p;
}
int rem = b % p;
mp[make_pair(p, rem)].push_back(b);
}
Graph<P>G;
for (auto elm : mp) {
int p = elm.first.first;
int rem = elm.first.second;
auto& v = elm.second;
int len = mceil(N - rem, p);
vector<LL>dist(len, INT_MAX);
for (int x : v)dist[x / p] = 0;
/*for (int i = 1; i < len; ++i) {
dist[i] = min(dist[i - 1] + 1, dist[i]);
}
for (int i = len - 1; i > 0; --i) {
dist[i - 1] = min(dist[i] + 1, dist[i - 1]);
}*/
for (int i = 0; i < len; ++i) {
if (dist[i] < INT_MAX)G.addEdge(P(0, i * p + rem), P(p, i * p + rem), dist[i]);
G.addEdge(P(p, i * p + rem), P(0, i * p + rem), 0LL);
}
for (int i = 1; i < len; ++i) {
G.addUndirectedEdge(P(p, (i - 1)*p + rem), P(p, i*p + rem), 1LL);
}
}
int len = mceil(N - enr, enp);
for (int i = 0; i < len; ++i) {
if (abs(i - (enb / enp)) == 0)
G.addEdge(P(0, i * enp + enr), P(N, i * enp + enr), abs(i - (enb / enp)));
}
map<P, LL> dist;
G.dijkstra(P(0, st), dist);
LL ans = LLONG_MAX;
for (int i = 0; i < len; ++i) {
if (abs(i - (enb / enp)) == 0 && dist.count(P(N, i * enp + enr)))
ans = min(ans, dist[P(N, i * enp + enr)]);
}
if (ans == LLONG_MAX) {
cout << -1 << endl;
}
else {
cout << ans << endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
432 KB |
Output is correct |
5 |
Correct |
2 ms |
448 KB |
Output is correct |
6 |
Correct |
2 ms |
488 KB |
Output is correct |
7 |
Correct |
2 ms |
488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
488 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
528 KB |
Output is correct |
6 |
Correct |
2 ms |
608 KB |
Output is correct |
7 |
Correct |
2 ms |
608 KB |
Output is correct |
8 |
Correct |
2 ms |
616 KB |
Output is correct |
9 |
Correct |
2 ms |
620 KB |
Output is correct |
10 |
Correct |
5 ms |
1136 KB |
Output is correct |
11 |
Correct |
12 ms |
1696 KB |
Output is correct |
12 |
Correct |
3 ms |
1696 KB |
Output is correct |
13 |
Correct |
3 ms |
1696 KB |
Output is correct |
14 |
Correct |
15 ms |
2180 KB |
Output is correct |
15 |
Correct |
13 ms |
2200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2200 KB |
Output is correct |
2 |
Correct |
2 ms |
2200 KB |
Output is correct |
3 |
Correct |
2 ms |
2200 KB |
Output is correct |
4 |
Correct |
2 ms |
2200 KB |
Output is correct |
5 |
Correct |
2 ms |
2200 KB |
Output is correct |
6 |
Correct |
2 ms |
2200 KB |
Output is correct |
7 |
Correct |
2 ms |
2200 KB |
Output is correct |
8 |
Correct |
2 ms |
2200 KB |
Output is correct |
9 |
Correct |
2 ms |
2200 KB |
Output is correct |
10 |
Correct |
5 ms |
2200 KB |
Output is correct |
11 |
Correct |
11 ms |
2200 KB |
Output is correct |
12 |
Correct |
3 ms |
2200 KB |
Output is correct |
13 |
Correct |
4 ms |
2200 KB |
Output is correct |
14 |
Correct |
13 ms |
2292 KB |
Output is correct |
15 |
Correct |
13 ms |
2304 KB |
Output is correct |
16 |
Correct |
5 ms |
2304 KB |
Output is correct |
17 |
Correct |
38 ms |
4264 KB |
Output is correct |
18 |
Correct |
6 ms |
4264 KB |
Output is correct |
19 |
Correct |
5 ms |
4264 KB |
Output is correct |
20 |
Correct |
8 ms |
4264 KB |
Output is correct |
21 |
Correct |
4 ms |
4264 KB |
Output is correct |
22 |
Correct |
6 ms |
4264 KB |
Output is correct |
23 |
Correct |
18 ms |
4264 KB |
Output is correct |
24 |
Correct |
41 ms |
4264 KB |
Output is correct |
25 |
Correct |
22 ms |
4264 KB |
Output is correct |
26 |
Correct |
17 ms |
4264 KB |
Output is correct |
27 |
Correct |
15 ms |
4264 KB |
Output is correct |
28 |
Correct |
47 ms |
6520 KB |
Output is correct |
29 |
Correct |
149 ms |
14860 KB |
Output is correct |
30 |
Correct |
47 ms |
14860 KB |
Output is correct |
31 |
Correct |
98 ms |
14860 KB |
Output is correct |
32 |
Correct |
86 ms |
14860 KB |
Output is correct |
33 |
Correct |
319 ms |
27864 KB |
Output is correct |
34 |
Correct |
275 ms |
27952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
27952 KB |
Output is correct |
2 |
Correct |
2 ms |
27952 KB |
Output is correct |
3 |
Correct |
2 ms |
27952 KB |
Output is correct |
4 |
Correct |
2 ms |
27952 KB |
Output is correct |
5 |
Correct |
2 ms |
27952 KB |
Output is correct |
6 |
Correct |
2 ms |
27952 KB |
Output is correct |
7 |
Correct |
2 ms |
27952 KB |
Output is correct |
8 |
Correct |
2 ms |
27952 KB |
Output is correct |
9 |
Correct |
2 ms |
27952 KB |
Output is correct |
10 |
Correct |
6 ms |
27952 KB |
Output is correct |
11 |
Correct |
15 ms |
27952 KB |
Output is correct |
12 |
Correct |
4 ms |
27952 KB |
Output is correct |
13 |
Correct |
3 ms |
27952 KB |
Output is correct |
14 |
Correct |
14 ms |
27952 KB |
Output is correct |
15 |
Correct |
13 ms |
27952 KB |
Output is correct |
16 |
Correct |
5 ms |
27952 KB |
Output is correct |
17 |
Correct |
40 ms |
27952 KB |
Output is correct |
18 |
Correct |
10 ms |
27952 KB |
Output is correct |
19 |
Correct |
7 ms |
27952 KB |
Output is correct |
20 |
Correct |
8 ms |
27952 KB |
Output is correct |
21 |
Correct |
5 ms |
27952 KB |
Output is correct |
22 |
Correct |
6 ms |
27952 KB |
Output is correct |
23 |
Correct |
18 ms |
27952 KB |
Output is correct |
24 |
Correct |
35 ms |
27952 KB |
Output is correct |
25 |
Correct |
31 ms |
27952 KB |
Output is correct |
26 |
Correct |
18 ms |
27952 KB |
Output is correct |
27 |
Correct |
14 ms |
27952 KB |
Output is correct |
28 |
Correct |
49 ms |
27952 KB |
Output is correct |
29 |
Correct |
183 ms |
27952 KB |
Output is correct |
30 |
Correct |
44 ms |
27952 KB |
Output is correct |
31 |
Correct |
93 ms |
27952 KB |
Output is correct |
32 |
Correct |
68 ms |
27952 KB |
Output is correct |
33 |
Correct |
314 ms |
28380 KB |
Output is correct |
34 |
Correct |
279 ms |
28380 KB |
Output is correct |
35 |
Correct |
367 ms |
28380 KB |
Output is correct |
36 |
Correct |
49 ms |
28380 KB |
Output is correct |
37 |
Correct |
631 ms |
41816 KB |
Output is correct |
38 |
Correct |
603 ms |
41816 KB |
Output is correct |
39 |
Correct |
593 ms |
41816 KB |
Output is correct |
40 |
Correct |
570 ms |
41816 KB |
Output is correct |
41 |
Correct |
554 ms |
41816 KB |
Output is correct |
42 |
Correct |
32 ms |
41816 KB |
Output is correct |
43 |
Correct |
29 ms |
41816 KB |
Output is correct |
44 |
Correct |
30 ms |
41816 KB |
Output is correct |
45 |
Execution timed out |
1097 ms |
110064 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
110064 KB |
Output is correct |
2 |
Correct |
2 ms |
110064 KB |
Output is correct |
3 |
Correct |
2 ms |
110064 KB |
Output is correct |
4 |
Correct |
2 ms |
110064 KB |
Output is correct |
5 |
Correct |
2 ms |
110064 KB |
Output is correct |
6 |
Correct |
2 ms |
110064 KB |
Output is correct |
7 |
Correct |
2 ms |
110064 KB |
Output is correct |
8 |
Correct |
2 ms |
110064 KB |
Output is correct |
9 |
Correct |
2 ms |
110064 KB |
Output is correct |
10 |
Correct |
5 ms |
110064 KB |
Output is correct |
11 |
Correct |
15 ms |
110064 KB |
Output is correct |
12 |
Correct |
3 ms |
110064 KB |
Output is correct |
13 |
Correct |
3 ms |
110064 KB |
Output is correct |
14 |
Correct |
13 ms |
110064 KB |
Output is correct |
15 |
Correct |
13 ms |
110064 KB |
Output is correct |
16 |
Correct |
4 ms |
110064 KB |
Output is correct |
17 |
Correct |
37 ms |
110064 KB |
Output is correct |
18 |
Correct |
6 ms |
110064 KB |
Output is correct |
19 |
Correct |
7 ms |
110064 KB |
Output is correct |
20 |
Correct |
12 ms |
110064 KB |
Output is correct |
21 |
Correct |
6 ms |
110064 KB |
Output is correct |
22 |
Correct |
9 ms |
110064 KB |
Output is correct |
23 |
Correct |
27 ms |
110064 KB |
Output is correct |
24 |
Correct |
41 ms |
110064 KB |
Output is correct |
25 |
Correct |
23 ms |
110064 KB |
Output is correct |
26 |
Correct |
18 ms |
110064 KB |
Output is correct |
27 |
Correct |
14 ms |
110064 KB |
Output is correct |
28 |
Correct |
48 ms |
110064 KB |
Output is correct |
29 |
Correct |
147 ms |
110064 KB |
Output is correct |
30 |
Correct |
62 ms |
110064 KB |
Output is correct |
31 |
Correct |
86 ms |
110064 KB |
Output is correct |
32 |
Correct |
82 ms |
110064 KB |
Output is correct |
33 |
Correct |
277 ms |
110064 KB |
Output is correct |
34 |
Correct |
305 ms |
110064 KB |
Output is correct |
35 |
Correct |
331 ms |
110064 KB |
Output is correct |
36 |
Correct |
48 ms |
110064 KB |
Output is correct |
37 |
Correct |
590 ms |
110064 KB |
Output is correct |
38 |
Correct |
561 ms |
110064 KB |
Output is correct |
39 |
Correct |
573 ms |
110064 KB |
Output is correct |
40 |
Correct |
592 ms |
110064 KB |
Output is correct |
41 |
Correct |
581 ms |
110064 KB |
Output is correct |
42 |
Correct |
36 ms |
110064 KB |
Output is correct |
43 |
Correct |
29 ms |
110064 KB |
Output is correct |
44 |
Correct |
24 ms |
110064 KB |
Output is correct |
45 |
Execution timed out |
1097 ms |
111596 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |