Given an undirected weighted graph. The task is to find the minimum cost of the path from source node to the destination node via an intermediate node.

**Note:** If an edge is traveled twice, only once weight is calculated as cost.

Examples:

Input:source = 0, destination = 2, intermediate = 3;

Output:6

The minimum cost path 0->1->3->1->2

The edge (1-3) occurs twice in the path, but its weight is

added only once to the answer.

Input:source = 0, destination = 2, intermediate = 1;

Output:3

The minimum cost path is 0->1>2

**Approach:** Let suppose take a path P1 from Source to intermediate, and a path P2 from intermediate to destination. There can be some common edges among these 2 paths. Hence, the optimal path will always have the following form: for any node U, the walk consists of edges on the shortest path from Source to U, from intermediate to U, and from destination to U. Hence, if dist(a, b) is the cost of shortest path between node a and b, the required minimum cost path will be **min{ dist(Source, U) + dist(intermediate, U) + dist(destination, U) }**** **for all U. The Minimum distance of all nodes from Source, intermediate, and destination can be found by doing Dijkstra’s Shortest Path algorithm from these 3 nodes.

Below is the implementation of the above approach.

`// CPP program to find minimum distance between ` `// source and destination node and visiting ` `// of intermediate node is compulsory ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define MAXN 100005 ` ` ` `// to strore maped values of graph ` `vector<pair<` `int` `, ` `int` `> > v[MAXN]; ` ` ` `// to store distance of ` `// all nodes from the source node ` `int` `dist[MAXN]; ` ` ` `// Dijkstra's algorithm to find ` `// shortest path from source to node ` `void` `dijkstra(` `int` `source, ` `int` `n) ` `{ ` ` ` `// set all the vertices ` ` ` `// distances as infinity ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `dist[i] = INT_MAX; ` ` ` ` ` `// set all vertex as unvisited ` ` ` `bool` `vis[n]; ` ` ` `memset` `(vis, ` `false` `, ` `sizeof` `vis); ` ` ` ` ` `// make distance from source ` ` ` `// vertex to source vertex is zero ` ` ` `dist = 0; ` ` ` ` ` `// // multiset do the job ` ` ` `// as a min-priority queue ` ` ` `multiset<pair<` `int` `, ` `int` `> > s; ` ` ` ` ` `// insert the source node with distance = 0 ` ` ` `s.insert({ 0, source }); ` ` ` ` ` `while` `(!s.empty()) { ` ` ` `pair<` `int` `, ` `int` `> p = *s.begin(); ` ` ` `// pop the vertex with the minimum distance ` ` ` `s.erase(s.begin()); ` ` ` ` ` `int` `x = p.second; ` ` ` `int` `wei = p.first; ` ` ` ` ` `// check if the popped vertex ` ` ` `// is visited before ` ` ` `if` `(vis[x]) ` ` ` `continue` `; ` ` ` ` ` `vis[x] = ` `true` `; ` ` ` ` ` `for` `(` `int` `i = 0; i < v[x].size(); i++) { ` ` ` `int` `e = v[x][i].first; ` ` ` `int` `w = v[x][i].second; ` ` ` ` ` `// check if the next vertex ` ` ` `// distance could be minimized ` ` ` `if` `(dist[x] + w < dist[e]) { ` ` ` ` ` `dist[e] = dist[x] + w; ` ` ` ` ` `// insert the next vertex ` ` ` `// with the updated distance ` ` ` `s.insert({ dist[e], e }); ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// function to add edges in graph ` `void` `add_edge(` `int` `s, ` `int` `t, ` `int` `weight) ` `{ ` ` ` `v[s].push_back({ t, weight }); ` ` ` `v[t].push_back({ s, weight }); ` `} ` ` ` `// function to find the minimum shortest path ` `int` `solve(` `int` `source, ` `int` `destination, ` ` ` `int` `intermediate, ` `int` `n) ` `{ ` ` ` `int` `ans = INT_MAX; ` ` ` ` ` `dijkstra(source, n); ` ` ` ` ` `// store distance from source to ` ` ` `// all other vertices ` ` ` `int` `dsource[n]; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `dsource[i] = dist[i]; ` ` ` ` ` `dijkstra(destination, n); ` ` ` `// store distance from destination ` ` ` `// to all other vertices ` ` ` `int` `ddestination[n]; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `ddestination[i] = dist[i]; ` ` ` ` ` `dijkstra(intermediate, n); ` ` ` `// store distance from intermediate ` ` ` `// to all other vertices ` ` ` `int` `dintermediate[n]; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `dintermediate[i] = dist[i]; ` ` ` ` ` `// find required answer ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `ans = min(ans, dsource[i] + ddestination[i] ` ` ` `+ dintermediate[i]); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `n = 4; ` ` ` `int` `source = 0, destination = 2, intermediate = 3; ` ` ` ` ` `// add edges in graph ` ` ` `add_edge(0, 1, 1); ` ` ` `add_edge(1, 2, 2); ` ` ` `add_edge(1, 3, 3); ` ` ` ` ` `// function call for minimum shortest path ` ` ` `cout << solve(source, destination, intermediate, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time complexity:** O((N + M) * logN), where N is number of nodes, M is number of edges.

**Auxiliary Space:** O(N+M)

## Recommended Posts:

- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Minimum edges to reverse to make path from a source to a destination
- Source to destination in 2-D path with fixed sized jumps
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- Shortest Path with even number of Edges from Source to Destination
- Check whether the cost of going from any node to any other node via all possible paths is same
- Print all paths from a given source to a destination
- Print all paths from a given source to a destination using BFS
- Sum of shortest distance on source to destination and back having at least a common vertex
- Count total ways to reach destination from source in an undirected Graph
- Print all shortest paths between given source and destination in an undirected graph
- Number of Walks from source to destination
- Count all possible walks from a source to a destination with exactly k edges
- Find the minimum cost to reach destination using a train
- Minimum Cost using Dijkstra by reducing cost of an Edge
- Minimum cost to empty Array where cost of removing an element is 2^(removed_count) * arr[i]
- Find if there is a path of more than k length from a source
- Multi Source Shortest Path in Unweighted Graph
- D'Esopo-Pape Algorithm : Single Source Shortest Path

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.