Interview Questions/Coding/Courier Route Ledger

Courier Route Ledger

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Courier Route Ledger

A campus courier service receives several city maps before a hiring simulation. Each map has numbered checkpoints and two-way roads with positive travel times. For every map, the courier starts at checkpoint 1 and must report the minimum possible travel time to the final checkpoint n. If the final checkpoint cannot be reached, report -1 for that map. Input Format: The first line contains t, the number of independent maps. Each map begins with n and m. The next m lines contain u, v, and w, meaning checkpoints u and v are connected by a two-way road taking w minutes. Output Format: For each map, print one integer: the minimum travel time from checkpoint 1 to checkpoint n, or -1 when no route exists.

Examples

Example 1
Input:
2
5 6
1 2 2
2 5 5
1 3 4
3 4 1
4 5 1
2 3 1
3 1
1 2 7
Output:
5

Explanation: In the first map, the cheapest route from checkpoint 1 to checkpoint 5 has total time 5. In the second map, checkpoint 3 is disconnected from checkpoint 1, so the answer is -1.

Approach hint

All road times are positive.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp