Under construction

Still being built. Things may change

Slate Modern (Small)

Time limit5sMemory limit512 MB

Summary
Given some pre-filled cells in an R by C grid with an edge-adjacency difference limit D, decide if the rest can be filled with positive integers and find the maximum total sum modulo 1e9+7.
Level

Medium7 of 10

Topics
Graph, Shortest path, Math, Implementation
Solved
No attempts yet

Problem

The Slate Modern gallery shows only grayscale paintings that follow strict rules. A painting in this gallery must be a grid with RR rows and CC columns, and every cell is painted with a positive integer brightness value. So that a painting is not too jarring, any two cells that share an edge (sharing only a corner does not count) must have brightness values that differ by at most DD.

Your artist friend Cody-Jamal is working on a canvas for the gallery. Last night he filled in NN different cells with positive integer brightness values. He heard the gallery rules only today, and now he wants to know whether every remaining cell can be filled with a positive integer brightness value without breaking the rules. If the painting can be finished, he wants the sum of all brightness values to be as large as possible so that he saves black paint. Find that sum, or report that finishing the painting is impossible. The sum can be very large, so print it modulo the prime 109+710^9+7.

Input

The first line contains the number of test cases TT. TT test cases follow. Each test case begins with one line holding four integers RR, CC, NN, and DD. Then NN lines follow. The ii-th of them holds three integers RiR_i, CiC_i, and BiB_i, meaning that the cell in row RiR_i and column CiC_i has brightness value BiB_i. Rows and columns are numbered from 1.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤R≤2001 \le R \le 200
  • 1≤C≤2001 \le C \le 200
  • 1≤N≤2001 \le N \le 200
  • 1≤D≤1091 \le D \le 10^9
  • 1≤Ri≤R1 \le R_i \le R and 1≤Ci≤C1 \le C_i \le C for all ii
  • 1≤Bi≤1091 \le B_i \le 10^9 for all ii. This upper bound applies only to the cells Cody-Jamal already painted. You may assign brightness values larger than 10910^9 to the other cells.
  • N<R×CN < R \times C, so at least one cell is empty.
  • If i≠ji \ne j then Ri≠RjR_i \ne R_j or Ci≠CjC_i \ne C_j, so all of the given cells are different.

Output

For each test case, print one line of the form Case #x: y, where x is the test case number starting from 1 and y is IMPOSSIBLE if the painting cannot be finished, and otherwise the maximum possible sum of all brightness values modulo the prime 109+710^9+7.

Hint

In the first test case the best way to finish the painting is

6 7 9
4 6 8

and the sum is 40.

In the second test case the best way is 2000000000 1000000000, whose sum is 3000000000. Modulo 109+710^9+7 that is 999999986.

The third test case is impossible. Whatever value goes into the cell in row 2, it differs too much from at least one of its two painted neighbors.

The fourth test case is impossible because the two cells Cody-Jamal already painted have brightness values that are too far apart.

Examples1

  1. Example 1

    Input
    4
    2 3 2 2
    2 1 4
    1 2 7
    1 2 1 1000000000
    1 2 1000000000
    3 1 2 100
    1 1 1
    3 1 202
    2 2 2 2
    2 1 1
    2 2 4
    
    Expected output
    Case #1: 40
    Case #2: 999999986
    Case #3: IMPOSSIBLE
    Case #4: IMPOSSIBLE