Making test data 1
Time limit1sMemory limit128 MB
Construct one shortest-path test input that makes Floyd-Warshall exceed 10^6 operations while keeping Dijkstra under it, using as few integers as possible.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Greedy, Implementation
- Solved
- No attempts yet
Problem
Writing a good contest problem is hard. The hardest part is often the test data. Good tests must separate a solution that follows the intended approach from one that does not. They must also catch a program that is right on most inputs and fails only on a special case.
This task does not ask you to compute shortest paths. It asks you to print test data.
Build one input for a weighted directed shortest-path problem. must satisfy both of the following for the two programs below.
- Modified Dijkstra must not time out on .
- Floyd-Warshall must time out on .
Smaller data is better. may contain at most integers.
Both programs keep a of operations. If exceeds , the program times out.
uses this format.
The first line is the number of vertices . Vertices are numbered from to .
Each of the next lines describes the outgoing edges of one vertex, starting from vertex . The first number on the line is the out-degree , followed by pairs . Each pair is an edge from that vertex to with weight .
The next line is the number of queries . Each of the next lines has a start and a target .
The data must obey these limits.
- is a non-negative integer
- no query start can reach a negative cycle
If a pair is unreachable, its shortest-path value is .
If several inputs satisfy the conditions, keep the one with the fewest integers. If there is still a tie, compare the integers in the order they appear as a sequence and keep the lexicographically smallest sequence.
Input
There is no input.
Output
Print the unique input chosen by the rules above.
The first line is .
Each of the next lines is the out-degree and the edge list of one vertex. If a vertex has no outgoing edges, print on that line.
The next line is .
Each of the next lines is and separated by a space.
Hint
Floyd-Warshall runs on an adjacency matrix as follows.
counter = 0
for k = 0 to V-1:
for i = 0 to V-1:
for j = 0 to V-1:
counter = counter + 1
if counter > 1000000: TLE
M[i][j] = min(M[i][j], M[i][k] + M[k][j])
The iteration count is always . It does not depend on the number of edges or queries.
Modified Dijkstra runs the following for each query . Initially and every other distance is infinite.
counter = 0
for each query (s, t):
dist[s] = 0
pq.push((0, s))
while pq is not empty:
counter = counter + 1
if counter > 1000000: TLE
(d, u) = pop(pq)
if d == dist[u]:
for each edge (u, v, w):
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
pq.push((dist[v], v))
is the number of priority-queue pops. If a vertex is relaxed more than once, it can enter the queue more than once.