Interview Questions/Coding/Road Upgrade Distance Ledger

Road Upgrade Distance Ledger

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

Hard

Road Upgrade Distance Ledger

A transport analytics team stores the current shortest travel time between every pair of cities. New two-way roads are opened one by one. After each opening, the team needs the sum of the shortest travel times over all unordered city pairs. For every batch, process the road openings in order and report the sum after each one. Input Format: The first line contains t. Each case begins with n, followed by an n by n shortest-distance matrix. Then k follows, and each of the next k lines contains a, b, and w for a planned two-way road. Output Format: For every planned road, print the updated sum of shortest distances after that road is considered.

Examples

Example 1
Input:
1
4
0 5 9 12
5 0 4 7
9 4 0 3
12 7 3 0
3
1 3 2
2 4 1
1 4 20
Output:
26

Explanation: Each output line is the sum after one road opening. The matrix is updated before the next planned road is processed.

Approach hint

The old matrix is already a shortest-distance matrix.

Common mistake

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

solution.cpp