【状压DP】——poj2288——最大权值哈密顿图

                                        Islands and Bridges

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 10454   Accepted: 2726

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


题意:

哈密尔顿路问题。n个点,每一个点有权值,设哈密尔顿路为 C1C2...Cn,Ci的权值为Vi,一条哈密尔顿路的值分为三部分计算:

1.每一个点的权值之和

2.对于图中的每一条CiCi+1,加上Vi*Vi+1

3.对于路径中的连续三个点:CiCi+1Ci+2,若在图中,三点的通路构成三角形,则要加上Vi*Vi+1*Vi+2

求一条汉密尔顿路可以获得的最大值,并且还要输出有多少条这样的哈密尔顿路。

 

思路:

取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 <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

#define maxn 10010

int dp[maxn][15][15];
long long num[maxn][15][15];
int mp[15][15];
int v[15];
int n,m;

int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&n,&m);
        for(int i = 0; i < n; i++)
            scanf("%d",&v[i]);

        memset(mp,0,sizeof(mp));
        memset(dp,-1,sizeof(dp));
        memset(num,0,sizeof(num));

        //初始化
        for(int i = 0; i < m; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            x--;
            y--;
            mp[x][y] = 1;
            mp[y][x] = 1;

            dp[(1<<x)|(1<<y)][x][y] = v[x] + v[y] + v[x] * v[y];
            num[(1<<x)|(1<<y)][x][y] = 1;

            dp[(1<<x)|(1<<y)][y][x] = v[x] + v[y] + v[x] * v[y];
            num[(1<<x)|(1<<y)][y][x] = 1;
        }

        if(n == 1)
        {
            printf("%d 1\n",v[0]);
            continue;
        }

        for(int s = 3; s < (1<<n); s++)
        {
            for(int i = 0; i < n; i++)
            {
                for(int j = 0; j < n; j++)
                {
                    for(int k = 0; k < n; k++)
                    {
                        if(i == j || j == k || i == k)//有重
                            continue;
                        if( !(s&(1<<i) && s&(1<<j) && s&(1<<k)) )//i,j,k中有不存在于s中的点
                            continue;
                        if(!mp[j][k] || dp[s-(1<<k)][i][j] == -1)//j k 之间没有路 || 终点为 i j 的哈密顿路不存在
                            continue;

                        int tmp = dp[s-(1<<k)][i][j] + v[k] + v[k] * v[j];

                        if(mp[i][k])//构成三角形
                            tmp += v[i] * v[j] * v[k];

                        if(tmp > dp[s][j][k])//得到更优解
                        {
                            dp[s][j][k] = tmp;
                            num[s][j][k] = num[s-(1<<k)][i][j];
                        }
                        else if(tmp == dp[s][j][k])//得到相同解
                            num[s][j][k] += num[s-(1<<k)][i][j];
                    }
                }
            }
        }

        int ans1 = 0;//哈密顿路最大值
        long long ans2 = 0;//最大哈密顿路条数

        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(i != j)
                {
                    if(dp[(1<<n)-1][i][j] > ans1)
                    {
                        ans1 = dp[(1<<n)-1][i][j];
                        ans2 = num[(1<<n)-1][i][j];
                    }
                    else if(dp[(1<<n)-1][i][j] == ans1)
                        ans2 += num[(1<<n)-1][i][j];
                }
            }
        }

        printf("%d %I64d\n",ans1,ans2/2);
    }
    return 0;
}

 

题后感:

这道题的状态有两个:最大权值 和 路径总数

由于要考虑前两个点,所以——

升一维以使描述更完备

由于起点不定,所以导致相同权值的哈密顿路会有很多情况,因此——

状态转移分类讨论,设计多个情况下的状态转移方程。

posted @ 2016-08-04 09:04  琥珀川||雨露晨曦  阅读(248)  评论(0)    收藏  举报