Sum of shortest distance on source to destination and back having at least a common vertex

Given a directed weighted graph and the source and destination vertex. The task is to find the sum of shortest distance on the path going from source to destination and and then from destination to source such that both the paths have at least a common vertex other than the source and the destination.

Note: On going from destination to source, all the directions of the edges are reversed.


Input: src = 0, des = 1

Output: 17
Common vertex is 4 and path is 0 -> 4 -> 3 -> 1 -> 4 -> 0

Approach: The idea is to use Dijkstra’s algorithm. On finding the shortest path from source to destination and shortest path from destination to the source using Dijkstra’s algorithm, it may not result in a path where there is at least one node in common except the source and destination vertex.

  • Let s be the source vertex and d be destination vertex and v be the intermediate node common in both the paths from source to destination and destination to source. The shortest pair of paths, so that v is in intersection of this two paths is a path: s -> v -> d -> v -> s and it’s length is

    dis[s][v] + dis[v][d] + dis[d][v] + dis[v][s]

  • Since s and d are fixed, just find v such that it gives shortest path.
  • In order to find such v, follow the below steps:
    1. Find shortest distance from all vertices to s and d which gives us the values of dis[v][s] and dis[v][d]. For finding the shortest path from all the vertices to a given node refer Shortest paths from all vertices to a destination.
    2. Find shortest distance of all vertex from s and d which gives us d[s][v] and d[d][v].
    3. Iterate for all v and find minimum of d[s][v] + d[v][d] + d[d][v] + d[v][s].

Below is the implementation of the above approach:





// CPP implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
// iPair represents the Integer Pair
typedef pair<int, int> iPair;
// This class represents
// a directed graph using
// adjacency list representation
class Graph {
    // Number of vertices
    int V;
    // In a weighted graph, store vertex
    // and weight pair for every edge
    list<pair<int, int> >* adj;
    // Constructor
    Graph(int V);
    // Function to add an edge to graph
    void addEdge(int u, int v, int w);
    // Find shortest path from
    // source vertex to all vertex
    void shortestPath(int src,
                      vector<int>& dist);
// Allocates memory for adjacency list
Graph::Graph(int V)
    this->V = V;
    adj = new list<iPair>[V];
// Function to add an edge to the graph
void Graph::addEdge(int u, int v, int w)
    adj[v].push_back(make_pair(u, w));
// Function to find the shortest paths
// from source to all other vertices
void Graph::shortestPath(int src,
                         vector<int>& dist)
    // Create a priority queue to
    // store vertices that
    // are being preprocessed
                   greater<iPair> >
    // Insert source itself in priority
    // queue and initialize
    // its distance as 0
    pq.push(make_pair(0, src));
    dist[src] = 0;
    // Loop till priority queue
    // becomes empty (or all
    // distances are not finalized)
    while (!pq.empty()) {
        // The first vertex in pair
        // is the minimum distance
        // vertex, extract it from
        // priority queue
        int u =;
        // 'i' is used to get all
        // adjacent vertices of a vertex
        list<pair<int, int> >::iterator i;
        for (i = adj[u].begin(); i != adj[u].end(); ++i) {
            // Get vertex label and
            // weight of current
            // adjacent of u
            int v = (*i).first;
            int weight = (*i).second;
            // If there is shorted
            // path to v through u
            if (dist[v] > dist[u] + weight) {
                // Updating distance of v
                dist[v] = dist[u] + weight;
                pq.push(make_pair(dist[v], v));
// Function to return the
// required minimum path
int minPath(int V, int src, int des,
            Graph g, Graph r)
    // Create a vector for
    // distances and
    // initialize all distances
    // as infinite (INF)
    // To store distance of all
    // vertex from source
    vector<int> dist(V, INF);
    // To store distance of all
    // vertex from destination
    vector<int> dist2(V, INF);
    // To store distance of source
    // from all vertex
    vector<int> dist3(V, INF);
    // To store distance of
    // destination from all vertex
    vector<int> dist4(V, INF);
    // Computing shortest path from
    // source vertex to all vertices
    g.shortestPath(src, dist);
    // Computing shortest path from
    // destination vertex to all vertices
    g.shortestPath(des, dist2);
    // Computing shortest path from
    // all the vertices to source
    r.shortestPath(src, dist3);
    // Computing shortest path from
    // all the vertices to destination
    r.shortestPath(des, dist4);
    // Finding the intermediate node (IN)
    // such that the distance of path
    // src -> IN -> des -> IN -> src is minimum
    // To store the shortest distance
    int ans = INT_MAX;
    for (int i = 0; i < V; i++) {
        // Intermediate node should not be
        // the source and destination
        if (i != des && i != src)
            ans = min(
                dist[i] + dist2[i]
                    + dist3[i] + dist4[i]);
    // Return the minimum path required
    return ans;
// Driver code
int main()
    // Create the graph
    int V = 5;
    int src = 0, des = 1;
    // To store the original graph
    Graph g(V);
    // To store the reverse graph
    // and compute distance from all
    // vertex to a particular vertex
    Graph r(V);
    // Adding edges
    g.addEdge(0, 2, 1);
    g.addEdge(0, 4, 5);
    g.addEdge(1, 4, 1);
    g.addEdge(2, 0, 10);
    g.addEdge(2, 3, 5);
    g.addEdge(3, 1, 1);
    g.addEdge(4, 0, 5);
    g.addEdge(4, 2, 100);
    g.addEdge(4, 3, 5);
    // Adding edges in reverse direction
    r.addEdge(2, 0, 1);
    r.addEdge(4, 0, 5);
    r.addEdge(4, 1, 1);
    r.addEdge(0, 2, 10);
    r.addEdge(3, 2, 5);
    r.addEdge(1, 3, 1);
    r.addEdge(0, 4, 5);
    r.addEdge(2, 4, 100);
    r.addEdge(3, 4, 5);
    cout << minPath(V, src, des, g, r);
    return 0;




Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.