Making test data 6

Time limit1sMemory limit128 MB

Summary
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 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.

Integers KK and QQ are given. The number of vertices is V=2K+1V = 2K+1. Vertices are numbered 00 through V−1V-1.

  • Vertex 00 has no outgoing edges.
  • Vertex 11 has one edge to vertex 00 of weight 11.
  • Vertex 22 has one edge to vertex 11 of weight 11.
  • For each t=1,2,…,K−1t = 1, 2, \ldots, K-1, attach the following triangle.
    • Vertex 2t+12t+1 has one edge to vertex 2t2t of weight −2t+1-2^{t+1}.
    • Vertex 2t+22t+2 has two outgoing edges. The first goes to vertex 2t+12t+1 with weight 2t2^t. The second goes to vertex 2t2t with weight 00.
  • There are QQ queries, each from V−1V-1 to 00.

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 10610^6 when KK 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 KK and QQ.

  • 1≤K≤161 \le K \le 16
  • 1≤Q≤101 \le Q \le 10

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, where V=2K+1V = 2K+1.

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.

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 (d,u)(d, u).

Examples3

  1. Example 1

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

    Input
    2 1
    
    Expected output
    5
    0
    1 0 1
    1 1 1
    1 2 -4
    2 3 2 2 0
    1
    4 0
  3. Example 3

    Input
    3 2
    
    Expected output
    7
    0
    1 0 1
    1 1 1
    1 2 -4
    2 3 2 2 0
    1 4 -8
    2 5 4 4 0
    2
    6 0
    6 0