Train lines and cities

4

18 votes
Graphs, Algorithms, Shortest Path Algorithms
Problem

A country contains n cities and m train lines. The ith train line works as follows:

A ticket of trains that run on this train line costs ci dollars. A train on this train line starts its journey from fi city and ends at ei city. The total running duration of the train is ti hours. It departures periodically at every hi hours. For example, the first train departure at 0 time, then another train departs at hi, another in 2hi, and so on.

Note: You can wait in cities for a train.

The total cost of a path, that takes H hours and costs C dollars, is defined as A×H+B×C. Initially, you are in city 1 and you must reach city n. Determine the minimum cost to complete this journey.

Input format

  • First line: Four integers n, m, A, and B denoting the number of cities, number of train lines, and two parameters that are used in the cost function
  • Next m lines: Describes train lines such that the ith line contains the description of the ith train line fieitici, and hi

Note:

  • The train lines are not bidirectional.
  • There might be several train lines between the two cities.
  • There might be a train line from a city to the same city.

Output format

Print the path from city 1 to city n that allows you to complete the journey in the minimum cost. Also, print the train lines that you are using in the order. It is guaranteed that there exists a path from city 1 to city n. It is not allowed and inefficient to use the same train line twice.

Constraints

1n,m1060A,B1001fi,ein1ti,ci,hi106IfA=0, then B0

Test generation and scoring

  • In 25% of the test cases, n1000
  • Value of n, m, A, and B are arbitrary
  • Other parts of the input are randomly-generated
Time Limit: 3
Memory Limit: 256
Source Limit:
Explanation

It takes 2 hours to reach the city 2 and its cost is 3 (using the first train line). Then we should wait for 1 hour in the city 2. Using the second train line we can reach the destination (city 3), in 5 hours with cost 2. So the total cost of this path is 1 * (2 + 1 + 5) + 4 * (3 + 2) = 28.

Another way to reach city 3 is to use the third train line which directly connects us to the destination. But it costs 1 * 1 + 4 * 10 = 41 which is worse.

Editor Image

?