-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5214.cpp
More file actions
61 lines (50 loc) · 1.06 KB
/
5214.cpp
File metadata and controls
61 lines (50 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#define N 100005
#define M 1005
using namespace std;
int n, k, m, d[N + M];
vector<vector<int>> arr;
queue<int> q;
bool check[N + M];
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int i, j, temp;
cin >> n >> k >> m;
arr.resize(n + m + 1);
for (i = 0; i < m; i++)
{
for (j = 0; j < k; j++)
{
cin >> temp;
arr[i + n + 1].push_back(temp);
arr[temp].push_back(i + n + 1);
}
}
q.push(1);
check[1] = true;
while (!q.empty())
{
int head = q.front();
q.pop();
if (head == n)
{
cout << d[head] / 2 + 1;
return 0;
}
for (i = 0; i < arr[head].size(); i++)
{
if (check[arr[head][i]])
continue;
check[arr[head][i]]=true;
d[arr[head][i]] = d[head] + 1;
q.push(arr[head][i]);
}
}
cout << "-1";
return 0;
}