- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to find out the shortest cost path in a given graph for q queries
Suppose, we are given a graph that contains n vertices and is minimally connected. The edges are given to us an array where the edges are given in a {source, dest, weight} format. Now, we are given q number of queries where each query is of the format {source, destination}. We have to find the shortest cost path from the source to the destination via vertex k. We print the cost of the path for each query.
So, if the input is like n = 6, q = 3, k = 1, edges = {{1, 2, 2}, {1, 3, 4}, {3, 4, 2}, {3, 5, 3}, {5, 6, 2}}, queries = {{1, 4}, {2, 6}, {2, 5}}, then the output will be 6 11 9.
Steps
To solve this, we will follow these steps −
Define one 2D array graph of pairs of size n * n Define an array pathTotal of size n Define a function dfs(), this will take a, b, for each value i at graph[a]: if first value of i is same as b, then: Ignore following part, skip to the next iteration pathTotal[first value of i] := pathTotal[a] + second value of i dfs(first value of i, a) for initialize i := 0, when i < n - 1, update (increase i by 1), do: a := first value of (edges[i]) b := second value of (edges[i]) c := third value of (edges[i]) decrease a and b by 1 insert pair (b, c) at the end of graph[a] insert pair (a, c) at the end of graph[b] (decrease k by 1) dfs(k, k) for initialize i := 0, when i < q, update (increase i by 1), do: x := first value of queries[i] y := second value of queries[i] decrease x and y by 1 print(pathTotal[x] + pathTotal[y])
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; vector<vector<pair<int,int>>> graph; vector<int> pathTotal; int k; void dfs(int a, int b){ for(auto i : graph.at(a)){ if(i.first == b) continue; pathTotal.at(i.first) = pathTotal.at(a) + i.second; dfs(i.first,a); } } void solve(int n, int q, vector<tuple<int, int, int>> edges, vector<pair<int, int>> queries){ int a, b, c, x, y; graph.resize(n); pathTotal.resize(n); for(int i = 0; i < n - 1; i++){ a = get<0> (edges[i]); b = get<1> (edges[i]); c = get<2> (edges[i]); a--, b--; graph.at(a).push_back(make_pair(b, c)); graph.at(b).push_back(make_pair(a, c)); } k--; dfs(k, k); for(int i = 0; i < q; i++){ x = queries[i].first; y = queries[i].second; x--, y--; cout << pathTotal.at(x) + pathTotal.at(y) << endl; } } int main() { int n = 6, q = 3; k = 1; vector<tuple<int, int, int>> edges = {{1, 2, 2}, {1, 3, 4}, {3, 4, 2}, {3, 5, 3}, {5, 6, 2}}; vector<pair<int, int>> queries = {{1, 4}, {2, 6}, {2, 5}}; solve(n, q, edges, queries); return 0; }
Input
6, 3, 1, {{1, 2, 2}, {1, 3, 4}, {3, 4, 2}, {3, 5, 3}, {5, 6, 2}}, {{1, 4}, {2, 6}, {2, 5}}
Output
6 11 9
- Related Articles
- C++ Program to find out the sum of shortest cost paths for all given triplets
- Program to find out the shortest path to reach the goal in Python
- Program to find out if a vertex in an undirected graph has a lesser cost path in Python
- C++ Program for Dijkstra’s shortest path algorithm?
- Shortest Path in a Directed Acyclic Graph
- C Program for Minimum Cost Path
- C++ Program to find out the number of bridge edges in a given graph
- C++ Program to find out the cost to travel all the given coordinates
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Dijkstra’s algorithm to compute the shortest path through a graph
- C / C++ Program for Dijkstra's shortest path algorithm
- Program to count number of queries that are true in a graph with weighted path in C++
- C++ Program to find out the super vertices in a graph
- C++ Program to Find Path Between Two Nodes in a Graph
- Program to Find Out the Cost after Finding k Unique Subsequences From a Given String in C++

Advertisements