[动态规划,tsp问题]pku2288 Islands and Bridges

来源:互联网 发布:淘宝发货地有影响吗 编辑:程序博客网 时间:2024/05/18 02:35
Islands and Bridges
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 4173 Accepted: 1008

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 C1C2...Cn is calculated as the sum of three parts. Let Vi be the value for the island Ci. 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+1 in the path, we add the product Vi*Vi+1. And for the third part, whenever three consecutive islands CiCi+1Ci+2 in the path forms a triangle in the map, i.e. there is a bridge between Ci and Ci+2, we add the product Vi*Vi+1*Vi+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.

Input

The input file starts with a number q (q<=20) on the first line, which is the number of test cases. Each test case starts with a line with two integers n and m, which are the number of islands and the number of bridges in the map, respectively. The next line contains n positive integers, the i-th number being the Vi value of island i. Each value is no more than 100. The following m lines are in the form x y, which indicates there is a (two way) bridge between island x and island y. Islands are numbered from 1 to n. You may assume there will be no more than 13 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.

Sample Input

23 32 2 21 22 33 14 61 2 3 41 21 31 42 32 43 4

Sample Output

22 369 1

Source

Shanghai 2004
分析:经典的TSP问题的变形。因为节点数比较少,最多只有13个,所以可以用状态压缩动态规划来做。令f[i,j,k]表示状态压缩后为i,(即i转化为二进制后,第x位上位1表示点x走过,否则没走过),倒数第二个经过的点为j,最后一个经过的点为k的最大得分。
初值:f[1 shl (i-1)+1 shl (j-1),i,j]:=d[i]+d[j]+d[i]*d[j](i、j之间有边)。
状态转移方程:f[i,j,k]=max(f[i',j',j]+point)(i-i'=1 shl (k-1),状态i‘没走过k这个点,j和k相连,point为这样走的得分,根据题目描述可算得)。
路径条数运用加法原理计算就行了。因为1->2和2->1是一样的,所以最后答案要除以2.
wa原因集锦:
1、没有考虑到n=1的情况,路径总条数应该为1.
2、没有看题,直接看蹩脚的翻译,结果理解错题意,point计算错误。
3、交错程序...= =
注意要用int64.
codes:
原创粉丝点击