#P16021. [ICPC 2021 NAC] The King' s Guards

[ICPC 2021 NAC] The King' s Guards

题目描述

In a certain kingdom, the king wants to protect his citizens by deploying guards. He has recruited a number of guards, and has outfitted them with heavy armor for protection from bandits, foreign knights, and other ne’er-do-wells. His guards are tough, but unfortunately they aren’t very bright and will attack anyone wearing armor, even each other!

The kingdom consists of a number of villages, connected by roads. All of the roads are of poor quality. Some are muddy, some have rickety bridges. None of them can support a guard in full armor. So, the king must decide which roads to improve so that his guards can reach the entire kingdom. The roads are bidirectional. Each guard can only be deployed to a single village in a certain subset of the kingdom’s villages.

The king needs to minimize the cost of improving roads, while satisfying several other constraints:

  • Every guard must be deployed; none must be left out.
  • Every guard must be deployed in their subset of villages.
  • Every village must be reachable by exactly one guard. If two guards can reach each other, they’ll fight.

Help the king determine the minimum cost of improving the roads of his kingdom while satisfying the above constraints.

输入格式

The first line of input contains three integers nn (1n3001 \le n \le 300), rr (0rn(n1)20 \le r \le \frac{n(n-1)}{2}) and gg (1gn1 \le g \le n), where nn is the number of villages, rr is the number of roads, and gg is the number of guards. The villages are numbered 11 through nn.

Each of the next rr lines contains three integers aa, bb (1a<bn1 \le a < b \le n) and cc (1c1,0001 \le c \le 1,000). Each line describes a road between village aa and village bb, costing cc to improve. The roads are bidirectional; a guard can go from aa to bb or from bb to aa. Every pair of villages has at most one road between them.

Each of the next gg lines starts with an integer kk (1kn1 \le k \le n), and then contains kk integers vv (1vn1 \le v \le n). Each line describes the villages comprising the subset where one particular guard may be placed. The subsets may overlap.

输出格式

Output a single integer, which is the minimum cost the king must pay to improve enough roads so that every village is reachable by exactly one guard, and every guard is deployed. Output 1-1 if it isn’t possible to deploy the guards in a way that satisfies all of the constraints.

5 6 2
1 2 1
1 3 4
2 4 2
2 5 5
3 4 7
4 5 3
2 1 2
2 2 4
8