  #include <iostream>

#include <queue>

#include <vector>

#include <climits>

using namespace std;

// Number of vertices in the graph

#define N 8

// structure to store graph

struct Edge {

int src, dest, weight;

};

// class to represent a graph object

class Graph

{

public:

// A array of vectors to represent adjacency list

// Constructor

Graph(vector<Edge> edges)

{

// add edges to the directed graph

for (int i = 0; i < edges.size(); i++)

{

int src = edges[i].src;

int dest = edges[i].dest;

int weight = edges[i].weight;

}

}

};

// BFS Node

struct Node {

int vertex, depth, weight;

};

// Perform BFS on graph g starting from vertex v

int modifiedBFS(Graph const &g, int src, int dest, int m)

{

// create a queue used to do BFS

queue<Node> q;

// push vertex into the queue

q.push({src, 0, 0});

// stores least- from source to

int minCost = INT_MAX;

// run till queue is not empty

while (!q.empty())

{

// pop front node from queue

Node node = q.front();

q.pop();

int v = node.vertex;

int depth = node.depth;

int cost = node.weight;

// if destination is reached and BFS depth is equal to m

// update minimum cost calculated so far

if (v == dest && depth == m)

minCost = min(minCost, cost);

// don’t consider nodes having BFS depth more than m.

// This check will result in optimized code and also

// handle cycles in the graph (else loop will never break)

if (depth > m)

break;

// do for every adjacent edge of v

{

// push every vertex (discovered or undiscovered) into

// the queue with depth as +1 of parent and cost equal

// to cost of parent plus weight of current edge

q.push( {edge.dest, depth + 1, cost + edge.weight} );

}

}

// return least-cost

return minCost;

}

// main function

int main()

{

// vector of graph edges as per above diagram

vector<Edge> edges =

{

{0, 6, 1}, {0, 1, 5}, {1, 6, 3}, {1, 5, 5}, {1, 2, 7},

{2, 3, 8}, {3, , }, {5, 2, 1}, {5, 3, 9}, {5, 4, 1},

{6, 5, 2}, {7, 6, 9}, {7, 1, 6}

};

// create a graph from edges

Graph g(edges);

int src = 0, dest = 3, m = 4;

// Do modified BFS traversal from source vertex src

cout << modifiedBFS(g, src, dest, m);

return 0;

}

SHARE