-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestFirstSearch.cpp
More file actions
59 lines (57 loc) · 1.27 KB
/
Copy pathBestFirstSearch.cpp
File metadata and controls
59 lines (57 loc) · 1.27 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
#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
vector<vector<pi>> graph;
void addedge(int x, int y, int cost)
{
graph[x].push_back(make_pair(cost,y));
graph[x].push_back(make_pair(cost,x));
}
void bfs(int ac, int target, int n)
{
vector<bool> visited(n,false);
priority_queue<pi, vector<pi>, greater<pi>> pq;
pq.push(make_pair(0,ac));
int s=ac;
visited[s]=true;
while (!pq.empty())
{
int x=pq.top().second;
cout<<x<<" ";
pq.pop();
if(x==target)
{
break;
}
for(int i=0;i<graph[x].size();i++)
{
if(!visited[graph[x][i].second])
{
visited[graph[x][i].second]=true;
pq.push(make_pair(graph[x][i].first,graph[x][i].second));
}
}
}
}
int main()
{
int v=14;
graph.resize(v);
addedge(0,1,3);
addedge(0,2,6);
addedge(0,3,5);
addedge(1,4,9);
addedge(1,5,8);
addedge(2,6,12);
addedge(2,7,14);
addedge(3,8,7);
addedge(8,9,5);
addedge(8,10,6);
addedge(9,11,1);
addedge(9,12,10);
addedge(9,13,2);
int source=0;
int target=9;
bfs(source,target,v);
return 0;
}