Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Reply Code Challenge - Teen Edition 2024

21 March 2024 bridges • EN

Problem 5 - Points and Islands (bridges)


Chick is a traveller who wants to travel through an archipelago of islands to reach the island where, he
knows, a precious treasure is hidden.

The archipelago consists of a number N of islands (each of them with an ID from 0 to N-1). Chick starts
from island 0 and has to reach the island N-1.
In the archipelago there are a lot of bridges, each of them connecting a couple of islands. Some bridges
are free, while others require a tax to be paied to be crossed.
Each bridge has an associated travel time LT and, if not free, the cost LC for the tax to be paid.
The journey has a limited budget B. Starting from island with id 0 find the path to reach island with id
N − 1 in the shortest time possible (before someone could take the treasure) without exceding Chick’s
budget B.
To solve this task you have to calculate also other two budget B1 and B2.
• B1 is the minimum budget to get to the destination, regardless the necessary time.
• B2 is the budget for the fastest path possible to reach the destination (find the minimum value for
B2)
B1 is the minimum budget to reach the destination, so the minimum tax that you have to pay to reach
the treasure (do not care on time required). B2 is the budget for the shortest path, so the minimum
budget that you should have (bigger than B) to reach the treasure in the shortest time possible.

Input data
The first line of the input file contains an integer T, the number of test cases to solve.
For each test case, the first line of the input file contains the integers:
• N, the number of islands
• Lf ree , the number of free bridges
• Lpay , the number of bridges that required a tax
• B, the budget available

bridges Page 1 of ??
The next Lf ree lines will contain three different space-separated integer numbers, representing information
of free bridges:
• I1 : the identifier of the first island
• I2 : the identifier of the second island
• LT : the time to cross the bridge
The next Lpay lines will contain three different space-separated integer numbers, representing information
of payment bridges:
• I1 : the identifier of the first island
• I2 : the identifier of the second island
• LT : the time to cross the bridge
• LC : the cost to cross the bridge
Note: Every bridge can be crossed in any direction: from I1 to I2 or from I2 to I1 .
Note: The time to move inside the island from the end of the incoming bridge to the next bridge is
irrelevant.
Note: Is possible that two or more different bridges connect the same two islands: choose wisely!

Output data
The output file must contains T lines.
For each test case in the input file, the output file must contain a line with the characters:
Case #t: T ime Cost B1 B2
Where t is the test case number, from 1 to T, T ime and Cost are the minimum time and the corresponding
cost to travel from island 0 to island N − 1. In case there are multiple paths with the lowest time possible
consider the path that requires the lowest budget (better save some money!!). B1 and B2 as described
before.
Note: the lines of the output file must be ordered from Case #1: to Case #T:.

Constraints
• Input 1 : T = 1, L ≤ 500, N ≤ 100
• Input 2 : T = 5, L ≤ 1 500, N ≤ 300
• Input 3 : T = 10, L ≤ 2 500, N ≤ 600
• Input 4 : T = 15, L ≤ 3 000, N ≤ 700
• Input 5 : T = 20, L ≤ 4 000, N ≤ 1 000

Examples

bridges Page 2 of ??
input output

1 Case #1: 129 40 0 65


12 14 8 60
10 9 22
0 1 4
3 8 65
4 5 39
9 10 19
1 2 7
10 7 24
5 4 31
2 6 32
3 5 2
2 4 16
8 9 25
7 11 45
5 9 24
9 8 12 12
4 3 39 15
4 1 30 28
8 7 11 15
0 4 6 13
10 11 14 79
6 11 23 65
8 6 14 50

Explanation
The best path in the example (with given budget B = 60) is 0 4 5 9 8 7 11. I.E. From 9 to 8 the payment
bridge is affordable and more convenient than the free bridge.
This solution requires a time 129 to cross all the bridges in this path and 40 has to be paied.
The lowest budget (B1) to reach the treasure is 0 since is possible to follow the path 0 1 2 4 5 9 10 7 11
made only by free bridges.
The best path would be 0 1 2 6 11 if only Chick could afford the budget (B2) of 65

bridges Page 3 of ??

You might also like