[動態規劃,tsp問題]pku2288 Islands and Bridges

Islands and Bridges
Time Limit: 4000MS   Memory Limit: 65536K
Total 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

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

Sample Output

22 3
69 1

Source

分析:經典的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:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章