#648. Islands and Bridges
Islands and Bridges
Description
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 n islands. The value of a Hamilton path is calculated as the sum of three parts. Let be the value for the island . As the first part, we sum over all the values for each island in the path. For the second part, for each edge in the path, we add the product . And for the third part, whenever three consecutive islands in the path forms a triangle in the map, i.e. there is a bridge between and , we add the product .
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.
Input
The input file starts with a number on the first line, which is the number of test cases. Each test case starts with a line with two integers and , which are the number of islands and the number of bridges in the map, respectively. The next line contains positive integers, the -th number being the value of island . Each value is no more than . The following lines are in the form x y
, which indicates there is (two way) bridge between island and island . Islands are numbered from to . You may assume there will be no more than islands.
Output
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 0
.
Note: A path may be written down in the reversed order. We still think it is the same path.
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
2 4
3 4
22 3
69 1
Constraints