This page is still under construction.

Parts of this page are still being built. What you see may change.

Making test data 5

Time limit1sMemory limit128 MB

Summary
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 10610^6, the run is treated as time limit exceeded.

Graph XX must satisfy the following.

  1. Code A must not exceed the time limit on XX.
  2. Code B must exceed the time limit on XX.

Many graphs would work, so the graph is fixed by the following rules.

Vertices are numbered 00 through V−1V-1.

  • Vertex 00 has SS self-loops of weight 11. If S=0S = 0, it has no outgoing edges.
  • For each i=1,2,…,V−1i = 1, 2, \ldots, V-1, vertex ii has exactly one outgoing edge to vertex i−1i-1 of weight 11.
  • There are QQ queries, each from V−1V-1 to 00.

The graph is a chain with nonnegative weights. ModifiedDijkstra pops each vertex from the priority queue about once. OptimizedBellmanFord scans vertices in order 0,1,…,V−10, 1, \ldots, V-1, so one full sweep advances the distance along the chain by a single hop. Self-loops at vertex 00 raise the edge count and do not create a shorter path.

Input

The first line contains three integers VV, SS, and QQ.

  • 1≤V≤3001 \le V \le 300
  • 0≤S0 \le S
  • 1≤Q≤101 \le Q \le 10
  • The number of edges V−1+SV - 1 + S is at most 50005000.

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 VV on the first line.

Then print VV lines. The line for vertex ii (with ii starting at 00) starts with nin_i, the number of outgoing edges, followed by nin_i pairs jj ww, where jj is the head and ww is the weight.

Then print QQ.

Then print QQ 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.

Examples3

  1. Example 1

    Input
    3 0 1
    
    Expected output
    3
    0
    1 0 1
    1 1 1
    1
    2 0
  2. Example 2

    Input
    3 2 2
    
    Expected output
    3
    2 0 1 0 1
    1 0 1
    1 1 1
    2
    2 0
    2 0
  3. Example 3

    Input
    1 0 1
    
    Expected output
    1
    0
    1
    0 0