|
<p> |
|
You've got yourself an unrooted tree with <strong>N</strong> nodes — that is, a connected, undirected graph with |
|
<strong>N</strong> nodes numbered from 1 to <strong>N</strong>, and <strong>N</strong> - 1 edges. |
|
The <strong>i</strong>th edge connects nodes <strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong>. |
|
</p> |
|
|
|
<p> |
|
You'd like to spend as little money as possible to label each node with a number from 1 to <strong>K</strong>, inclusive. |
|
It costs <strong>C<sub>i,j</sub></strong> dollars to label the <strong>i</strong>th node with the number <strong>j</strong>. |
|
</p> |
|
|
|
<p> |
|
Additionally, after the whole tree has been labelled, you must pay <strong>P</strong> more dollars for each node which has at least one pair of |
|
neighbours that share the same label as each other. In other words, for each node <strong>u</strong>, you must pay <strong>P</strong> dollars |
|
if there exist two other nodes <strong>v</strong> and <strong>w</strong> which are both adjacent to node <strong>u</strong>, |
|
such that the labels on nodes <strong>v</strong> and <strong>w</strong> are equal (note that node <strong>u</strong>'s label is irrelevant). |
|
You only pay the penalty of <strong>P</strong> dollars once for a given central node <strong>u</strong>, |
|
even if it has multiple pairs of neighbours which satisfy the above condition. |
|
</p> |
|
|
|
<p> |
|
What's the minimum cost (in dollars) to label all <strong>N</strong> nodes? |
|
</p> |
|
|
|
|
|
<h3>Input</h3> |
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of trees. |
|
For each tree, there is first a line containing the space-separated integers <strong>N</strong>, <strong>K</strong>, and <strong>P</strong>. |
|
Then, <strong>N</strong> lines follow, the <strong>i</strong>th of which contains the space-separated integers |
|
<strong>C<sub>i,1</sub></strong> through <strong>C<sub>i,K</sub></strong> in order. |
|
Then, <strong>N</strong> - 1 lines follow, the <strong>i</strong>th of which contains the space-separated integers |
|
<strong>A<sub>i</sub></strong> and <strong>B<sub>i</sub></strong> |
|
</p> |
|
|
|
|
|
<h3>Output</h3> |
|
<p> |
|
For the <strong>i</strong>th tree, print a line containing "Case #<strong>i</strong>: " followed by the minimum cost to label all of the tree's nodes. |
|
</p> |
|
|
|
|
|
<h3>Constraints</h3> |
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 30 <br /> |
|
1 ≤ <strong>N</strong> ≤ 1,000 <br /> |
|
1 ≤ <strong>K</strong> ≤ 30 <br /> |
|
0 ≤ <strong>P</strong> ≤ 1,000,000 <br /> |
|
0 ≤ <strong>C<sub>i,j</sub></strong> ≤ 1,000,000 <br /> |
|
1 ≤ <strong>A<sub>i</sub></strong>, <strong>B<sub>i</sub></strong> ≤ <strong>N</strong> <br /> |
|
</p> |
|
|
|
|
|
<h3>Explanation of Sample</h3> |
|
<p> |
|
In the first case, there is only one node which must be painted the only possible color for 111 dollars. In the second case, there is only one color, so a penalty of 8 dollars must be paid since node 2 has two neighbors with the same color. In total we pay 1 + 2 + 4 + 8 = 15 dollars. In the third case, it's optimal to paint nodes 1 and 2 with color 1, and node 3 with color 2. The total cost is 4 + 8 + 3 = 15 dollars. |
|
</p> |
|
|