Making test data 6
Time limit1sMemory limit128 MB
Print a fixed directed weighted graph with K triangles and Q queries that makes ModifiedDijkstra exceed a counter limit while OptimizedBellmanFord stays under it.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Implementation, Greedy
- 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 OptimizedBellmanFord. Code B is ModifiedDijkstra. 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.
Integers and are given. The number of vertices is . Vertices are numbered through .
- Vertex has no outgoing edges.
- Vertex has one edge to vertex of weight .
- Vertex has one edge to vertex of weight .
- For each , attach the following triangle.
- Vertex has one edge to vertex of weight .
- Vertex has two outgoing edges. The first goes to vertex with weight . The second goes to vertex with weight .
- There are queries, each from to .
The graph is a chain of triangles that include negative weights. ModifiedDijkstra reinserts the same vertices into the priority queue an exponential number of times, so the counter exceeds when is large. OptimizedBellmanFord sees no negative cycle and only a few vertices and edges, so its counter stays under the limit.
Input
The first line contains two integers and .
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, where .
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.
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]
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]
Edges are scanned from smaller vertex indices to larger ones. Edges of the same vertex are scanned in the order they are printed. The ModifiedDijkstra priority queue is a min-heap on .