Minimum Time Problem Statement

In a city with ‘N’ junctions and ‘M’ bi-directional roads, each junction is connected to other junctions with specified travel times. No road connects a junction to itself, and only one road connects any pair of junctions. A vehicle can only move along a road when the traffic light at the current junction is green. If a vehicle arrives when the light is not green, it must wait until the next green flash.

The goal is to determine the minimum time required to travel from a given source junction ‘src’ to a destination junction ‘des’ using the given map, which includes the travel times for all roads and the green light period ‘P[i]’ for each junction ‘i’.

Input:
The first line contains a single integer ‘T’ representing the number of test cases.
Each test case includes the following:
- Two space-separated integers, ‘N’ and ‘M’, representing the number of junctions and roads.
- ‘N’ space-separated integers denoting the green light period array ‘P’ where ‘P[i]’ is the time needed at junction ‘i’.
- ‘M’ lines, each containing three space-separated integers denoting a road between junction ‘Edges[i][0]’ and junction ‘Edges[i][1]’ with travel time ‘Edges[i][2]’.
- Two space-separated integers, ‘src’ and ‘des’, indicating the source and destination junctions.
Output:
For each test case, output a single integer representing the minimum time needed from ‘src’ to ‘des’. Output ‘-1’ if ‘des’ is unreachable.
Example:
Consider ‘N’ = 3 with edges:
[[0, 1, 2],
[1, 2, 4]]
‘P’ = [2, 3, 4], from 0 to 2.
The path 0 -> 1 -> 2 takes time 2 + 1 (wait) + 4 = 7.
Hence, the answer is 7.

Constraints:

  • 1 ≤ T ≤ 5
  • 2 ≤ N ≤ 105
  • 1 ≤ M ≤ 105
  • 1 ≤ P[i] ≤ 107
  • 0 ≤ Edges[i][0], Edges[i][1] < N
  • 1 ≤ Edges[i][2] ≤ 107
  • 0 ≤ src, des < N
  • Time limit: 1 sec
AnswerBot
4mo

The problem involves finding the minimum time to travel from a source junction to a destination junction in a city with specified travel times and green light periods.

  • Input consists of the number of t...read more

Help your peers!
Select
Add answer anonymously...

RUBRIK INDIA Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Minimum Removals Problem Statement Given an integer array ARR of size N and an i...read more
A Software Developer Intern was asked Q. Minimum Number of Stabs to Kill the King You are given the initial health of a k...read more
A Software Developer Intern was asked Q. Scramble String Problem Statement You are given an integer 'N' and two strings S...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Minimum Removals Problem Statement Given an integer array ARR of size N and an i...read more
A Software Developer Intern was asked Q2. Minimum Number of Stabs to Kill the King You are given the initial health of a k...read more
A Software Developer Intern was asked Q3. Scramble String Problem Statement You are given an integer 'N' and two strings S...read more
RUBRIK INDIA Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits