Islands and Bridges POJ - 2288(三维状压dp)

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 ViV 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 ViV 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

题意:
对于哈密顿路径c1c2c3c4…,价值分三种
1.节点权值和
2.相邻节点权值乘积v1v2,v2v3…
3.三角形权值积:如果c1->c2,并且c1->c3,那么加上权值v1v2v3。
求所得哈密顿最大价值,以及对应的路径数。

思路:
类似普通的哈密顿或者TSP问题。定义状态为DP(S,X),S集和代表走过多少个点,当前在S点。
炮兵阵地那道题目中,每个炮台会攻击到之前两行,因此需要枚举之前两行的状态。
本题一样,一个点会与之后两个点发生作用,所以一共需要枚举三个点,并记录两个点的状态。

dp(s,i,j) 状态集和为s, 从i走向j。枚举第三个点k
再转移到 dp(s | (1 << k, j , k) + val(k) + val(j) * val(k)。
如果组成三角形,还要加上val(i) X val(k) X val(j)。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;
ll dp[1 << 13][14][14];
ll val[14];
ll way[1 << 13][14][14],link[14][14];

void init()
{
    memset(dp,-1,sizeof(dp));
    memset(way,0,sizeof(way));
    memset(link,0,sizeof(link));
}

int main()
{
    ll T;scanf("%lld",&T);
    while(T--)
    {
        init();
        ll n,m;scanf("%lld%lld",&n,&m);
        for(ll i = 1;i <= n;i++) scanf("%lld",&val[i]);
        for(ll i = 1;i <= m;i++)
        {
            ll x,y;scanf("%lld%lld",&x,&y);
            dp[(1 << (x - 1)) | (1 << (y - 1))][x][y] = dp[1 << (x - 1) | (1 << (y - 1))][y][x] = val[x] + val[y] + val[x] * val[y];
            way[(1 << (x - 1)) | (1 << (y - 1))][x][y] = way[(1 << (x - 1)) | (1 << (y - 1))][y][x] = 1;
            link[x][y] = link[y][x] = 1;
        }
        
        if(n == 1)
        {
            printf("%lld %d\n",val[1],1);
            continue;
        }
        
        for(ll s = 0;s < (1 << n);s++)
        {
            for(ll i = 1;i <= n;i++)//第一个点
            {
                if(!(s & (1 << (i - 1))))continue;
                for(ll j = 1;j <= n;j++)//第二个点
                {
                    if(!(s & (1 << (j - 1))))continue;
                    if(i == j)continue;
                    if(dp[s][i][j] == -1)continue;
                    if(!link[i][j])continue;//不用这句也可。因为状态合理就表明i和j肯定连了边。
                    
                    for(ll k = 1;k <= n;k++)
                    {
                        if(!link[j][k])continue;
                        if(s & (1 << (k - 1)))continue;
                        
                        ll news = s | (1 << (k - 1));
                        ll tmp = dp[s][i][j] + val[k] + val[j] * val[k];
                        if(link[i][k])tmp += val[i] * val[j] * val[k];
                        
                        if(tmp == dp[news][j][k])
                            way[news][j][k] += way[s][i][j];
                        else if(tmp > dp[news][j][k])
                        {
                            way[news][j][k] = way[s][i][j];
                            dp[news][j][k] = tmp;
                        }
                    }
                }
            }
        }
        
        ll maxv = -1;
        ll maxn = 0,s = (1 << n) - 1;
        for(ll i = 1;i <= n;i++)
        {
            for(ll j = 1;j <= n;j++)
            {
                if(!link[i][j])continue;
                if(dp[s][i][j] > maxv)
                {
                    maxv = dp[s][i][j];
                    maxn = way[s][i][j];
                }
                else if(dp[s][i][j] == maxv)
                {
                    maxn += way[s][i][j];
                }
            }
        }
        
        if(maxv == -1)
        {
            printf("0 0\n");
        }
        else printf("%lld %lld\n",maxv,maxn / 2);//因为逆序哈密顿路径算一种,所以除以2。
    }
    return 0;
}

发布了628 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/103994218