Under construction

Still being built. Things may change

Making shortest-path test data

Time limit1sMemory limit128 MB

Summary
Print the prescribed shortest-path test graph: a chain with a computed number of self-loops at vertex 0, plus Q queries from V-1 to 0.
Level

Easy3 of 10

Topics
Implementation, Graph, Math, Brute force
Solved
No attempts yet

Problem

A graph problem for a programming contest needs test data that lets an intended solution finish and makes a slow solution exceed the time limit. This task asks you to print such data. Many graphs can meet the time-limit conditions, so print only the construction fixed below.

The target problem is shortest paths on a directed weighted graph. Vertices are numbered 00 to V−1V-1. For a query (s,t)(s, t), let p(s,t)p(s, t) be the total weight of a shortest path from ss to tt. If tt is unreachable from ss, then p(s,t)=109p(s, t) = 10^9.

Both implementations treat a run as time limit exceeded when counter in the code exceeds 10610^6.

Floyd-Warshall runs the nested loops below on an adjacency matrix, so counter is always V3V^3. If V≤100V \le 100, Floyd-Warshall does not exceed the time limit.

counter = 0
for k = 0 to V-1:
    for i = 0 to V-1:
        for j = 0 to V-1:
            counter += 1
            M[i][j] = min(M[i][j], M[i][k] + M[k][j])

Optimized Bellman-Ford, for each query, resets distances and then relaxes every adjacency-list edge for at most V−1V-1 rounds. Vertices are scanned from 00 to V−1V-1. If a round makes no update, the loop stops. counter increases by 11 each time an edge is examined.

counter = 0
for each query (s, t):
    set dist[v] = 10^9 for every vertex v
    dist[s] = 0
    repeat V-1 times:
        change = false
        for each edge (u, v) in adjacency-list order:
            counter += 1
            if dist[u] + weight(u, v) < dist[v]:
                dist[v] = dist[u] + weight(u, v)
                change = true
        if change is false:
            break

The graph you print is uniquely determined as follows.

There are VV vertices 0,1,…,V−10, 1, \ldots, V-1. For each i=1,2,…,V−1i = 1, 2, \ldots, V-1, put one edge from ii to i−1i-1 with weight 11. Vertex 00 has KK self-loops of weight 11. KK is the largest nonnegative integer that satisfies both of the following.

  • The number of edges E=(V−1)+KE = (V-1)+K is at most 50005000.
  • The number of integers in the output F=3V+2Q+2KF = 3V + 2Q + 2K is at most TT.

There are QQ queries, each with source V−1V-1 and target 00.

On this graph, Floyd-Warshall's counter is V3V^3. Optimized Bellman-Ford relaxes one hop of the chain per round, so it runs all V−1V-1 rounds and counter equals Q×E×(V−1)Q \times E \times (V-1). When V=100V = 100, Q=10Q = 10, and T=2222T = 2222, Floyd-Warshall does not exceed the time limit and optimized Bellman-Ford does.

Input

The first line contains three integers VV, QQ, and TT: the number of vertices, the number of queries, and the upper bound on the number of integers in the output.

2≤V≤1002 \le V \le 100

1≤Q≤101 \le Q \le 10

3V+2Q≤T≤22223V + 2Q \le T \le 2222

Output

Print the data in the shortest-path input format.

Print VV on the first line.

Then print VV lines for vertices 00 through V−1V-1. Each line starts with the out-degree nin_i, followed by nin_i pairs jj ww, where jj is the head and ww is the weight.

  • Vertex 00: n0=Kn_0 = K, and every pair is 00 11.
  • Vertex ii (1≤i≤V−11 \le i \le V-1): ni=1n_i = 1, and the pair is i−1i-1 11.

Print QQ on the next line.

Then print QQ query lines. Every line is V−1V-1 00.

Separate adjacent integers on a line with a single space. Do not put trailing spaces.

Notes

Count integers in the output as follows. One integer for VV, VV integers for the out-degrees nin_i, 2E2E integers for the pairs (j,w)(j, w), one integer for QQ, and 2Q2Q integers for the pairs (s,t)(s, t). Hence F=V+2E+2Q+2F = V + 2E + 2Q + 2. Substituting E=(V−1)+KE = (V-1)+K gives F=3V+2Q+2KF = 3V + 2Q + 2K.

The graph must also satisfy the following. Every edge weight has absolute value less than 10610^6, and no negative cycle is reachable from a query source. All weights in this construction are 11, so both hold.

A positive self-loop never improves the distance of vertex 00. Only the chain V−1→V−2→⋯→0V-1 \to V-2 \to \cdots \to 0 is relaxed, one hop per round.

Examples4

  1. Example 1

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

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

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

    Input
    2 1 10
    
    Expected output
    2
    1 0 1
    1 0 1
    1
    1 0