Animal Farm
Time limit2sMemory limit512 MB
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 animals (). From a store you bought pre-made pens to house them. The pens satisfy the following conditions:
- each pen has between and 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 , the number of pens. Each of the next lines describes one pen. A description consists of three parts separated by single spaces:
- the first part is an integer (), the number of edges of pen ;
- the second part is a sequence of integers giving the corners of the pen, where each integer is at most ;
- the third part is a sequence of integers giving the cost of each edge, where each integer is at most .
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 has cost , edge has cost , and edge has cost .
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.