-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0-1_BFS.cpp
108 lines (88 loc) · 1.86 KB
/
0-1_BFS.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <queue>
#include <stack>
#include <time.h>
#define MAX_SIZE 100007
using namespace std;
vector<pair<int, int>> adj[MAX_SIZE];
int edge = 0;
int vertex = 0;
bool visited[MAX_SIZE];
int trace[MAX_SIZE];
int weight[MAX_SIZE];
void input()
{
cin >> vertex >> edge;
for (int i = 0; i < edge; i++)
{
int x, y, s;
cin >> x >> y >> s;
adj[x].push_back({ y, s });
//adj[y].push_back({ x, s });
}
for (int i = 0; i < vertex; i++)
{
visited[i] = false;
}
}
void BFS(int start_vertex)
{
queue <int> q;
q.push(start_vertex);
visited[start_vertex] = true;
while (!q.empty())
{
int v = q.front();
q.pop();
for (auto i = 0; i < adj[v].size(); i++)
{
if (visited[adj[v][i].first] == false)
{
q.push(adj[v][i].first);
visited[adj[v][i].first] = true;
trace[adj[v][i].first] = v;
weight[v] = adj[v][i].second;
}
}
}
}
void printShortestPath(int start_vertex, int finish_vertex)
{
int weight_sum = 0;
stack<int> shortest_trace;
shortest_trace.push(finish_vertex);
int j = finish_vertex;
while (trace[j] != start_vertex)
{
shortest_trace.push(trace[j]);
j = trace[j];
weight_sum += weight[j];
}
shortest_trace.push(start_vertex);
weight_sum += weight[start_vertex];
cout << shortest_trace.top();
shortest_trace.pop();
while (!shortest_trace.empty())
{
cout << " --> " << shortest_trace.top();
shortest_trace.pop();
}
cout << "\nShortest path: " << weight_sum << endl;
cout << endl;
}
int main()
{
input();
int start_vertex = 1;
int finish_vertex = vertex;
clock_t s, e;
double time_use;
s = clock();
BFS(start_vertex);
cout << "\nShortest path from " << start_vertex << " to " << finish_vertex << endl;
printShortestPath(start_vertex, finish_vertex);
e = clock();
time_use = (double)(e - s) / CLOCKS_PER_SEC;
cout << "BFS algorithm runtime is: " << time_use << endl;
return 0;
}