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

该博客介绍了如何运用动态规划算法解决PKU2288题目的旅行商问题(TSP),探讨了输入输出格式以及示例分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 CiC i+1 in the path, we add the product Vi*V i+1. And for the third part, whenever three consecutive islands CiC i+1C i+2 in the path forms a triangle in the map, i.e. there is a bridge between Ci and C i+2, we add the product Vi*V i+1*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.

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:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值