A. Shortest Path
Shortest Path
You cannot submit for this problem because the contest is ended. You can click "Open in Problem Set" to view this problem in normal mode.
Description
In this lab, you are a network engineer managing a large computer network. Your system connects several servers (nodes). When a new network connection is established (an edge), it has a specific latency, measured in milliseconds (ms). This is the time it takes for a data packet to travel from one server to the other.
Because this latency represents time, it is always a positive cost (you can't travel back in time!).
Your goal is to implement a routing calculator. Given a "source" server (the node you are at), you need to find the best route to all other servers in the network.
For every possible destination, you must compute two things:
- The total latency of the fastest possible path (the shortest path cost).
- The "next stop" (or "next hop"). This is the first server on that fastest path that you should send your packet to.
For example, if the best path from to is , the total latency is the sum of latencies , and the next stop is .
You will be given the number of servers, a list of all the network links, and a single source server.
You will need to:
- Represent the Network: Use an adjacency list to store the graph. All connections are bi-directional.
- Implement Dijkstra's Algorithm:
- Use a priority queue (min-heap) to efficiently select the next node to visit.
- Hint: To find the "next stop," you will need to store the predecessor (or
parent) of each node in the shortest path tree. After Dijkstra's is complete, to find the next hop for a destination , you must trace the path all the way back to . The "next hop" is the node on this path that comes immediately after .
Format
Input
- The first line contains two integers, and (, ), representing the number of servers (nodes) and the number of network connections (edges). The servers are numbered to .
- The next lines each contain three integers: , , and (, ). This represents a bi-directional connection between server and with a latency of ms.
- The last line contains a single integer (), representing the source server.
Output
- Print lines of output, one for each server (from to ).
- Each line must contain two space-separated integers:
- The minimum latency from the source to server .
- The ID of the next hop on the path from to .
- If server is the source itself (), output
0 -1(latency is 0, and there is no next hop). - If server is unreachable from , output
-1 -1.
Samples
5 7
0 1 10
0 2 3
1 2 1
1 3 2
2 3 8
2 4 2
3 4 5
0
0 -1
4 2
3 2
6 2
5 2
Explanation for Sample 1: The source is Node 0.
- To 0 (Source): Latency is 0, next hop is -1. Output:
0 -1 - To 1: Path is . Latency = 3 + 1 = 4. The next hop from 0 is 2. Output:
4 2 - To 2: Path is . Latency = 3. The next hop from 0 is 2. Output:
3 2 - To 3: Path is . Latency = 3 + 1 + 2 = 6. The next hop from 0 is 2. Output:
6 2 - To 4: Path is . Latency = 3 + 2 = 5. The next hop from 0 is 2. Output:
5 2
4 2
0 1 5
1 2 2
0
0 -1
5 1
7 1
-1 -1
Explanation for Sample 2: The source is Node 0.
- To 0 (Source): Latency is 0, next hop is -1. Output:
0 -1 - To 1: Path is . Latency = 5. The next hop from 0 is 1. Output:
5 1 - To 2: Path is . Latency = 5 + 2 = 7. The next hop from 0 is 1. Output:
7 1 - To 3: Node 3 is unreachable. Output:
-1 -1
Hint
- Since all link latencies (edge weights) are positive, this is a classic application of Dijkstra's algorithm.
- To retrieve the "next hop," you will need to backtrack from each destination node to the source using the
parentarray built during Dijkstra's execution.
Limitation
1s, 128MiB for each test case.
Assignment 3
- Status
- Done
- Problem
- 1
- Open Since
- 2025-11-9 0:00
- Deadline
- 2025-11-23 23:59
- Extension
- 72 hour(s)