This page is still under construction.

Parts of this page are still being built. What you see may change.

Zebra Herd

Interview

Time limit7sMemory limit128 MB

Summary
Assign each of z zebras one of two colors at each of t times, minimizing same-color distance costs, opposite-color bonuses, and color-change penalties.
Level

Medium6 of 10

Topics
Dynamic programming, Bit manipulation, Brute force, Math
Solved
No attempts yet

Problem

Zebras are highly social animals. Like other members of the horse family, they form groups that tend to stay together and spend time with one another fairly regularly, though not exclusively. Researchers want to understand how communities of zebras evolve over time and what triggers those changes. All they have to work with are observations of where the zebras are at various moments in time, and from those they would like to recover the most natural groupings.

The working assumptions are:

  • (a) if a zebra belongs to a group, it tends to stay close to the other members of that group;
  • (b) if a zebra does not belong to a group, it tends to stay farther from the members of that group;
  • (c) a zebra rarely changes its group membership.

Let us make this precise. You are given a sequence of observations. At each observation time you know the exact location of every zebra, and the distance between two zebras is their Euclidean (straight-line) distance. Assume the herd splits into exactly two groups, which we denote by two colors. For every time step we color each zebra either red or blue to indicate which group it belongs to.

To model assumption (c), we charge a penalty of cc every time a single zebra changes its color between two consecutive time steps. To model assumptions (a) and (b), at each time step we look at the distance d(i,j)d(i, j) between every pair of zebras ii and jj: if ii and jj have the same color we charge a penalty of a⋅d(i,j)a \cdot d(i, j) for that pair, and if they have opposite colors we charge a penalty of −b⋅d(i,j)-b \cdot d(i, j) (that is, a bonus).

Given a coloring of all zebras for every time step, the total penalty is the sum of all pair penalties over all time steps plus all color-change penalties. Your task is to find the smallest total penalty achievable by any coloring. You only need to output this minimum total penalty, not the coloring itself.

Input

The first line contains the number KK of data sets. Each of the KK data sets has the following form.

The first line of a data set contains two integers zz and tt: the number of zebras 2≤z≤102 \le z \le 10 and the number of time steps 2≤t≤502 \le t \le 50. The next line contains three non-negative floating-point numbers aa, bb, and cc, the penalty multipliers. Then follow tt lines describing the zebra positions. Each of these lines contains 2z2z floating-point numbers giving the positions as x1 y1 x2 y2 … xz yzx_1\ y_1\ x_2\ y_2\ \dots\ x_z\ y_z. The first such line gives the positions at time 11, the second at time 22, and so on.

Output

For each data set, print a line Data Set x: where xx is the data set's number (starting from 11). On the following line, print the minimum total penalty that any coloring over time can achieve, rounded to exactly two decimals. Separate consecutive data sets with a single blank line.

Examples2

  1. Example 1

    Input
    1
    5 10
    1.0 1.0 20.0
    0.0 0.0 0.0 0.5 0.0 1.0 10.0 0.0 10.0 0.5
    0.0 0.0 0.0 0.5 10.0 1.0 10.0 0.0 10.0 0.5
    0.0 0.0 0.0 0.5 0.0 1.0 10.0 0.0 10.0 0.5
    0.0 0.0 0.0 0.5 8.0 1.0 10.0 0.0 10.0 0.5
    0.0 0.0 0.0 0.5 0.0 1.0 10.0 0.0 10.0 0.5
    0.0 0.0 0.0 0.5 0.0 1.0 10.0 0.0 10.0 0.5
    0.0 0.0 0.0 0.5 9.0 1.0 10.0 0.0 10.0 0.5
    0.0 0.0 0.0 0.5 9.0 1.0 10.0 0.0 10.0 0.5
    0.0 0.0 0.0 0.5 9.0 1.0 10.0 0.0 10.0 0.5
    0.0 0.0 0.0 0.5 9.0 1.0 10.0 0.0 10.0 0.5
    
    Expected output
    Data Set 1:
    -476.30
    
  2. Example 2

    Input
    1
    2 2
    1.0 1.0 5.0
    0.0 0.0 3.0 4.0
    0.0 0.0 3.0 4.0
    
    Expected output
    Data Set 1:
    -10.00