QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 1024 MB Total points: 100

#1825. 國王的衛兵

Statistics

在某個王國中,國王想要透過部署守衛來保護他的子民。他招募了若干名守衛,並為他們配備了重型盔甲,以防禦強盜、外國騎士和其他不法之徒。他的守衛雖然強悍,但遺憾的是他們不太聰明,會攻擊任何穿著盔甲的人,甚至是彼此!

王國由若干個村莊組成,村莊之間由道路連接。所有的道路品質都很差,有些泥濘不堪,有些則有搖搖欲墜的橋樑。沒有任何一條道路能支撐穿著全套盔甲的守衛。因此,國王必須決定改善哪些道路,以便他的守衛能夠到達整個王國。道路是雙向的。每位守衛只能被部署在王國中特定村莊子集內的其中一個村莊。

國王需要最小化改善道路的成本,同時滿足以下幾個限制:

  • 每一位守衛都必須被部署;不能有守衛被遺漏。
  • 每一位守衛都必須部署在他們所屬的村莊子集內。
  • 每一個村莊都必須恰好由一位守衛負責(可到達)。如果兩位守衛可以互相到達,他們就會打架。

請協助國王在滿足上述限制的前提下,計算出改善王國道路所需的最低成本。

輸入格式

輸入的第一行包含三個整數 $n$ ($1 \le n \le 300$)、$r$ ($0 \le r \le \frac{n(n-1)}{2}$) 和 $g$ ($1 \le g \le n$),其中 $n$ 是村莊的數量,$r$ 是道路的數量,$g$ 是守衛的數量。村莊編號為 $1$ 到 $n$。

接下來的 $r$ 行,每行包含三個整數 $a, b$ ($1 \le a < b \le n$) 和 $c$ ($1 \le c \le 1000$)。每行描述一條連接村莊 $a$ 和村莊 $b$ 的道路,改善成本為 $c$。道路是雙向的;守衛可以從 $a$ 走到 $b$ 或從 $b$ 走到 $a$。每對村莊之間最多只有一條道路。

接下來的 $g$ 行,每行開頭為一個整數 $k$ ($1 \le k \le n$),隨後包含 $k$ 個整數 $v$ ($1 \le v \le n$)。每行描述了某位特定守衛可以被放置的村莊子集。這些子集可能會重疊。

輸出格式

輸出一個整數,代表國王為了讓每個村莊都恰好由一位守衛負責,且每位守衛都被部署,所必須支付的改善道路之最低成本。如果無法以滿足所有限制的方式部署守衛,則輸出 $-1$。

範例

範例輸入 1

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

範例輸出 1

8

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#598Editorial Open集训队作业 解题报告 by 陈翔乐Qingyu2026-01-02 22:49:48 Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.