- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# C++ program to find out the minimum amount of time needed to reach from source to destination station by train

Suppose n stations are connected by m tracks. The stations are named from 1 to n. The tracks are bidirectional, and we have to reach station dest from station src. Thes source and destination stations of the i-th railroad is given in the array 'roads' where roads[i] is of the format {station1, station2}. From the j-th station, a train leaves for all stations that are connected with the station at the multiples of time kj and each train takes tj amount of time to reach the destination. The values are given in an array 'departure' where each element is of the format {tj, kj}. Now, we have to figure out the minimum possible time it takes to reach from src to dest. We can change multiple trains and the time taken to change the trains is negligible.

So, if the input is like n = 4, m = 3, src = 1, dst = 4, roads = {{1, 2}, {2, 4}, {3, 4}}, departure = {{2, 1}, {3, 5}, {7, 6}}, then the output will be 8.

From station 1, we take the train to station 2 at time 0. Time taken to reach station 2 is 2. From station 2, we take the train to station 4 at time 5. Time taken to reach station 2 is 3. So total time taken is (5 + 3) = 8.

## Steps

To solve this, we will follow these steps −

src := src - 1 dst := dst - 1 Define a new array graph[n] that contains tuples for initialize i := 0, when i < m, update (increase i by 1), do: a := first value of roads[i] - 1 b := second value of roads[i] - 1 t := first value of departure[i] k := second value of departure[i] add tuple (b, t, k) at the end of graph[a] add tuple (a, t, k) at the end of graph[b] Define an array dp of size n initialized with value -9999 Define a priority queue priq that contains pairs dp[src] := 0 insert pair(-dp[src], src) at the end of priq while not priq is empty, do: tuple containing (w, a) := largest value of priq delete top element from priq if a is same as dst, then: return -w if w < dp[a], then: Ignore following part, skip to the next iteration for each v in graph[a], do: create a tuple containing (b, t, k) weight := (w - k + 1) / k * k - t if weight > dp[b], then: dp[b] := weight insert pair(weight, b) at the end of priq return -1

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int solve(int n, int m, int src, int dst, vector<pair<int, int>> roads, vector<pair<int, int>> departure){ src -= 1; dst -= 1; vector<tuple<int, int, int>> graph[n]; int a, b; int t, k; for(int i = 0; i < m; i++){ a = roads[i].first - 1; b = roads[i].second - 1; t = departure[i].first; k = departure[i].second; graph[a].emplace_back(b, t, k); graph[b].emplace_back(a, t, k); } vector<int> dp(n, -9999); priority_queue<pair<int, int>> priq; dp[src] = 0; priq.push(make_pair(-dp[src], src)); int w; while(not priq.empty()){ tie(w, a) = priq.top(); priq.pop(); if(a == dst){ return -w; } if(w < dp[a]) continue; for(auto &v: graph[a]){ tie(b, t, k) = v; int weight = (w - k + 1) / k * k - t; if(weight > dp[b]){ dp[b] = weight; priq.push(make_pair(weight, b)); } } } return -1; } int main() { int n = 4, m = 3, src = 1, dst = 4; vector<pair<int, int>> roads = {{1, 2}, {2, 4}, {3, 4}}, departure = {{2, 1}, {3, 5}, {7, 6}}; cout<< solve(n, m, src, dst, roads, departure); return 0; }

## Input

4, 3, 1, 4, {{1, 2}, {2, 4}, {3, 4}}, {{2, 1}, {3, 5}, {7, 6}}

## Output

8

- Related Articles
- Find the minimum cost to reach destination using a train
- How to find the minimum number of steps needed by knight to reach the destination using C#?
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- Minimum Initial Points to Reach Destination
- Program to find minimum number of heights to be increased to reach destination in Python
- C++ program to find minimum number of punches are needed to make way to reach target
- Program to find minimum amount needed to be paid all good performers in Python
- Program to find out the minimum rotations needed to maximize the profit from a Ferris wheel in Python
- 8085 program to move blocks of bits from source location to a destination location
- How OSPF routes the packets from source to destination?
- C++ program to find minimum number of steps needed to move from start to end
- C++ code to find minimum time needed to do all tasks
- C++ Program to find out the number of jumps needed for a robot to reach a particular cell in a grid
- C++ program to count number of operations needed to reach n by paying coins
- Program to find out the minimum number of moves for a chess piece to reach every position in Python