2642. Design Graph With Shortest Path Calculator
There is a directed weighted graph that consists of
nnodes numbered from0ton - 1. The edges of the graph are initially represented by the given arrayedgeswhereedges[i] = [fromi, toi, edgeCosti]meaning that there is an edge fromfromitotoiwith the costedgeCosti.Implement the
Graphclass:
Graph(int n, int[][] edges)initializes the object withnnodes 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 fromnode1tonode2. 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 { |