-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2644.cpp
More file actions
48 lines (43 loc) · 984 Bytes
/
2644.cpp
File metadata and controls
48 lines (43 loc) · 984 Bytes
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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n, targetA, targetB, m, x, y, current, nxt, d[101];
vector<vector<int>> v;
bool visited[101];
queue<int> q;
bool checker, fin;
void bfs(int ta, int tb)
{
q.push(ta);
visited[ta] = true;
while (!q.empty())
{
current = q.front();
q.pop();
for (int i = 0; i < v[current].size(); ++i)
{
nxt = v[current][i];
if (!visited[nxt])
{
q.push(nxt);
visited[nxt] = true;
d[nxt] = d[current] + 1;
}
}
}
cout << (d[tb] == 0 ? -1 : d[tb]) << endl;
}
int main()
{
cin >> n >> targetA >> targetB >> m;
v.resize(n);
while (m--)
{
cin >> x >> y;
v[x - 1].push_back(y - 1);
v[y - 1].push_back(x - 1);
}
bfs(targetA - 1, targetB - 1);
}
// dp처럼 각 점으로 부터의 거리를 하나씩 추가해줘야함