Shortest path

4.5

2 votes
Graphs, Algorithms, Shortest Path Algorithms
Problem

 In a country, there are N cities numbered from 1 to N.

There are M trains in the country which are used by people to move from one city to another. Each train starts from city Li and ends at city Ri and the cost of taking the train is Wi. One can travel between any two cities (from smaller index to larger one) that lie between Li and Ri ( both inclusive) with the cost of Wi.

More formally people can travel from city u to v (Liu<vRi) at the cost of Wi (1iM).

Task

Determine the minimum cost required to travel from city 1 to city N. If the city N is not reachable from city 1 then print -1.

Notes

  • Assume 1-based indexing
  • Trains are given in form of array A of size M*3, denoting the starting city, ending city, and the cost

Example

Assumptions

  • N = 5
  • M = 2
  • A = [ [1, 3, 5], [2, 5, 10] ]

Approach

  • First, take the first train from city 1 and reach city 2 at a cost of 5.
  • Then take the second train from city 2 to city 5 at a cost of 10.
  • Thus the total cost to travel from city 1 to 5 is 5+10=15

Hence, the answer is 15.

Function description

Complete the function solve provided in the editor. This function takes the following parameters and returns the minimum cost required to travel from city 1 to city N:

  • N: Represents the number of cities
  • M: Represents the number of trains
  • A: Represents array in which A[i][0], A[i][1], and A[i][2] represent the city at which ith train starts, the city at which ith train ends, and the cost of taking the train respectively

Input format

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

  • The first line contains denoting the number of test cases. T also specifies the number of times you have to run the solve function on a different set of inputs.
  • For each test case:
    • The first line contains 2 integer and represents the number of cities and the number of trains.
    • The Next line contains 3 space-separated integers that represent the city at which ith train starts, the city at which ith train ends, and the cost of taking the train respectively.

Output format

For each test case in a new line, print the minimum cost required to travel from city 1 to city N. If the city N is not reachable from city 1 then print -1.

Constraints

1T102N1051M1051Li<RiN1Wi109

Code snippets (also called starter code/boilerplate code)

This question has code snippets for C, CPP, Java, and Python.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The first line contains the number of test cases, T = 2.

The first test case

Given

  • N = 4
  • M = 1
  • A = [ [1, 3, 10] ]

Approach

  • Since by taking the first train one can only travel from any city between 1 and 3. There is no way to reach city 4
  • Thus, the answer is -1.

The second test case

Given

  • N = 4
  • M = 3
  • A = [ [1, 4, 10], [1, 2, 5], [2, 4, 3] ].

Approach

  • First, take the second train from city 1 and reach city 2 at a cost of 5.
  • Then, take the third train from city 2 and reach city 4 at a cost of 3.
  • Thus, the total cost is 5+3 = 8
Editor Image

?