// EXPLOSION!
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include<chrono>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpll;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define pb push_back
#define mp make_pair
#define rsz resize
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define f first
#define s second
#define cont continue
#define endl '\n'
//#define ednl '\n'
#define test int testc;cin>>testc;while(testc--)
#define pr(a, b) trav(x,a)cerr << x << b; cerr << endl;
#define message cout << "Hello World" << endl;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; // for every grid problem!!
const ll linf = 4000000000000000000LL;
const ll inf = 998244353;// 1000000007
void pv(vi a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vll a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vector<vi>a) {
F0R(i, sz(a)) { cout << i << endl; pv(a[i]); cout << endl; }
}void pv(vector<vll>a) { F0R(i, sz(a)) { cout << i << endl; pv(a[i]); }cout << endl; }void pv(vector<string>a) { trav(x, a)cout << x << endl; cout << endl; }
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef arwaeystoamneg
if (sz(s))
{
freopen((s + ".in").c_str(), "r", stdin);
if (s != "test1")
freopen((s + ".out").c_str(), "w", stdout);
}
#endif
}
const int MAX = 33333, k = 170;
int n, m;
pii a[MAX];
int dist[MAX][k];
vi b[MAX];
int main()
{
setIO("test1");
cin >> m >> n;
F0R(i, n)cin >> a[i].f >> a[i].s;
F0R(i, n)b[a[i].f].pb(a[i].s);
F0R(i, m)fill(dist[i], dist[i] + k, inf);
dist[a[0].f][0] = 0;
// dist, position, len. len = 0 means too big
priority_queue<pair<int, pii>, vector<pair<int, pii>>, greater<pair<int, pii>>>todo;
todo.push({ 0,{a[0].f,0} });
while (sz(todo))
{
pair<int, pii> t = todo.top();
todo.pop();
int i = t.s.f, j = t.s.s;
if (dist[i][j] != t.f)continue;
if (j == 0)
{
trav(x, b[i])
{
if (x >= k)
{
int c = 0;
for (int j = i; j >= 0; j -= x, c++)
{
if (dist[j][0] > dist[i][0] + c)
{
dist[j][0] = dist[i][0] + c;
todo.push({ dist[j][0], {j,0} });
}
}
c = 1;
for (int j = i + x; j < m; j += x, c++)
{
if (dist[j][0] > dist[i][0] + c)
{
dist[j][0] = dist[i][0] + c;
todo.push({ dist[j][0], {j,0} });
}
}
}
else
{
dist[i][x] = dist[i][0];
todo.push({ dist[i][x],{i,x} });
}
}
}
else
{
if (i - j >= 0 && dist[i - j][j] > dist[i][j] + 1)
{
dist[i - j][j] = dist[i][j] + 1;
todo.push({ dist[i - j][j], {i - j, j} });
if (dist[i - j][0] > dist[i - j][j])
{
dist[i - j][0] = dist[i - j][j];
todo.push({ dist[i - j][0],{i - j, 0} });
}
}
if (i + j < m && dist[i + j][j] > dist[i][j] + 1)
{
dist[i + j][j] = dist[i][j] + 1;
todo.push({ dist[i + j][j], {i + j, j} });
if (dist[i + j][0] > dist[i + j][j])
{
dist[i + j][0] = dist[i + j][j];
todo.push({ dist[i + j][0],{i + j, 0} });
}
}
}
}
cout << (dist[a[1].f][0] == inf ? -1 : dist[a[1].f][0]) << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1228 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1024 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1228 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 KB |
Output is correct |
17 |
Correct |
3 ms |
1612 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
2 ms |
2508 KB |
Output is correct |
21 |
Correct |
1 ms |
1356 KB |
Output is correct |
22 |
Correct |
2 ms |
2252 KB |
Output is correct |
23 |
Correct |
3 ms |
2124 KB |
Output is correct |
24 |
Correct |
3 ms |
2412 KB |
Output is correct |
25 |
Correct |
3 ms |
2380 KB |
Output is correct |
26 |
Correct |
3 ms |
2508 KB |
Output is correct |
27 |
Correct |
2 ms |
2508 KB |
Output is correct |
28 |
Correct |
3 ms |
2508 KB |
Output is correct |
29 |
Correct |
9 ms |
2380 KB |
Output is correct |
30 |
Correct |
4 ms |
2380 KB |
Output is correct |
31 |
Correct |
6 ms |
2380 KB |
Output is correct |
32 |
Correct |
5 ms |
2380 KB |
Output is correct |
33 |
Correct |
17 ms |
2548 KB |
Output is correct |
34 |
Correct |
17 ms |
2540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1216 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1228 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 KB |
Output is correct |
17 |
Correct |
3 ms |
1612 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
2 ms |
2508 KB |
Output is correct |
21 |
Correct |
1 ms |
1356 KB |
Output is correct |
22 |
Correct |
2 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
3 ms |
2380 KB |
Output is correct |
25 |
Correct |
3 ms |
2380 KB |
Output is correct |
26 |
Correct |
3 ms |
2508 KB |
Output is correct |
27 |
Correct |
2 ms |
2508 KB |
Output is correct |
28 |
Correct |
3 ms |
2508 KB |
Output is correct |
29 |
Correct |
9 ms |
2380 KB |
Output is correct |
30 |
Correct |
4 ms |
2380 KB |
Output is correct |
31 |
Correct |
6 ms |
2380 KB |
Output is correct |
32 |
Correct |
4 ms |
2380 KB |
Output is correct |
33 |
Correct |
17 ms |
2508 KB |
Output is correct |
34 |
Correct |
17 ms |
2540 KB |
Output is correct |
35 |
Correct |
16 ms |
2580 KB |
Output is correct |
36 |
Correct |
4 ms |
1868 KB |
Output is correct |
37 |
Correct |
23 ms |
2920 KB |
Output is correct |
38 |
Correct |
22 ms |
3020 KB |
Output is correct |
39 |
Correct |
22 ms |
3020 KB |
Output is correct |
40 |
Correct |
22 ms |
3020 KB |
Output is correct |
41 |
Correct |
22 ms |
3064 KB |
Output is correct |
42 |
Correct |
10 ms |
3404 KB |
Output is correct |
43 |
Correct |
10 ms |
3404 KB |
Output is correct |
44 |
Correct |
10 ms |
3328 KB |
Output is correct |
45 |
Correct |
58 ms |
3320 KB |
Output is correct |
46 |
Correct |
62 ms |
3316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1356 KB |
Output is correct |
15 |
Correct |
2 ms |
1228 KB |
Output is correct |
16 |
Correct |
1 ms |
1228 KB |
Output is correct |
17 |
Correct |
3 ms |
1612 KB |
Output is correct |
18 |
Correct |
2 ms |
2252 KB |
Output is correct |
19 |
Correct |
2 ms |
2380 KB |
Output is correct |
20 |
Correct |
2 ms |
2508 KB |
Output is correct |
21 |
Correct |
1 ms |
1356 KB |
Output is correct |
22 |
Correct |
2 ms |
2252 KB |
Output is correct |
23 |
Correct |
2 ms |
2124 KB |
Output is correct |
24 |
Correct |
3 ms |
2392 KB |
Output is correct |
25 |
Correct |
2 ms |
2380 KB |
Output is correct |
26 |
Correct |
3 ms |
2508 KB |
Output is correct |
27 |
Correct |
2 ms |
2508 KB |
Output is correct |
28 |
Correct |
3 ms |
2524 KB |
Output is correct |
29 |
Correct |
10 ms |
2468 KB |
Output is correct |
30 |
Correct |
3 ms |
2440 KB |
Output is correct |
31 |
Correct |
6 ms |
2380 KB |
Output is correct |
32 |
Correct |
5 ms |
2464 KB |
Output is correct |
33 |
Correct |
17 ms |
2636 KB |
Output is correct |
34 |
Correct |
17 ms |
2508 KB |
Output is correct |
35 |
Correct |
16 ms |
2636 KB |
Output is correct |
36 |
Correct |
4 ms |
1868 KB |
Output is correct |
37 |
Correct |
23 ms |
2892 KB |
Output is correct |
38 |
Correct |
21 ms |
3064 KB |
Output is correct |
39 |
Correct |
22 ms |
3060 KB |
Output is correct |
40 |
Correct |
22 ms |
3056 KB |
Output is correct |
41 |
Correct |
22 ms |
3020 KB |
Output is correct |
42 |
Correct |
10 ms |
3404 KB |
Output is correct |
43 |
Correct |
10 ms |
3420 KB |
Output is correct |
44 |
Correct |
10 ms |
3388 KB |
Output is correct |
45 |
Correct |
58 ms |
3340 KB |
Output is correct |
46 |
Correct |
60 ms |
3340 KB |
Output is correct |
47 |
Correct |
95 ms |
10364 KB |
Output is correct |
48 |
Correct |
16 ms |
17100 KB |
Output is correct |
49 |
Correct |
16 ms |
18580 KB |
Output is correct |
50 |
Correct |
15 ms |
20056 KB |
Output is correct |
51 |
Correct |
60 ms |
22404 KB |
Output is correct |
52 |
Correct |
67 ms |
22432 KB |
Output is correct |
53 |
Correct |
27 ms |
21836 KB |
Output is correct |
54 |
Correct |
13 ms |
20940 KB |
Output is correct |
55 |
Correct |
13 ms |
21068 KB |
Output is correct |
56 |
Correct |
25 ms |
22152 KB |
Output is correct |
57 |
Correct |
93 ms |
21176 KB |
Output is correct |
58 |
Correct |
25 ms |
22028 KB |
Output is correct |
59 |
Correct |
27 ms |
22020 KB |
Output is correct |
60 |
Correct |
31 ms |
22036 KB |
Output is correct |
61 |
Correct |
28 ms |
21964 KB |
Output is correct |
62 |
Correct |
53 ms |
22340 KB |
Output is correct |
63 |
Correct |
67 ms |
23128 KB |
Output is correct |
64 |
Correct |
73 ms |
23120 KB |
Output is correct |
65 |
Correct |
313 ms |
23240 KB |
Output is correct |
66 |
Correct |
873 ms |
24668 KB |
Output is correct |
67 |
Correct |
886 ms |
24872 KB |
Output is correct |