博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2288 Islands and Bridges (状压DP)
阅读量:6558 次
发布时间:2019-06-24

本文共 3267 字,大约阅读时间需要 10 分钟。

Islands and Bridges
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 7729   Accepted: 1977

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

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

Sample Output

22 369 1

Source

 
 
这是一道典型的利用状态压缩DP求最优Hamilton回路的题目。
取dp[state][i][j]表示state状态下倒数第二个岛为i,最后一个岛为j时的最优解,num[state][i][j]为相应的路径数目,其中state的二进制表示的i位为1表示岛i被访问过,反之为0。
则显然当有边(i,j)存在时,有如下初值可赋:
dp[(1<<i)+(1<<j)][i][j]=val[i]+val[j]+val[i]*val[j],num[(1<<i)+(1<<j)][i][j]=1。
如果状态(state,i,j)可达,检查岛k,如果此时k没有被访问过并且有边(j,k)存在,则做如下操作:
1)设tmp为下一步访问岛k时获得的总利益,r=state+(1<<k)。
2)如果t,p>dps[r][j][k],表示此时可以更新到更优解,则更新 :
    dp[r][j][k]=q,num[r][j][k]=num[state][i][j]。
3)如果tmp==dp[r][j][k],表示此时可以获得达到局部最优解的更多方式,则更新:
    num[r][j][k]+=num[p][i][j]。
最后检查所有的状态((1<<n)-1,i,j),叠加可以得到最优解的道路数。
需要注意的是,题目约定一条路径的两种行走方式算作一种,所以最终结果要除2。
 
 
 
#include
#include
#include
using namespace std;int n,m;int val[15],map[13][13];int dp[1<<13][13][13]; //dp[state][i][j]表示state状态下倒数第二个岛为i,最后一个岛为j时的最优解long long num[1<<13][13][13]; //num[state][i][j]为相应的路径数目int main(){ //freopen("input.txt","r",stdin); int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(int i=0;i

 

转载地址:http://rlqco.baihongyu.com/

你可能感兴趣的文章
iscsi网络存储
查看>>
团队随笔
查看>>
Java内存块说明
查看>>
List集合具体对象的特点
查看>>
网络信息安全之防火墙***检测方法 (五)
查看>>
怎样为用户写“招标书”
查看>>
1.7 文件目录管理及相关的命令使用方法
查看>>
实际案例告诉你大数据在农业中如何应用
查看>>
LAMP优化策略
查看>>
PDF中添加页面/合并 PDF 内容
查看>>
JS仿FLASH特效可跳转回首页的CSS二级联动菜单
查看>>
页面导入样式时,使用link和@import有什么区别?
查看>>
类成员与类的实例成员
查看>>
Spark源码编译并在YARN上运行WordCount实例
查看>>
Spring AOP + AspectJ annotation example
查看>>
Spring VS EJB 3 的若干认识误区(转)
查看>>
React.js初探(一)
查看>>
Neo4j CQL -(17)- NULL值
查看>>
BZOJ4554: [Tjoi2016&Heoi2016]游戏 luoguP2825 loj2057
查看>>
json_encode后的中文不编码成unicode
查看>>