http://www.techiedelight.com/least-cost-path-weighted-digraph-using-bfs/
Consider a directed graph where weight of its edges can be one of x, 2x or 3x (x is a given integer), compute the least cost path from source to destination efficiently.
The idea is to modify the input graph in such a way that all its edges have same weight. For edges having weight 3x, we split them into three edges of weight x each. Similarly, edges having weight 2x gets split into two edges of weight x each. Nothing needs to be done for edges already having weight x. Special care has to be taken while introducing new edges in the graph such that we should not introduce new routes into the graph. So in order to split an edge of weight 3x, we need to create two new vertices in the graph instead of using existing vertices. Similarly, in order to split edge having weight 2x, we need to create one new vertex. Lets illustrate this with the help of a diagram.
Split edge (v, u) having weight 3x into three edges (v, v+N), (v+N, v+2N) and (v+2N, u) each having weight x.
// data structure to store graph edges
struct Edge {
int source, dest, weight;
};
// class to represent a graph object
class Graph
{
public:
// A array of vectors to represent adjacency list
vector<int> adjList[3*N];
// Constructor
Graph(vector<Edge> edges, int x)
{
// add edges to the undirected graph
for (unsigned i = 0; i < edges.size(); i++)
{
int v = edges[i].source;
int u = edges[i].dest;
int weight = edges[i].weight;
// create two new vertices v+N and v+2*N if the weight
// of edge is 3x. Also, split the edge (v, u) into (v, v+N),
// (v+N, v+2N) and (v+2N, u) each having weight x
if (weight == 3*x)
{
adjList[v].push_back(v + N);
adjList[v + N].push_back(v + 2 * N);
adjList[v + 2 * N].push_back(u);
}
// create one new vertex v+N if the weight of the edge
// is 2x. Also split the edge (v, u) into (v, v+N),
// (v+N, u) each having weight x
else if (weight == 2*x)
{
adjList[v].push_back(v + N);
adjList[v + N].push_back(u);
}
// no splitting is needed if edge weight is 1x
else
adjList[v].push_back(u);
}
}
};
// Recursive function to print path of given vertex v from
// the source vertex
void printPath(vector<int> predecessor, int v, int& cost)
{
if (v < 0)
return;
printPath(predecessor, predecessor[v], cost);
cost++;
// consider only original nodes present in the graph
if (v < N)
cout << v << " ";
}
// Perform BFS on graph starting from vertex source
void BFS(Graph const& graph, int source, int dest)
{
// stores vertex is discovered in BFS traversal or not
vector<bool> discovered(3*N, false);
// mark source vertex as discovered
discovered[source] = true;
// predecessor[] stores predecessor information. It is used
// to trace least cost path from destination back to source.
vector<int> predecessor(3*N, -1);
// create a queue used to do BFS and push source vertex
// into the queue
queue<int> q;
q.push(source);
// run till queue is not empty
while (!q.empty())
{
// pop front node from queue and print it
int curr = q.front();
q.pop();
// if destination vertex is reached
if (curr == dest)
{
int cost = -1;
cout << "Least cost path between " << source << " and " <<
dest << " is "; printPath(predecessor, dest, cost);
cout << "having cost " << cost;
}
// do for every adjacent edge of current vertex
for (int v : graph.adjList[curr])
{
if (!discovered[v])
{
// mark it discovered and push it into queue
discovered[v] = true;
q.push(v);
// set curr as predecessor of vertex v
predecessor[v] = curr;
}
}
}
}