Making test data 5
Time limit1sMemory limit128 MB
Print a fixed chain graph with self-loops and queries so that ModifiedDijkstra stays under the counter limit while OptimizedBellmanFord exceeds it.
- Level
Medium5 of 10
- Topics
- Graph, Shortest path, Implementation, Simulation
- Solved
- No attempts yet
Problem
Writing a contest problem is hard, and the hardest part is often the test data. Good tests separate a solution that matches the intended algorithm from one that does not.
This task does not ask you to compute shortest paths. It asks you to print a directed weighted graph that separates two implementations.
Code A is ModifiedDijkstra. Code B is OptimizedBellmanFord. Both keep a counter. If the counter exceeds , the run is treated as time limit exceeded.
Graph must satisfy the following.
- Code A must not exceed the time limit on .
- Code B must exceed the time limit on .
Many graphs would work, so the graph is fixed by the following rules.
Vertices are numbered through .
- Vertex has self-loops of weight . If , it has no outgoing edges.
- For each , vertex has exactly one outgoing edge to vertex of weight .
- There are queries, each from to .
The graph is a chain with nonnegative weights. ModifiedDijkstra pops each vertex from the priority queue about once. OptimizedBellmanFord scans vertices in order , so one full sweep advances the distance along the chain by a single hop. Self-loops at vertex raise the edge count and do not create a shorter path.
Input
The first line contains three integers , , and .
- The number of edges is at most .
Output
Print the graph and the queries in the format below. Separate integers on the same line with a single space. Do not put a trailing space at the end of a line.
Print on the first line.
Then print lines. The line for vertex (with starting at ) starts with , the number of outgoing edges, followed by pairs , where is the head and is the weight.
Then print .
Then print lines, each with the source and the target of one query.
Hint
Pseudocode for the two implementations follows. The counter is the time-limit meter.
ModifiedDijkstra
counter = 0
for each query (s, t):
dist[u] = INF for all u
dist[s] = 0
pq.push((0, s))
while pq is not empty:
counter += 1
(d, u) = pq.pop()
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))
output dist[t]
OptimizedBellmanFord
counter = 0
for each query (s, t):
dist[u] = INF for all u
dist[s] = 0
loop V - 1 times:
change = false
for each edge (u, v, w) in adjacency list order:
counter += 1
if dist[u] + w < dist[v]:
dist[v] = dist[u] + w
change = true
if change is false:
break
output dist[t]
Edges are scanned from smaller vertex indices to larger ones. Edges of the same vertex are scanned in the order they are printed.