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<cassert>
#include<cstring>
#include<map>
#include<set>
#include<time.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<utility>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<limits>
using namespace std;
long long gcd( long long b, long long s ){
return (s!=0) ? gcd( s, b%s ) : b;
}
const int N = 100005;
const int INF = 1000000007;
vector <int> e[N];
int d[N];
int main(){
int n, m;
scanf("%d %d", &n, &m);
for(int i=0; i<m; i++){
int x, y;
scanf("%d %d", &x, &y);
e[x].push_back( y );
}
queue <int> q;
for(int i=1; i<=n; i++)
d[i] = INF;
q.push( 1 );
d[1] = 0;
while( !q.empty() ){
int p = q.front(); q.pop();
for(int i=0; i<e[p].size(); i++){
int u = e[p][i];
if( d[p]+1 < d[u] ){
d[u] = d[p] + 1;
q.push( u );
}
}
}
if( d[n] != INF )
printf("%d\n", d[n]);
else
puts("-1");
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |