poj 2288 islands and Bridges(汉密尔顿环,状压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 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

题意:

找到权值最大的哈密顿圈,权值为路径上每个地点的价值,加上相邻两个点的价值的积,加上路径上能形成三角形的三个价值的积。

思路:状压dp。dp[s][i][j]表示在s状态下,最后两个地点是i和j的最大权值。way[s][i][j]表示其路径数量,且因为ij和ji是一样的,所以最后结果除以二。注意特殊判断只有一个点的情况。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
int t,n,m,v[20],map[20][20],ans;
int f[10005][15][15];
long long s[10005][15][15],res;

using namespace std;
int main()
{

scanf("%d",&t);
while(t--)
{
    memset(map,0,sizeof(map));
    memset(f,-1,sizeof(f));
    memset(s,0,sizeof(s));
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    scanf("%d",&v[i]);
     if(n==1)
    {
        printf("%d 1\n",v[0]);
        continue;
    }
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        x--;
        y--;
        map[x][y]=map[y][x]=1;
    }
    int end=(1<<n)-1;

    for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
    if(map[i][j])
    {
        f[(1<<i)|(1<<j)][i][j]=v[i]+v[j]+v[i]*v[j];
        s[(1<<i)|(1<<j)][i][j]=1;
    }
            for(int i=0;i<=end;i++)
                for(int j=0;j<n;j++)
                if((i&(1<<j))>0)
                for(int k=0;k<n;k++)
                if((i&(1<<k))>0&&map[j][k]&&f[i][j][k]>=0)

                for(int g=0;g<n;g++)
                if(map[g][j]&&(i&(1<<g))==0)
                {
                    int w=v[g]+v[g]*v[j];
                    if(map[g][k])
                    w+=v[g]*v[j]*v[k];
                    if(f[i|(1<<g)][g][j]==f[i][j][k]+w)
                    s[i|(1<<g)][g][j]+=s[i][j][k];
                    if(f[i|(1<<g)][g][j]<f[i][j][k]+w)
                    {
                        f[i|(1<<g)][g][j]=f[i][j][k]+w;
                        s[i|(1<<g)][g][j]=s[i][j][k];
                    }


                }
                ans=res=0;
                for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                {
                    if(ans==f[end][i][j])
                    res+=s[end][i][j];
                    if(ans<f[end][i][j])
                    {
                        ans=f[end][i][j];
                        res=s[end][i][j];
                    }
                }
                printf("%d %lld\n",ans,res/2);


}
return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/82939157