/* 문제
*/
// 2014.
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <string>
#include <map>
using namespace std;
#define maxS 100001
vector <int> x[maxS];
queue <int> qu;
int N, M , ans = 2100000000;
bool visited[maxS];
int cmp1(int i, int j)
{
return i > j;
}
void bfs()
{
qu.push(1);
int m = 0;
while (!qu.empty())
{
int tmp = qu.front();
visited[tmp] = true;
qu.pop();
int sz = x[tmp].size();
for (int i = 0; i < sz; i++)
{
if (x[tmp][i] == N)
{
ans = m + 1;
return;
}
if (visited[x[tmp][i]] == false)
qu.push(x[tmp][i]);
}
m++;
}
return;
}
int main()
{
scanf("%d%d", &N, &M);
for (int i = 0; i < M; i++)
{
int n, m;
scanf("%d%d", &n, &m);
x[n].push_back(m);
}
for (int i = 1; i <= N; i++)
sort(x[i].begin(), x[i].end(),cmp1);
bfs();
if (ans == 2100000000)
printf("-1\n");
else
printf("%d\n", ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3684 KB |
Output is correct |
2 |
Correct |
0 ms |
3684 KB |
Output is correct |
3 |
Incorrect |
0 ms |
3684 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |