Making shortest-path test data
Time limit1sMemory limit128 MB
Print the prescribed shortest-path test graph: a chain with a computed number of self-loops at vertex 0, plus Q queries from V-1 to 0.
- Level
Easy3 of 10
- Topics
- Implementation, Graph, Math, Brute force
- Solved
- No attempts yet
Problem
A graph problem for a programming contest needs test data that lets an intended solution finish and makes a slow solution exceed the time limit. This task asks you to print such data. Many graphs can meet the time-limit conditions, so print only the construction fixed below.
The target problem is shortest paths on a directed weighted graph. Vertices are numbered to . For a query , let be the total weight of a shortest path from to . If is unreachable from , then .
Both implementations treat a run as time limit exceeded when counter in the code exceeds .
Floyd-Warshall runs the nested loops below on an adjacency matrix, so counter is always . If , Floyd-Warshall does not exceed the time limit.
counter = 0
for k = 0 to V-1:
for i = 0 to V-1:
for j = 0 to V-1:
counter += 1
M[i][j] = min(M[i][j], M[i][k] + M[k][j])
Optimized Bellman-Ford, for each query, resets distances and then relaxes every adjacency-list edge for at most rounds. Vertices are scanned from to . If a round makes no update, the loop stops. counter increases by each time an edge is examined.
counter = 0
for each query (s, t):
set dist[v] = 10^9 for every vertex v
dist[s] = 0
repeat V-1 times:
change = false
for each edge (u, v) in adjacency-list order:
counter += 1
if dist[u] + weight(u, v) < dist[v]:
dist[v] = dist[u] + weight(u, v)
change = true
if change is false:
break
The graph you print is uniquely determined as follows.
There are vertices . For each , put one edge from to with weight . Vertex has self-loops of weight . is the largest nonnegative integer that satisfies both of the following.
- The number of edges is at most .
- The number of integers in the output is at most .
There are queries, each with source and target .
On this graph, Floyd-Warshall's counter is . Optimized Bellman-Ford relaxes one hop of the chain per round, so it runs all rounds and counter equals . When , , and , Floyd-Warshall does not exceed the time limit and optimized Bellman-Ford does.
Input
The first line contains three integers , , and : the number of vertices, the number of queries, and the upper bound on the number of integers in the output.
Output
Print the data in the shortest-path input format.
Print on the first line.
Then print lines for vertices through . Each line starts with the out-degree , followed by pairs , where is the head and is the weight.
- Vertex : , and every pair is .
- Vertex (): , and the pair is .
Print on the next line.
Then print query lines. Every line is .
Separate adjacent integers on a line with a single space. Do not put trailing spaces.
Notes
Count integers in the output as follows. One integer for , integers for the out-degrees , integers for the pairs , one integer for , and integers for the pairs . Hence . Substituting gives .
The graph must also satisfy the following. Every edge weight has absolute value less than , and no negative cycle is reachable from a query source. All weights in this construction are , so both hold.
A positive self-loop never improves the distance of vertex . Only the chain is relaxed, one hop per round.