This page is still under construction.

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

Animal Farm

Time limit2sMemory limit512 MB

Summary
Given M pens sharing walls, find the minimum total wall-removal cost so that all animals gather in one connected region, inside one pen or outside all pens.
Level

Medium7 of 10

Topics
Graph, Minimum spanning tree, Union-find, Greedy
Solved
No attempts yet

Problem

You run a farm with NN animals (1≤N≤1001 \le N \le 100). From a store you bought M=NM = N pre-made pens to house them. The pens satisfy the following conditions:

  • each pen has between 33 and 88 edges (walls);
  • an edge that appears in two pens joins those two pens together;
  • an edge that appears in only one pen connects that pen to the outside (the area outside every pen);
  • initially there is exactly one animal in each pen and no animals outside the pens.

The animals love a game called "Escape from the pen." Every edge has a cost, and the animals want the minimum total cost to make all of them gather in the same area by trampling down the walls of various pens. The animals may gather inside one particular pen, or outside all of the pens. Once an edge has been trampled down, any animal may cross it afterwards at no additional cost.

Given the description of the pens and the placement of the animals, determine the smallest cost needed to move all of the animals into the same area.

Input

The first line contains the integer MM, the number of pens. Each of the next MM lines describes one pen. A description consists of three parts separated by single spaces:

  • the first part is an integer epe_p (3≤ep≤83 \le e_p \le 8), the number of edges of pen pp;
  • the second part is a sequence of epe_p integers giving the corners of the pen, where each integer is at most 10001000;
  • the third part is a sequence of epe_p integers giving the cost of each edge, where each integer is at most 50005000.

The corners and edge costs are listed in cyclic order. For example, the pen description

3 1 2 3 7 4 6

means the pen has three corners (and therefore three edges), where edge (1,2)(1, 2) has cost 77, edge (2,3)(2, 3) has cost 44, and edge (3,1)(3, 1) has cost 66.

Output

Output on a single line the minimum cost that lets all of the animals gather inside one pen or outside all of the pens.

Examples3

  1. Example 1

    Input
    4
    3 1 2 3 7 4 6
    4 1 2 4 5 7 7 2 6
    4 4 7 6 5 4 8 9 2
    5 3 2 4 7 8 4 7 4 7 7
    
    Expected output
    10
    
  2. Example 2

    Input
    1
    3 1 2 3 5 5 5
    
    Expected output
    0
    
  3. Example 3

    Input
    2
    3 1 2 3 10 20 30
    3 1 2 4 10 5 5
    
    Expected output
    10