-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbreadth_first_search.cpp
executable file
·54 lines (45 loc) · 1003 Bytes
/
breadth_first_search.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
const int N = 1e5 + 9, M = 2e5 + 9, oo = 0x3f3f3f3f;
ll INF = 0x3f3f3f3f3f3f3f3f;
int Head[N], Par[N], Next[M], To[M], Cost[M], ne, n, m, u, v, st, tr, tax;
ll dis[N];
void addEdge(int from, int to, int cost) {
Next[++ne] = Head[from];
Head[from] = ne;
Cost[ne] = cost;
To[ne] = to;
}
void _clear() {
memset(Head, 0, sizeof(Head[0]) * (n + 2));
ne = 0;
}
void BFS(int src) {
memset(dis, 0x3f, sizeof(dis[0]) * (n + 2));
memset(Par, -1, sizeof(Par[0]) * (n + 2));
queue <int> q;
q.push(src);
dis[src] = 0;
int u;
while(q.size()) {
u = q.front(); q.pop();
for(int i = Head[u]; i; i = Next[i])
if(dis[To[i]] == oo) {
dis[To[i]] = dis[u] + 1;
Par[To[i]] = u;
q.push(To[i]);
}
}
}
int main()
{
cin >> n >> m >> st >> tr;
while(m--) {
cin >> u >> v >> tax;
addEdge(u, v, tax);
addEdge(v, u, tax);
}
BFS(st);
cout << dis[tr] << endl;
}