pku 2288 Islands and Bridges(状态压缩DP)

来源:互联网 发布:机器人模拟软件 编辑:程序博客网 时间:2024/06/11 12:06

 

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+1 Ci+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.

 

 

[state][i][j],state表示已经到达过的顶点,i记录倒数第2个顶点,j记录倒数第一个顶点。

DP[][][]记录最大值,way[][][]记录最大值对应的路径数。

 

做这题,才注意到位运算的<<>>左移右移优先级好低,不如+-加减,得加括号。

 

要注意的地方有两个

1.一个点的情况

2.用__int64

 

我的代码用的是最直观的DP,跑了2500ms。

假设state1可以由state2转化得到,通过分析可以发现0<=state2<state1。于是可以在最外层循环用state递增。

用记忆化搜索重新写了一遍。本以为可以很快搞定,但WA了差不多10次。__int64,血的教训。

 

 

原创粉丝点击