Maximum cost path from source node to destination node via at most K intermediate nodes
Last Updated :
23 Jul, 2025
Given a directed weighted graph consisting of N vertices and an array Edges[][], with each row representing two vertices connected by an edge and the weight of that edge, the task is to find the path with the maximum sum of weights from a given source vertex src to a given destination vertex dst, made up of at most K intermediate vertices. If no such path exists, then print -1.
Examples:
Input: N = 3, Edges[][] = {{0, 1, 100}, {1, 2, 100}, {0, 2, 500}}, src = 0, dst = 2, K = 0
Output: 500
Explanation:

Path 0 → 2: The path with maximum weight and at most 0 intermediate nodes is of weight 500.
Approach: The given problem can be solved by using BFS(Breadth-First Search) Traversal. Follow the steps below to solve the problem:
- Initialize the variable, say ans, to store the maximum distance between the source and the destination node having at most K intermediates nodes.
- Initialize an adjacency list of the graph using the edges.
- Initialize an empty queue and push the source vertex into it. Initialize a variable, say lvl, to store the number of nodes present in between src and dst.
- While the queue is not empty and lvl is less than K + 2 perform the following steps:
- Store the size of the queue in a variable, say S.
- Iterate over the range [1, S] and perform the following steps:
- Pop the front element of the queue and store it in a variable, say T.
- If T is the dst vertex, then update the value of ans as the maximum of ans and the current distance T.second.
- Traverse through all the neighbors of the current popped node and check if the distance of its neighbor is greater than the current distance or not. If found to be true, then push it in the queue and update its distance.
- Increase the value of lvl by 1.
- After completing the above steps, print the value of ans as the resultant maximum distance.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the longest distance
// from source to destination with at
// most K intermediate nodes
int findShortestPath(
int n, vector<vector<int> >& edges,
int src, int dst, int K)
{
// Initialize the adjacency list
vector<vector<pair<int, int> > > adjlist(
n, vector<pair<int, int> >());
// Initialize a queue to perform BFS
queue<pair<int, int> > q;
unordered_map<int, int> mp;
// Store the maximum distance of
// every node from source vertex
int ans = INT_MIN;
// Initialize adjacency list
for (int i = 0; i < edges.size(); i++) {
auto edge = edges[i];
adjlist[edge[0]].push_back(
make_pair(edge[1], edge[2]));
}
// Push the first element into queue
q.push({ src, 0 });
int level = 0;
// Iterate until the queue becomes empty
// and the number of nodes between src
// and dst vertex is at most to K
while (!q.empty() && level < K + 2) {
// Current size of the queue
int sz = q.size();
for (int i = 0; i < sz; i++) {
// Extract the front
// element of the queue
auto pr = q.front();
// Pop the front element
// of the queue
q.pop();
// If the dst vertex is reached
if (pr.first == dst)
ans = max(ans, pr.second);
// Traverse the adjacent nodes
for (auto pr2 : adjlist[pr.first]) {
// If the distance is greater
// than the current distance
if (mp.find(pr2.first)
== mp.end()
|| mp[pr2.first]
< pr.second
+ pr2.second) {
// Push it into the queue
q.push({ pr2.first,
pr.second
+ pr2.second });
mp[pr2.first] = pr.second
+ pr2.second;
}
}
}
// Increment the level by 1
level++;
}
// Finally, return the maximum distance
return ans != INT_MIN ? ans : -1;
}
// Driver Code
int main()
{
int n = 3, src = 0, dst = 2, k = 1;
vector<vector<int> > edges
= { { 0, 1, 100 },
{ 1, 2, 100 },
{ 0, 2, 500 } };
cout << findShortestPath(n, edges,
src, dst, k);
return 0;
}
Java
import java.util.*;
class Main {
// Function to find the longest distance
// from source to destination with at
// most K intermediate nodes
public static int findShortestPath(int n, int[][] edges,
int src, int dst,
int K)
{
// Initialize the adjacency list
List<List<int[]> > adjlist = new ArrayList<>();
for (int i = 0; i < n; i++) {
adjlist.add(new ArrayList<int[]>());
}
// Initialize a queue to perform BFS
Queue<int[]> q = new LinkedList<>();
Map<Integer, Integer> mp = new HashMap<>();
// Store the maximum distance of
// every node from source vertex
int ans = Integer.MIN_VALUE;
// Initialize adjacency list
for (int[] edge : edges) {
adjlist.get(edge[0]).add(
new int[] { edge[1], edge[2] });
}
// Push the first element into queue
q.add(new int[] { src, 0 });
int level = 0;
// Iterate until the queue becomes empty
// and the number of nodes between src
// and dst vertex is at most to K
while (!q.isEmpty() && level < K + 2) {
// Current size of the queue
int sz = q.size();
for (int i = 0; i < sz; i++) {
// Extract the front
// element of the queue
int[] pr = q.poll();
// If the dst vertex is reached
if (pr[0] == dst)
ans = Math.max(ans, pr[1]);
// Traverse the adjacent nodes
for (int[] pr2 : adjlist.get(pr[0])) {
// If the distance is greater
// than the current distance
if (!mp.containsKey(pr2[0])
|| mp.get(pr2[0])
> pr[1] + pr2[1]) {
// Push it into the queue
q.add(new int[] { pr2[0],
pr[1] + pr2[1] });
mp.put(pr2[0], pr[1] + pr2[1]);
}
}
}
// Increment the level by 1
level++;
}
// Finally, return the maximum distance
return ans != Integer.MIN_VALUE ? ans : -1;
}
// Driver Code
public static void main(String[] args)
{
int n = 3, src = 0, dst = 2, k = 1;
int[][] edges = { { 0, 1, 100 },
{ 1, 2, 100 },
{ 0, 2, 500 } };
System.out.println(
findShortestPath(n, edges, src, dst, k));
}
}
Python
# Python3 program for the above approach
from collections import deque
# Function to find the longest distance
# from source to destination with at
# most K intermediate nodes
def findShortestPath(n, edges, src, dst, K):
# Initialize the adjacency list
adjlist = [[] for i in range(n)]
# Initialize a queue to perform BFS
q = deque()
mp = {}
# Store the maximum distance of
# every node from source vertex
ans = -10**9
# Initialize adjacency list
for i in range(len(edges)):
edge = edges[i]
adjlist[edge[0]].append([edge[1],
edge[2]])
# Push the first element into queue
q.append([src, 0])
level = 0
# Iterate until the queue becomes empty
# and the number of nodes between src
# and dst vertex is at most to K
while (len(q) > 0 and level < K + 2):
# Current size of the queue
sz = len(q)
for i in range(sz):
# Extract the front
# element of the queue
pr = q.popleft()
# Pop the front element
# of the queue
# q.pop()
# If the dst vertex is reached
if (pr[0] == dst):
ans = max(ans, pr[1])
# Traverse the adjacent nodes
for pr2 in adjlist[pr[0]]:
# If the distance is greater
# than the current distance
if ((pr2[0] not in mp) or
mp[pr2[0]] > pr[1] + pr2[1]):
# Push it into the queue
q.append([pr2[0], pr[1] + pr2[1]])
mp[pr2[0]] = pr[1] + pr2[1]
# Increment the level by 1
level += 1
# Finally, return the maximum distance
return ans if ans != -10**9 else -1
# Driver Code
if __name__ == '__main__':
n, src, dst, k = 3, 0, 2, 1
edges= [ [ 0, 1, 100 ],
[ 1, 2, 100 ],
[ 0, 2, 500 ] ]
print(findShortestPath(n, edges,src, dst, k))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find the longest distance
// from source to destination with at
// most K intermediate nodes
static int FindShortestPath(int n, int[][] edges, int src, int dst, int K)
{
// Initialize the adjacency list
List<int[]>[] adjlist = new List<int[]>[n];
for (int i = 0; i < n; i++) {
adjlist[i] = new List<int[]>();
}
// Initialize a queue to perform BFS
Queue<int[]> q = new Queue<int[]>();
Dictionary<int, int> mp = new Dictionary<int, int>();
// Store the maximum distance of
// every node from source vertex
int ans = -1000000000;
// Initialize adjacency list
for (int i = 0; i < edges.Length; i++) {
int[] edge = edges[i];
adjlist[edge[0]].Add(new int[] {edge[1], edge[2]});
}
// Push the first element into queue
q.Enqueue(new int[] {src, 0});
int level = 0;
// Iterate until the queue becomes empty
// and the number of nodes between src
// and dst vertex is at most to K
while (q.Count > 0 && level < K + 2) {
// Current size of the queue
int sz = q.Count;
for (int i = 0; i < sz; i++) {
// Extract the front
// element of the queue
int[] pr = q.Dequeue();
// If the dst vertex is reached
if (pr[0] == dst) {
ans = Math.Max(ans, pr[1]);
}
// Traverse the adjacent nodes
foreach (int[] pr2 in adjlist[pr[0]]) {
// If the distance is greater
// than the current distance
if (!mp.ContainsKey(pr2[0]) || mp[pr2[0]] > pr[1] + pr2[1]) {
// Push it into the queue
q.Enqueue(new int[] {pr2[0], pr[1] + pr2[1]});
mp[pr2[0]] = pr[1] + pr2[1];
}
}
}
// Increment the level by 1
level++;
}
// Finally, return the maximum distance
return ans != -1000000000 ? ans : -1;
}
// Driver Code
public static void Main()
{
int n = 3, src = 0, dst = 2, k = 1;
int[][] edges = new int[][] {
new int[] {0, 1, 100},
new int[] {1, 2, 100},
new int[] {0, 2, 500}
};
Console.WriteLine(FindShortestPath(n, edges, src, dst, k));
}
}
// This code is contributed by codebraxnzt
JavaScript
// JavaScript implementation of the above C++ code
function findShortestPath(n, edges, src, dst, k) {
// Initialize the adjacency list
var adjlist = new Array(n).fill(null).map(() => []);
// Initialize a queue to perform BFS
var q = [];
var mp = new Map();
// Store the maximum distance of
// every node from source vertex
var ans = Number.MIN_SAFE_INTEGER;
// Initialize adjacency list
for (var i = 0; i < edges.length; i++) {
var edge = edges[i];
adjlist[edge[0]].push([edge[1], edge[2]]);
}
// Push the first element into queue
q.push([src, 0]);
var level = 0;
// Iterate until the queue becomes empty
// and the number of nodes between src
// and dst vertex is at most to K
while (q.length > 0 && level < k + 2) {
// Current size of the queue
var sz = q.length;
for (var i = 0; i < sz; i++) {
// Extract the front
// element of the queue
var pr = q.shift();
// If the dst vertex is reached
if (pr[0] === dst) {
ans = Math.max(ans, pr[1]);
}
// Traverse the adjacent nodes
for (var j = 0; j < adjlist[pr[0]].length; j++) {
var pr2 = adjlist[pr[0]][j];
// If the distance is greater
// than the current distance
if (mp.get(pr2[0]) === undefined || mp.get(pr2[0]) > pr[1] + pr2[1]) {
// Push it into the queue
q.push([pr2[0], pr[1] + pr2[1]]);
mp.set(pr2[0], pr[1] + pr2[1]);
}
}
}
// Increment the level by 1
level++;
}
// Finally, return the maximum distance
return ans !== Number.MIN_SAFE_INTEGER ? ans : -1;
}
// Example usage
var n = 3, src = 0, dst = 2, k = 1;
var edges = [[0, 1, 100], [1, 2, 100], [0, 2, 500]];
console.log(findShortestPath(n, edges, src, dst, k));
Time Complexity: O(N + E)
Auxiliary Space: O(N)
Alternate approach: Modification of Bellman Ford algorithm after modifying the weights
If all the weights of the given graph are made negative of the original weights, the path taken to minimize the sum of weights with at most k nodes in middle will give us the path we need. Hence this question is similar to this problem. Below is the code implementation of the problem
C++
#include <bits/stdc++.h>
using namespace std;
int max_cost(int n, vector<vector<int> >& edges, int src,
int dst, int k)
{
// We use 2 arrays for this algorithm
// temp is the shortest distances array in current pass
vector<int> temp(n, INT_MAX);
temp[src] = 0;
for (int i = 0; i <= k; i++) {
// c is the shortest distances array in previous
// pass For every iteration current pass becomes the
// previous
vector<int> c(temp);
for (auto edge : edges) {
int a = edge[0], b = edge[1], d = edge[2];
// Updating the current array using previous
// array Subtracting d is same as adding -d
temp[b]
= min(temp[b],
c[a] == INT_MAX ? INT_MAX : c[a] - d);
}
}
// Checking is dst is reachable from src or not
if (temp[dst] != INT_MAX) {
// Returning the negative value of the shortest
// distance to return the longest distance
return -temp[dst];
}
return -1;
}
int main()
{
vector<vector<int> > edges = {
{ 0, 1, 100 },
{ 1, 2, 100 },
{ 0, 2, 500 },
};
int src = 0;
int dst = 2;
int k = 1;
int n = 3;
cout << max_cost(n, edges, src, dst, k) << endl;
return 0;
}
// This code was contributed Prajwal Kandekar
Java
import java.util.*;
public class Main {
static int max_cost(int n, List<List<Integer>> edges, int src,
int dst, int k)
{
// We use 2 arrays for this algorithm
// temp is the shortest distances array in current pass
int[] temp = new int[n];
Arrays.fill(temp, Integer.MAX_VALUE);
temp[src] = 0;
for (int i = 0; i <= k; i++)
{
// c is the shortest distances array in previous
// pass For every iteration current pass becomes the
// previous
int[] c = temp.clone();
for (List<Integer> edge : edges) {
int a = edge.get(0), b = edge.get(1), d = edge.get(2);
// Updating the current array using previous
// array Subtracting d is same as adding -d
temp[b] = Math.min(temp[b], c[a] == Integer.MAX_VALUE ? Integer.MAX_VALUE : c[a] - d);
}
}
// Checking if dst is reachable from src or not
if (temp[dst] != Integer.MAX_VALUE)
{
// Returning the negative value of the shortest
// distance to return the longest distance
return -temp[dst];
}
return -1;
}
public static void main(String[] args) {
List<List<Integer>> edges = Arrays.asList(
Arrays.asList(0, 1, 100),
Arrays.asList(1, 2, 100),
Arrays.asList(0, 2, 500)
);
int src = 0;
int dst = 2;
int k = 1;
int n = 3;
System.out.println(max_cost(n, edges, src, dst, k));
}
}
Python
def max_cost(n, edges, src, dst, k):
# We use 2 arrays for this algorithm
# temp is the shortest distances array in current pass
temp=[0 if i==src else float("inf") for i in range(n)]
for _ in range(k+1):
# c is the shortest distances array in previous pass
# For every iteration current pass becomes the previous
c=temp.copy()
for a,b,d in edges:
# Updating the current array using previous array
# Subtracting d is same as adding -d
temp[b]=min(temp[b],c[a]-d)
# Checking is dst is reachable from src or not
if temp[dst]!=float("inf"):
# Returning the negative value of the shortest distance to return the longest distance
return -temp[dst]
return -1
edges = [
[0, 1, 100],
[1, 2, 100],
[0, 2, 500],
]
src = 0
dst = 2
k = 1
n = 3
print(max_cost(n,edges,src,dst,k))
# This code was contributed by Akshayan Muralikrishnan
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class MainClass
{
static int MaxCost(int n, List<List<int>> edges, int src, int dst, int k)
{
// We use 2 arrays for this algorithm
// temp is the shortest distances array in current pass
int[] temp = new int[n];
Array.Fill(temp, int.MaxValue);
temp[src] = 0;
for (int i = 0; i <= k; i++)
{
// c is the shortest distances array in previous
// pass For every iteration current pass becomes the
// previous
int[] c = (int[])temp.Clone();
foreach (var edge in edges)
{
int a = edge[0], b = edge[1], d = edge[2];
// Updating the current array using previous
// array Subtracting d is same as adding -d
temp[b] = Math.Min(temp[b], c[a] == int.MaxValue ? int.MaxValue : c[a] - d);
}
}
// Checking if dst is reachable from src or not
if (temp[dst] != int.MaxValue)
{
// Returning the negative value of the shortest
// distance to return the longest distance
return -temp[dst];
}
return -1;
}
public static void Main()
{
List<List<int>> edges = new List<List<int>>()
{
new List<int>(){0, 1, 100},
new List<int>(){1, 2, 100},
new List<int>(){0, 2, 500}
};
int src = 0;
int dst = 2;
int k = 1;
int n = 3;
Console.WriteLine(MaxCost(n, edges, src, dst, k));
}
}
JavaScript
function max_cost(n, edges, src, dst, k) {
// We use 2 arrays for this algorithm
// temp is the shortest distances array in current pass
let temp = new Array(n).fill(Number.MAX_SAFE_INTEGER);
temp[src] = 0;
for (let i = 0; i <= k; i++) {
// c is the shortest distances array in previous
// pass For every iteration current pass becomes the
// previous
let c = [...temp];
for (let j = 0; j < edges.length; j++) {
let [a, b, d] = edges[j];
// Updating the current array using previous
// array Subtracting d is same as adding -d
temp[b] = Math.min(temp[b], (c[a] === Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : c[a] - d);
}
}
// Checking is dst is reachable from src or not
if (temp[dst] !== Number.MAX_SAFE_INTEGER) {
// Returning the negative value of the shortest
// distance to return the longest distance
return -temp[dst];
}
return -1;
}
let edges = [ [0, 1, 100],
[1, 2, 100],
[0, 2, 500]
];
let src = 0;
let dst = 2;
let k = 1;
let n = 3;
console.log(max_cost(n, edges, src, dst, k));
Time Complexity: O(E*k) where E is the number of edges
Auxiliary Space: O(n)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem