Under construction

Still being built. Things may change

Jurisdiction Restrictions

Time limit30sMemory limit1024 MB

Summary
Given a grid and police stations with square patrol ranges, assign each coverable block to one station so the gap between the largest and smallest counts is minimal.
Level

Medium7 of 10

Topics
Bit manipulation, Graph, Binary search
Solved
No attempts yet

Problem

The city of Gridtopia is a matrix of square cells ("blocks") with RR rows and CC columns. Rows are numbered from 1 at the top to RR at the bottom, and columns are numbered from 1 at the left to CC at the right. The city is served by SS police stations. The ii-th station is in the block at row RiR_i and column CiC_i. No block contains more than one station.

Each station can only patrol blocks that are no more than DiD_i blocks away from it, horizontally or vertically. The ii-th station can patrol the block at row R′R' and column C′C' if max⁡(∣R′−Ri∣,∣C′−Ci∣)≤Di\max(|R' - R_i|, |C' - C_i|) \le D_i. Put another way, the ii-th station can patrol only the blocks inside the square of side length 2Di+12D_i + 1 centered on that station.

You are the new police commissioner. Assign some blocks to exactly one station that can patrol them. Blocks that contain stations and blocks that no station can patrol are not assigned. Every other block must be assigned. Let AiA_i be the number of blocks assigned to the ii-th station. Distribute the load as evenly as possible by minimizing the difference between the maximum and the minimum of all AiA_i values. What is the smallest possible difference?

Input

The first line contains TT, the number of test cases. Each case begins with a line containing RR, CC, and SS. Then SS lines follow, each with RiR_i, CiC_i, and DiD_i.

Output

For each test case, output one line Case #x: y, where xx is the test case number (starting from 1) and yy is the difference described above.

Limits

  • 1≤T≤1001 \le T \le 100.
  • 2≤S≤152 \le S \le 15.
  • 1≤Ri≤R1 \le R_i \le R for all ii.
  • 1≤Ci≤C1 \le C_i \le C for all ii.
  • For all i≠ji \ne j, Ri≠RjR_i \ne R_j and/or Ci≠CjC_i \ne C_j. (No two stations are in the same block.)
  • 1≤Di<max⁡(R,C)1 \le D_i < \max(R, C) for all ii.

Hint

In Sample Case #1, the city is a grid with 3 rows and 4 columns, with one station in the upper left block and one station in the block to the left of the lower right block. The first station can only patrol the three blocks that touch the edge or corner of its block. Every other block is more than 1 block away horizontally or vertically. The second station can patrol any block in the grid except the blocks containing the stations. The difference in the number of assigned blocks is minimized if station 1 is assigned all three of the blocks it can patrol, and the remaining seven blocks are assigned to station 2.

In Sample Case #2, one optimal strategy is shown below. 1 is station 1, 2 is station 2, ! is a block assigned to station 1, @ is a block assigned to station 2, and . is a block assigned to neither station. A station's assigned blocks do not need to form a single continuous area.

@@@@.
!!!@.
!2!@.
1!!@.
!@!@.

Examples1

  1. Example 1

    Input
    2
    3 4 2
    1 1 1
    3 3 2
    5 5 2
    4 1 2
    3 2 2
    
    Expected output
    Case #1: 4
    Case #2: 0