Parcel Routing
InterviewTime limit2sMemory limit128 MB
Given a weighted graph, compute for every pair of hubs the next hub to visit on a shortest path using all-pairs shortest paths.
- Level
Medium4 of 10
- Topics
- Shortest path, Graph, Dynamic programming, Matrix
- Solved
- No attempts yet
Problem
A delivery company has several collection hubs. Cargo can move between some pairs of hubs, and each route has a travel time.
For every ordered pair of different hubs, the company wants a route table that says which hub should be visited first when sending cargo along a shortest path. Rows are starting hubs and columns are destination hubs. If the entry in row r, column c is x, then cargo sent from hub r to hub c should first move to hub x.
Write a program that prints this route table.
Input
The first line contains two integers n and m.
nis the number of hubs, where1 <= n <= 200.mis the number of bidirectional routes, where1 <= m <= 10000.
Each of the next m lines contains three integers a, b, and t, meaning that cargo can move between hubs a and b in t time. All hub numbers and route times are positive integers no greater than 1000.
Output
Print n lines containing the route table. The j-th value on the i-th line is the first hub to visit on a shortest path from hub i to hub j. If i = j, print -. Separate values on each line with one space.