This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 200000;
int n, m, d[N];
bool vst[N];
vector<int> graph[N];
queue<int> q;
int main(void) {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++)
d[i] = 2*N;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--, v--;
graph[u].push_back(v);
}
d[0] = 0;
q.push(0);
while (!q.empty()) {
int u = q.front();
q.pop();
if (vst[u]) continue;
vst[u] = true;
for (int ed = 0; ed < (int)graph[u].size(); ed++) {
int v = graph[u][ed];
if (vst[v]) continue;
d[v] = min(d[v], d[u]+1);
q.push(v);
}
}
if (vst[n-1]) printf("%d\n", d[n-1]);
else printf("-1\n");
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |