#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
queue<pair<int, int>> q;
vector<int> adj[30005];
int dist[2005][2005];
bool vis[2005];
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
for (int i = 0; i <= 2000; i++) {
for (int j = 0; j <= 2000; j++) dist[i][j] = -1;
}
for (auto x : adj[0]) {
q.push({0, x});
dist[0][x] = 0;
}
vis[0] = 1;
while (!q.empty()) {
int x = q.front().fi, d = q.front().se;
q.pop();
if (x == 1) {
cout << dist[x][d] << "\n";
return 0;
}
if (x - d >= 0 and !vis[x - d]) {
for (auto u : adj[x - d]) {
q.push({x - d, u});
dist[x - d][u] = dist[x][d] + 1;
}
}
if (x - d >= 0 and dist[x - d][d] == -1) {
q.push({x - d, d});
dist[x - d][d] = dist[x][d] + 1;
}
if (x + d < n and !vis[x + d]) {
for (auto u : adj[x + d]) {
q.push({x + d, u});
dist[x + d][u] = dist[x][d] + 1;
}
}
if (x + d < n and dist[x + d][d] == -1) {
q.push({x + d, d});
dist[x + d][d] = dist[x][d] + 1;
}
}
cout << -1 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Incorrect |
7 ms |
16708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16632 KB |
Output is correct |
2 |
Incorrect |
7 ms |
16632 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16612 KB |
Output is correct |
2 |
Incorrect |
7 ms |
16644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
16724 KB |
Output is correct |
2 |
Incorrect |
7 ms |
16652 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16724 KB |
Output is correct |
2 |
Incorrect |
7 ms |
16724 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |