Given a directed graph with **N** nodes and **E** edges where the weight of each of the edge is **> 1**, also given a source **S** and a destination **D**. The task is to find the path with the minimum product of edges from **S** to **D**. If there is no path from **S** to **D** then print **-1**.

**Examples:**

Input:N = 3, E = 3, Edges = {{{1, 2}, 5}, {{1, 3}, 9}, {{2, 3}, 1}}, S = 1, and D = 3

Output:5

The path with smallest product of edges will be 1->2->3

with product as 5*1 = 5.

Input:N = 3, E = 3, Edges = {{{3, 2}, 5}, {{3, 3}, 9}, {{3, 3}, 1}}, S = 1, and D = 3

Output:-1

**Approach:** The idea is to use Dijkstra’s shortest path algorithm with a slight variation.

The following steps can be followed to compute the result:

- If the source is equal to the destination then return
**0**. - Initialise a priority-queue
**pq**with**S**and its weight as**1**and a visited array**v[]**. - While
**pq**is not empty:- Pop the top-most element from
**pq**. Let’s call it as**curr**and its product of distance as**dist**. - If
**curr**is already visited then continue. - If
**curr**is equal to**D**then return**dist**. - Iterate all the nodes adjacent to
**curr**and push into**pq**(next and dist + gr[nxt].weight)

- Pop the top-most element from
- return
**-1**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the smallest ` `// product of edges ` `double` `dijkstra(` `int` `s, ` `int` `d, ` ` ` `vector<vector<pair<` `int` `, ` `double` `> > > gr) ` `{ ` ` ` `// If the source is equal ` ` ` `// to the destination ` ` ` `if` `(s == d) ` ` ` `return` `0; ` ` ` ` ` `// Initialise the priority queue ` ` ` `set<pair<` `int` `, ` `int` `> > pq; ` ` ` `pq.insert({ 1, s }); ` ` ` ` ` `// Visited array ` ` ` `bool` `v[gr.size()] = { 0 }; ` ` ` ` ` `// While the priority-queue ` ` ` `// is not empty ` ` ` `while` `(pq.size()) { ` ` ` ` ` `// Current node ` ` ` `int` `curr = pq.begin()->second; ` ` ` ` ` `// Current product of distance ` ` ` `int` `dist = pq.begin()->first; ` ` ` ` ` `// Popping the top-most element ` ` ` `pq.erase(pq.begin()); ` ` ` ` ` `// If already visited continue ` ` ` `if` `(v[curr]) ` ` ` `continue` `; ` ` ` ` ` `// Marking the node as visited ` ` ` `v[curr] = 1; ` ` ` ` ` `// If it is a destination node ` ` ` `if` `(curr == d) ` ` ` `return` `dist; ` ` ` ` ` `// Traversing the current node ` ` ` `for` `(` `auto` `it : gr[curr]) ` ` ` `pq.insert({ dist * it.second, it.first }); ` ` ` `} ` ` ` ` ` `// If no path exists ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `// Graph as adjacency matrix ` ` ` `vector<vector<pair<` `int` `, ` `double` `> > > gr(n + 1); ` ` ` ` ` `// Input edges ` ` ` `gr[1].push_back({ 3, 9 }); ` ` ` `gr[2].push_back({ 3, 1 }); ` ` ` `gr[1].push_back({ 2, 5 }); ` ` ` ` ` `// Source and destination ` ` ` `int` `s = 1, d = 3; ` ` ` ` ` `// Dijkstra ` ` ` `cout << dijkstra(s, d, gr); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the smallest ` `# product of edges ` `def` `dijkstra(s, d, gr) : ` ` ` ` ` `# If the source is equal ` ` ` `# to the destination ` ` ` `if` `(s ` `=` `=` `d) : ` ` ` `return` `0` `; ` ` ` ` ` `# Initialise the priority queue ` ` ` `pq ` `=` `[]; ` ` ` `pq.append(( ` `1` `, s )); ` ` ` ` ` `# Visited array ` ` ` `v ` `=` `[` `0` `]` `*` `(` `len` `(gr) ` `+` `1` `); ` ` ` ` ` `# While the priority-queue ` ` ` `# is not empty ` ` ` `while` `(` `len` `(pq) !` `=` `0` `) : ` ` ` ` ` `# Current node ` ` ` `curr ` `=` `pq[` `0` `][` `1` `]; ` ` ` ` ` `# Current product of distance ` ` ` `dist ` `=` `pq[` `0` `][` `0` `]; ` ` ` ` ` `# Popping the top-most element ` ` ` `pq.pop(); ` ` ` ` ` `# If already visited continue ` ` ` `if` `(v[curr]) : ` ` ` `continue` `; ` ` ` ` ` `# Marking the node as visited ` ` ` `v[curr] ` `=` `1` `; ` ` ` ` ` `# If it is a destination node ` ` ` `if` `(curr ` `=` `=` `d) : ` ` ` `return` `dist; ` ` ` ` ` `# Traversing the current node ` ` ` `for` `it ` `in` `gr[curr] : ` ` ` `if` `it ` `not` `in` `pq : ` ` ` `pq.insert(` `0` `,( dist ` `*` `it[` `1` `], it[` `0` `] )); ` ` ` ` ` `# If no path exists ` ` ` `return` `-` `1` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `3` `; ` ` ` ` ` `# Graph as adjacency matrix ` ` ` `gr ` `=` `{}; ` ` ` ` ` `# Input edges ` ` ` `gr[` `1` `] ` `=` `[( ` `3` `, ` `9` `) ]; ` ` ` `gr[` `2` `] ` `=` `[ (` `3` `, ` `1` `) ]; ` ` ` `gr[` `1` `].append(( ` `2` `, ` `5` `)); ` ` ` `gr[` `3` `] ` `=` `[]; ` ` ` ` ` `#print(gr); ` ` ` `# Source and destination ` ` ` `s ` `=` `1` `; d ` `=` `3` `; ` ` ` ` ` `# Dijkstra ` ` ` `print` `(dijkstra(s, d, gr)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time complexity:** O((E + V) logV)

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.

## Recommended Posts:

- Path with smallest product of edges with weight > 0
- Remove all outgoing edges except edge with minimum weight
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Check if alternate path exists from U to V with smaller individual weight in a given Graph
- Shortest path with exactly k edges in a directed and weighted graph
- Minimum edges to reverse to make path from a source to a destination
- Dijkstra's shortest path with minimum edges
- Minimum cost to reverse edges such that there is path between every pair of nodes
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Path with minimum XOR sum of edges in a directed graph
- Shortest Path with even number of Edges from Source to Destination
- Product of minimum edge weight between all pairs of a Tree
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- k'th heaviest adjacent node in a graph where each vertex has weight
- Find minimum weight cycle in an undirected graph
- Check if there is a cycle with odd weight sum in an undirected graph
- Number of Paths of Weight W in a K-ary tree

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.