#include<bits/stdc++.h>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;
template<class A, class B>
bool maximize(A& x, B y) {
if (x < y) return x = y, true; else return false;
}
template<class A, class B>
bool minimize(A& x, B y) {
if (x > y) return x = y, true; else return false;
}
/* END OF TEMPLATE */
const int N = 2e3 + 7;
const int M = 3e4 + 7;
int n, m, d[N][N], a[M], b[M];
vector<int> v[N];
int col0 = 0, col1 = 1;
void bfs(int s, int x, int t) {
deque<pii> dq;
FOR(i, 0, n)
FOR(j, 0, n) d[i][j] = 1e9;
dq.push_front({s, x});
d[s][x] = 0;
while (!dq.empty()) {
pii tmp = dq.front();
dq.pop_front();
int x = tmp.X, y = tmp.Y;
if (x == t) {
cout<<d[x][y];
exit(0);
}
for (auto a : v[x])
if (minimize(d[x][a], d[x][y])) dq.push_front({x, a});
v[x].clear();
if (x - y >= 0 && minimize(d[x - y][y], d[x][y] + 1)) dq.push_back({x - y, y});
if (x + y < n && minimize(d[x + y][y], d[x][y] + 1)) dq.push_back({x + y, y});
}
cout<<-1;
}
int main() {
// freopen("APIO15_SKYSCRAPER.inp", "r", stdin);
// freopen("APIO15_SKYSCRAPER.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
REP(i, 0, m) {
cin>>a[i]>>b[i];
v[a[i]].push_back(b[i]);
}
bfs(a[0], b[0], a[1]);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
3 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
6704 KB |
Output is correct |
18 |
Correct |
3 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
16220 KB |
Output is correct |
20 |
Correct |
4 ms |
16228 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
5 ms |
14680 KB |
Output is correct |
23 |
Correct |
3 ms |
14680 KB |
Output is correct |
24 |
Correct |
5 ms |
15452 KB |
Output is correct |
25 |
Correct |
4 ms |
16220 KB |
Output is correct |
26 |
Correct |
4 ms |
16220 KB |
Output is correct |
27 |
Correct |
5 ms |
16220 KB |
Output is correct |
28 |
Correct |
5 ms |
16220 KB |
Output is correct |
29 |
Correct |
4 ms |
16220 KB |
Output is correct |
30 |
Correct |
4 ms |
16220 KB |
Output is correct |
31 |
Correct |
6 ms |
16220 KB |
Output is correct |
32 |
Correct |
4 ms |
16220 KB |
Output is correct |
33 |
Correct |
6 ms |
16220 KB |
Output is correct |
34 |
Correct |
5 ms |
16220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2560 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2536 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2640 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
6488 KB |
Output is correct |
18 |
Correct |
3 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
16220 KB |
Output is correct |
20 |
Correct |
4 ms |
16220 KB |
Output is correct |
21 |
Correct |
1 ms |
4440 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14684 KB |
Output is correct |
24 |
Correct |
4 ms |
15452 KB |
Output is correct |
25 |
Correct |
4 ms |
16208 KB |
Output is correct |
26 |
Correct |
5 ms |
16216 KB |
Output is correct |
27 |
Correct |
4 ms |
16216 KB |
Output is correct |
28 |
Correct |
5 ms |
16220 KB |
Output is correct |
29 |
Correct |
5 ms |
16220 KB |
Output is correct |
30 |
Correct |
4 ms |
16096 KB |
Output is correct |
31 |
Correct |
4 ms |
16220 KB |
Output is correct |
32 |
Correct |
4 ms |
16148 KB |
Output is correct |
33 |
Correct |
5 ms |
16220 KB |
Output is correct |
34 |
Correct |
5 ms |
16220 KB |
Output is correct |
35 |
Correct |
8 ms |
13148 KB |
Output is correct |
36 |
Correct |
3 ms |
10844 KB |
Output is correct |
37 |
Correct |
7 ms |
16220 KB |
Output is correct |
38 |
Correct |
9 ms |
16984 KB |
Output is correct |
39 |
Correct |
8 ms |
16732 KB |
Output is correct |
40 |
Correct |
8 ms |
16732 KB |
Output is correct |
41 |
Correct |
9 ms |
16988 KB |
Output is correct |
42 |
Correct |
7 ms |
16728 KB |
Output is correct |
43 |
Correct |
8 ms |
16732 KB |
Output is correct |
44 |
Correct |
9 ms |
16728 KB |
Output is correct |
45 |
Correct |
13 ms |
16988 KB |
Output is correct |
46 |
Correct |
10 ms |
16916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2464 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2588 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
3 ms |
14684 KB |
Output is correct |
19 |
Correct |
4 ms |
16220 KB |
Output is correct |
20 |
Correct |
4 ms |
16220 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
3 ms |
14684 KB |
Output is correct |
23 |
Correct |
3 ms |
14684 KB |
Output is correct |
24 |
Correct |
4 ms |
15452 KB |
Output is correct |
25 |
Correct |
4 ms |
16192 KB |
Output is correct |
26 |
Correct |
4 ms |
16220 KB |
Output is correct |
27 |
Correct |
4 ms |
16220 KB |
Output is correct |
28 |
Correct |
5 ms |
16216 KB |
Output is correct |
29 |
Correct |
5 ms |
16220 KB |
Output is correct |
30 |
Correct |
4 ms |
16220 KB |
Output is correct |
31 |
Correct |
4 ms |
16220 KB |
Output is correct |
32 |
Correct |
4 ms |
16220 KB |
Output is correct |
33 |
Correct |
7 ms |
16220 KB |
Output is correct |
34 |
Correct |
5 ms |
16164 KB |
Output is correct |
35 |
Correct |
7 ms |
13148 KB |
Output is correct |
36 |
Correct |
3 ms |
10844 KB |
Output is correct |
37 |
Correct |
7 ms |
16292 KB |
Output is correct |
38 |
Correct |
10 ms |
16988 KB |
Output is correct |
39 |
Correct |
8 ms |
16928 KB |
Output is correct |
40 |
Correct |
8 ms |
16728 KB |
Output is correct |
41 |
Correct |
8 ms |
17240 KB |
Output is correct |
42 |
Correct |
8 ms |
16732 KB |
Output is correct |
43 |
Correct |
7 ms |
16732 KB |
Output is correct |
44 |
Correct |
7 ms |
16732 KB |
Output is correct |
45 |
Correct |
14 ms |
17048 KB |
Output is correct |
46 |
Correct |
10 ms |
17120 KB |
Output is correct |
47 |
Runtime error |
2 ms |
1116 KB |
Execution killed with signal 11 |
48 |
Halted |
0 ms |
0 KB |
- |