QOJ.ac

QOJ

実行時間制限: 3 s メモリ制限: 256 MB 満点: 100

#11349. 官渡之战

統計

公元 200 年,曹操与袁绍在官渡交战。官渡之战是一场大战,双方在 $M$ 个不同的战场上交战,战场编号为 $1$ 到 $M$。附近还有 $N$ 个村庄,编号为 $1$ 到 $N$。曹操可以从这些村庄征召士兵来增强他的军队。对于村庄 $i$,曹操只能征召士兵前往战场 $x_i$。然而,当时袁绍的势力非常强大,为了自保,村庄 $i$ 也会派遣同样数量的士兵前往战场 $y_i$ 加入袁绍的军队。如果曹操从村庄 $i$ 征召一名士兵,他需要为该村庄支付 $c_i$ 单位的钱。曹操不需要为加入袁绍军队的士兵支付任何费用。起初,每个战场上双方都没有士兵。

作为当时最伟大的战略家之一,曹操正在考虑如何击败袁绍。我们可以想象,各个战场有着不同的重要程度 $w_i$。对于一些重要程度 $w_i = 2$ 的战场,它们非常重要,因此曹操必须保证在这些战场上,他的士兵人数严格多于袁绍的士兵人数。对于一些重要程度 $w_i = 1$ 的战场,它们的重要性稍低,因此曹操必须保证在这些战场上,他的士兵人数大于或等于袁绍的士兵人数。其他重要程度 $w_i = 0$ 的战场则没有重要性,因此对这些战场上的士兵人数没有限制。现在,在这些条件下,你能帮曹操算出他为了赢得战场胜利所需要支付的最少金钱吗?

输入格式

输入的第一行包含测试用例的数量 $T(1 \le T \le 30)$。接下来是 $T$ 个测试用例。 每个测试用例的第一行包含两个整数 $N$ 和 $M(1 \le N, M \le 10^5)$。 第二行包含 $N$ 个以空格分隔的整数。第 $i$ 个整数 $x_i(1 \le x_i \le M)$ 表示曹操可以从村庄 $i$ 征召士兵前往战场 $x_i$。 第三行包含 $N$ 个以空格分隔的整数。第 $i$ 个整数 $y_i(1 \le y_i \le M)$ 表示如果曹操从村庄 $i$ 征召了若干士兵,则会有相同数量的士兵加入袁绍的军队并在战场 $y_i$ 作战。 下一行包含 $N$ 个以空格分隔的整数。第 $i$ 个整数 $c_i(0 \le c_i \le 10^5)$ 表示曹操为该村庄的每名士兵需要支付的金额。 最后一行包含 $M$ 个以空格分隔的整数。第 $i$ 个数字 $w_i(w_i \in \{0, 1, 2\})$ 表示第 $i$ 个战场的重要程度。

输出格式

对于每个测试用例,输出一行 “Case #x: y”,其中 $x$ 是测试用例编号(从 1 开始),$y$ 是曹操为了赢得所有战场胜利所需要支付的最少金钱。如果他无法获胜,则 $y = -1$。

样例

输入 1

2
2 3
2 3
1 1
1 1
0 1 2
1 1
1
1
1
2

输出 1

Case #1: 1
Case #2: -1

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.