Under construction

Still being built. Things may change

K-matching

Time limit9sMemory limit512 MB

Summary
Given an n by m grid graph with edge weights, find the minimum total weight of K edges that share no endpoints (a K-matching).
Level

Hard8 of 10

Topics
Dynamic programming, Binary search, Graph
Solved
No attempts yet

Problem

Consider a graph GG with n⋅mn \cdot m nodes (i,j)(i, j) (1≤i≤n1 \leq i \leq n, 1≤j≤m1 \leq j \leq m). There is an edge between two nodes (a,b)(a, b) and (c,d)(c, d) if and only if ∣a−c∣+∣b−d∣=1|a - c| + |b - d| = 1. Each edge has a weight.

Find the minimum weight of a KK-matching in GG.

An edge set SS is a matching of G=⟨V,E⟩G = \langle V, E \rangle if and only if each node in VV is connected to at most one edge in SS. A matching SS is a KK-matching if and only if ∣S∣=K|S| = K. The weight of a matching SS is the sum of the weights of the edges in SS. The minimum weight KK-matching of GG is the KK-matching with the smallest possible weight.

Input

The first line contains an integer tt, the number of test cases (1≤t≤10001 \leq t \leq 1000). At most 33 test cases have n>100n > 100.

For each test case, the first line contains three integers nn, mm and KK (1≤n≤4⋅1041 \leq n \leq 4 \cdot 10^4, 1≤m≤41 \leq m \leq 4, 1≤K≤⌊n⋅m2⌋1 \leq K \leq \lfloor \frac{n \cdot m}{2} \rfloor).

Then n−1n - 1 lines follow, each containing mm integers Ai,jA_{i, j}: the weights of the edges between (i,j)(i, j) and (i+1,j)(i + 1, j) (1≤Ai,j≤1091 \leq A_{i, j} \leq 10^9).

If m>1m > 1, then nn more lines follow, each containing m−1m - 1 integers Bi,jB_{i, j}: the weight of the edge between (i,j)(i, j) and (i,j+1)(i, j + 1) (1≤Bi,j≤1091 \leq B_{i, j} \leq 10^9).

Output

For each test case, print a single line containing the minimum weight.

Examples1

  1. Example 1

    Input
    3
    3 3 1
    3 4 5
    8 9 10
    1 2
    6 7
    11 12
    3 3 2
    3 4 5
    8 9 10
    1 2
    6 7
    11 12
    3 3 3
    3 4 5
    8 9 10
    1 2
    6 7
    11 12
    
    Expected output
    1
    5
    12