CF1038E.Maximum Matching

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given nn blocks, each of them is of the form [color1_1|value|color2_2], where the block can also be flipped to get [color2_2|value|color1_1].

A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C.

The value of the sequence is defined as the sum of the values of the blocks in this sequence.

Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence.

给你 nn 个方块,每个方块的形式为 [color1_1|value|color2_2],其中该方块也可以被翻转,从而变为 [color2_2|value|color1_1]。

若一个方块序列中,相邻方块相接触的端点具有相同的颜色,则称该序列为合法序列。例如,三个方块 A、B 和 C 构成的序列是合法的,当且仅当 B 的左端颜色与 A 的右端颜色相同,且 B 的右端颜色与 C 的左端颜色相同。

该序列的值定义为序列中所有方块的 value 之和。

请从给定的方块集合中选出一个子集(可重排顺序,也可对其中任意方块进行翻转),构造出一个合法序列,使得其值最大。每个方块在序列中至多使用一次。

输入格式

The first line of input contains a single integer nn (1≤n≤1001 \le n \le 100) — the number of given blocks.

Each of the following nn lines describes corresponding block and consists of color1,i\mathrm{color}_{1,i}, valuei\mathrm{value}_i and color2,i\mathrm{color}_{2,i} (1≤color1,i,color2,i≤41 \le \mathrm{color}_{1,i}, \mathrm{color}_{2,i} \le 4, 1≤valuei≤100 0001 \le \mathrm{value}_i \le 100\,000).

输入的第一行包含一个整数 nn(1≤n≤1001 \le n \le 100)—— 表示给定的方块数量。

接下来的 nn 行,每行描述一个对应的方块,包含 color1,i\mathrm{color}_{1,i}、valuei\mathrm{value}_i 和 color2,i\mathrm{color}_{2,i}(其中 1≤color1,i,color2,i≤41 \le \mathrm{color}_{1,i}, \mathrm{color}_{2,i} \le 4,1≤valuei≤100 0001 \le \mathrm{value}_i \le 100\,000)。

输出格式

Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence.

输出一个整数——即构成有效序列的方块子集的最大总价值。

输入输出样例

  • 输入#1

    6
    2 1 4
    1 2 4
    3 4 4
    2 8 3
    3 16 3
    1 32 2

    输出#1

    63
  • 输入#2

    7
    1 100000 1
    1 100000 2
    1 100000 2
    4 50000 3
    3 50000 4
    4 50000 4
    3 50000 3

    输出#2

    300000
  • 输入#3

    4
    1 1000 1
    2 500 2
    3 250 3
    4 125 4

    输出#3

    1000

说明/提示

In the first example, it is possible to form a valid sequence from all blocks.

One of the valid sequences is the following:

[4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2]

The first block from the input ([2|1|4] →\to [4|1|2]) and second ([1|2|4] →\to [4|2|1]) are flipped.

In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped):

[2|100000|1] [1|100000|1] [1|100000|2]

In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.

在第一个例子中,可以使用所有方块构成一个有效的序列。

其中一个有效序列如下:

[4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2]

输入中的第一个方块([2|1|4] →\to [4|1|2])和第二个方块([1|2|4] →\to [4|2|1])被翻转了。

在第二个例子中,最优解可由输入的前三个方块构成,如下所示(输入中的第二个或第三个方块被翻转):

[2|100000|1] [1|100000|1] [1|100000|2]

在第三个例子中,无法构成包含两个或更多方块的有效序列,因此答案仅为仅含第一个方块的序列,因为它是所有方块中数值最大的那个。

输入解题思路,AI测评打分。不知道怎么写?

首页