2642. Design Graph With Shortest Path Calculator
There is a directed weighted graph that consists of
n
nodes numbered from0
ton - 1
. The edges of the graph are initially represented by the given arrayedges
whereedges[i] = [fromi, toi, edgeCosti]
meaning that there is an edge fromfromi
totoi
with the costedgeCosti
.Implement the
Graph
class:
Graph(int n, int[][] edges)
initializes the object withn
nodes and the given edges.addEdge(int[] edge)
adds an edge to the list of edges whereedge = [from, to, edgeCost]
. It is guaranteed that there is no edge between the two nodes before adding this one.int shortestPath(int node1, int node2)
returns the minimum cost of a path fromnode1
tonode2
. If no path exists, return-1
. The cost of a path is the sum of the costs of the edges in the path.
1 | class Graph { |