Find the cheapest route between two cities with a limited number of half-price tolls

Quick Overview

Find the cheapest route from city 0 to city n - 1 over undirected toll highways when a limited number of tickets can each halve one toll with integer division. Tests shortest-path reasoning on weighted graphs where a limited resource changes the cost of each edge.

Find the cheapest route between two cities with a limited number of half-price tolls

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A network of highways connects `n` cities numbered `0` to `n - 1`. You are given a list `highways` where `highways[i] = [city1, city2, toll]` means a highway joins `city1` and `city2` and can be driven in either direction for a cost of `toll`. You also hold `discounts` discount tickets. Using a ticket on a highway lowers the cost of crossing it to `toll // 2` (integer division). Each ticket can be used only once, and at most one ticket can be applied to a single highway crossing. Return the minimum total cost to travel from city `0` to city `n - 1`, or `-1` if city `n - 1` cannot be reached from city `0`. ### Function Signature ```python def minimum_cost(n: int, highways: list[list[int]], discounts: int) -> int: ``` ### Rules - Highways are undirected: `[a, b, toll]` allows travel from `a` to `b` and from `b` to `a` at the same toll. - A discounted crossing costs `toll // 2`, rounded down. A toll of `11` becomes `5`. - Using a discount is optional. You may finish with tickets left over. - Return `-1` when no route connects city `0` to city `n - 1`. ### Constraints - `2 <= n <= 1000` - `1 <= len(highways) <= 1000` - `len(highways[i]) == 3` - `0 <= city1, city2 <= n - 1` - `city1 != city2` - `0 <= toll <= 10^5` - `0 <= discounts <= 500` - There are no duplicate highways. ### Examples ```text Input: n = 5, highways = [[0,1,4],[2,1,3],[1,4,11],[3,2,3],[3,4,2]], discounts = 1 Output: 9 ``` The route `0 -> 1 -> 4` costs `4 + 11 // 2 = 4 + 5 = 9` with the single ticket spent on the toll of `11`. The other route, `0 -> 1 -> 2 -> 3 -> 4`, costs `12` at full price and `10` at best with one ticket. ```text Input: n = 4, highways = [[1,3,17],[1,2,7],[3,2,5],[0,1,6],[3,0,20]], discounts = 20 Output: 8 ``` There are enough tickets to halve every highway on any route. The route `0 -> 1 -> 2 -> 3` then costs `3 + 3 + 2 = 8`, which beats the direct highway `0 -> 3` at `10` and the route `0 -> 1 -> 3` at `3 + 8 = 11`. ```text Input: n = 4, highways = [[0,1,3],[2,3,2]], discounts = 1 Output: -1 ``` City `3` is joined only to city `2`, and neither of them can be reached from city `0`, so no route exists.

Overview: Find the cheapest route from city 0 to city n - 1 over undirected toll highways when a limited number of tickets can each halve one toll with integer division. Tests shortest-path reasoning on weighted graphs where a limited resource changes the cost of each edge.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A network of highways connects n cities numbered 0 to n - 1. You are given a list highways where highways[i] = [city1, city2, toll] means a highway joins city1 and city2 and can be driven in either direction for a cost of toll.

You also hold discounts discount tickets. Using a ticket on a highway lowers the cost of crossing it to toll // 2 (integer division). Each ticket can be used only once, and at most one ticket can be applied to a single highway crossing.

Return the minimum total cost to travel from city 0 to city n - 1, or -1 if city n - 1 cannot be reached from city 0.

Function Signature

def minimum_cost(n: int, highways: list[list[int]], discounts: int) -> int:

Rules

  • Highways are undirected: [a, b, toll] allows travel from a to b and from b to a at the same toll.
  • A discounted crossing costs toll // 2 , rounded down. A toll of 11 becomes 5 .
  • Using a discount is optional. You may finish with tickets left over.
  • Return -1 when no route connects city 0 to city n - 1 .

Constraints

  • 2 <= n <= 1000
  • 1 <= len(highways) <= 1000
  • len(highways[i]) == 3
  • 0 <= city1, city2 <= n - 1
  • city1 != city2
  • 0 <= toll <= 10^5
  • 0 <= discounts <= 500
  • There are no duplicate highways.

Examples

Input: n = 5, highways = [[0,1,4],[2,1,3],[1,4,11],[3,2,3],[3,4,2]], discounts = 1
Output: 9

The route 0 -> 1 -> 4 costs 4 + 11 // 2 = 4 + 5 = 9 with the single ticket spent on the toll of 11. The other route, 0 -> 1 -> 2 -> 3 -> 4, costs 12 at full price and 10 at best with one ticket.

Input: n = 4, highways = [[1,3,17],[1,2,7],[3,2,5],[0,1,6],[3,0,20]], discounts = 20
Output: 8

There are enough tickets to halve every highway on any route. The route 0 -> 1 -> 2 -> 3 then costs 3 + 3 + 2 = 8, which beats the direct highway 0 -> 3 at 10 and the route 0 -> 1 -> 3 at 3 + 8 = 11.

Input: n = 4, highways = [[0,1,3],[2,3,2]], discounts = 1
Output: -1

City 3 is joined only to city 2, and neither of them can be reached from city 0, so no route exists.