Buy or Build
Time limit1sMemory limit128 MB
Pick a subset of up to 8 subnetworks to buy and build edges so all n cities connect, minimizing total cost.
- Level
Medium7 of 10
- Topics
- Minimum spanning tree, Graph, Brute force, Union-find
- Solved
- No attempts yet
Problem
World Wide Networks (WWN) operates large telecommunication networks and wants to build a new network in the country of Borduria that connects all of its largest cities at the smallest possible total cost.
Several local companies already run small subnetworks, each of which interconnects some of the cities. To provide connectivity, WWN may use either of two mechanisms:
- Build an edge directly between two cities. Its cost is exactly the square of the Euclidean distance between them: for cities at and the cost is .
- Buy a subnetwork. Buying subnetwork costs and immediately connects all cities that belong to it. A subnetwork must be bought as a whole and cannot be split.
Every city has integer coordinates. There are subnetworks, and is always small (); the internal layout of a subnetwork is irrelevant, because buying it makes all of its cities mutually connected.
Choose which subnetworks to buy and which edges to build so that all cities end up connected and the total cost — the prices of the bought subnetworks plus the cost of every built edge — is minimized.
Input
The first line contains two integers and (, ): the number of cities and the number of existing subnetworks. Cities are numbered from to .
Each of the next lines describes one subnetwork. The line begins with an integer , the number of cities in that subnetwork, then an integer , its price (), then the distinct city numbers belonging to it.
Each of the final lines contains two integers and (): the coordinates of city (city on the first of these lines, city on the second, and so on).
Output
Print a single integer: the minimum total cost to connect all cities.
Notes
The figures below are illustrations. The first two show a -city instance with subnetworks and a solution that buys the first and third subnetworks (thick edges belong to a bought subnetwork; thin edges are built from scratch). The last two show the -city instance from the example and one of its optimal solutions, in which the first and second subnetworks are bought and the remaining connecting edges are built from scratch, for a total cost of .



