이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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]) {
vis[x - d] = 1;
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]) {
vis[x + d] = 1;
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";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |