Adeptalgorithms & data structures
The Writs of Free Passage
The toll-roads out of bitwick are held by waystation keepers, and the courier's guild issues writs of free passage: one writ buys one crossing with no toll paid. A courier is leaving the town gate with a satchel of writs and needs the cheapest way out to the far waystation.
Write minToll(n, roads, passes).
nis an integer, the number of waystations, numbered0ton - 1(n >= 1). The courier starts at0and must reachn - 1.roadsis a list of triples[a, b, toll]. Each triple is one two-way road between the distinct waystationsaandb, costingtollcoins to cross in either direction.tollis an integer>= 0. There may be more than one road between the same pair of waystations.passesis an integer>= 0, the number of writs carried. Each writ may be spent on a single crossing of a single road, making that crossing cost0coins. A writ is used up when spent, and writs need not all be spent.
Waystations and roads may be visited and crossed any number of times.
Return the smallest total number of coins paid on a journey from waystation 0 to waystation n - 1, as an integer, or -1 if no such journey exists.
examples
- case 1
n of 3, roads of [[0, 2, 100], [0, 1, 7], [1, 2, 7]], passes of 10 — the single road 0-2 costs 100 coins, but one writ waives that crossing entirely; the two-road route would still cost 7 coins after a writ - case 2
n of 4, roads of [[0, 1, 10], [1, 2, 10], [2, 3, 10]], passes of 120 — three crossings are unavoidable and one of them is free - case 3
n of 4, roads of [[0, 1, 5], [2, 3, 4]], passes of 2-1 — no journey reaches waystation 3 - case 4
n of 1, roads of [], passes of 00 — the courier is already at the destination
- reward
- 310 XP
- signature
minToll
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
loading the editor…
- already at the destination
- no route exists
- a writ on the costliest road
- no writs at all
- two writs change the route
- more writs than crossings
- a free road on the way
- the long ladder of paired roads