#P10963. Islands and Bridges

Islands and Bridges

题目描述

Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the bridges such that it visits each island exactly once. On our map, there is also a positive integer value associated with each island. We call a Hamilton path the best triangular Hamilton path if it maximizes the value described below.

Suppose there are nn islands. The value of a Hamilton path C1C2CnC_1 C_2 \dots C_n is calculated as the sum of three parts. Let Vi be the value for the island CiC_i. As the first part, we sum over all the Vi values for each island in the path. For the second part, for each edge CiCi+1C_i C_{i+1} in the path, we add the product Vi×Vi+1V_i \times V_{i+1}. And for the third part, whenever three consecutive islands Ci,Ci+1,Ci+2C_i,C_{i+1},C_{i+2} in the path forms a triangle in the map, i.e. there is a bridge between CiC_i and Ci+2C_{i+2}, we add the product Vi×Vi+1×Vi+2V_i \times V_{i+1} \times V_{i+2}.

Most likely but not necessarily, the best triangular Hamilton path you are going to find contains many triangles. It is quite possible that there might be more than one best triangular Hamilton paths; your second task is to find the number of such paths.

输入格式

The input file starts with a number q(q20) q (q \le 20) on the first line, which is the number of test cases. Each test case starts with a line with two integers nn and mm, which are the number of islands and the number of bridges in the map, respectively. The next line contains nn positive integers, the ii-th number being the ViV_i value of island ii. Each value is no more than 100100. The following mm lines are in the form x yx\ y, which indicates there is a (two way) bridge between island xx and island yy. Islands are numbered from 11 to nn. You may assume there will be no more than 1313 islands.

输出格式

For each test case, output a line with two numbers, separated by a space. The first number is the maximum value of a best triangular Hamilton path; the second number should be the number of different best triangular Hamilton paths. If the test case does not contain a Hamilton path, the output must be 0 00\ 0.

Note: A path may be written down in the reversed order. We still think it is the same path.

题目大意

题目描述

给定一张由岛屿和连接这些岛屿的桥梁构成的地图,众所周知,哈密顿路径是沿着桥梁的路径,能够恰好访问每个岛屿一次。在我们的地图中,每个岛屿还关联一个正整数值。我们定义一种哈密顿路径为 最佳三角形哈密顿路径,其最大化以下描述的值。

假设有 nn 个岛屿。哈密顿路径 C1,C2,,CnC_1,C_2,\dots,C_n 的值分为三部分计算。设 ViV_i 为岛屿 CiC_i 的值。第一部分为路径中每个岛屿的 ViV_i 值的总和。第二部分,对于路径中的每条边 CiCi+1C_i C_{i+1},加上 Vi×Vi+1V_i \times V_{i+1} 的积。第三部分,对于路径中的每三个连续岛屿 Ci,Ci+1,Ci+2C_i, C_{i+1}, C_{i+2},如果它们在地图中形成一个三角形(即 CiC_iCi+2C_{i+2} 之间有桥),加上 Vi×Vi+1×Vi+2V_i \times V_{i+1} \times V_{i+2} 的积。

最佳三角形哈密顿路径很可能(但不一定)包含多个三角形。可能会存在多个最佳三角形哈密顿路径。你的第二个任务是找出这样的路径的数量。


输入格式

输入文件的第一行是一个整数 q (q20)q\ (q \le 20),表示测试用例的数量。

每个测试用例的第一行有两个整数 nnmm,分别表示地图中岛屿的数量和桥梁的数量。

接下来的第一行包含 nn 个正整数,第 ii 个数是岛屿 iiViV_i 值。每个值不超过 100100

接下来的 mm 行,每行的格式为 x yx\ y,表示岛屿 xx 和岛屿 yy 之间有一座双向桥。岛屿从 11nn 编号。可以假设岛屿总数不超过 1313


输出格式

对于每个测试用例,输出一行,包含两个用空格分隔的数。第一个数是最佳三角形哈密顿路径的最大值;第二个数是不同最佳三角形哈密顿路径的数量。如果测试用例中不存在哈密顿路径,则输出 0 00\ 0

注意:如果一条路径的顺序反转,仍认为它是相同的路径。


2
3 3
2 2 2
1 2
2 3
3 1 
4 6
1 2 3 4
1 2
1 3
1 4
2 3
24
34
22 3
69 1