首页 > 代码库 > POJ2288:Islands and Bridges(状态压缩)

POJ2288:Islands and Bridges(状态压缩)

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

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

Sample Output

22 369 1
 
这道题因为括号的问题纠察了好久,主要还是因为运算符的优先级问题。。。
 
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int n,m;int edge[15][15];__int64 dp[1<<14][15][15],way[1<<14][15][15],tem,v[15];int main(){    int t,i,j,k,x,y,s;    scanf("%d",&t);    while(t--)    {        memset(edge,0,sizeof(edge));        scanf("%d%d",&n,&m);        for(i = 1; i<=n; i++)            scanf("%I64d",&v[i]);        if(n == 1)        {            printf("%I64d 1\n",v[1]);            continue;        }        memset(way,0,sizeof(way));        memset(dp,-1,sizeof(dp));        for(i = 0; i<m; i++)        {            scanf("%d%d",&x,&y);            edge[x][y] = edge[y][x] = 1;        }        for(i = 1; i<=n; i++)//所有两两相连的路dp储存价值,way储存路数        {            for(j = 1; j<=n; j++)            {                if(i == j || !edge[i][j]) continue;                int ii = 1<<(i-1);                int jj = 1<<(j-1);                tem = v[i]+v[j]+v[i]*v[j];                dp[ii+jj][i][j] = tem;                way[ii+jj][i][j] = 1;            }        }        for(s = 0; s<(1<<n); s++)//枚举状态        {            for(i = 1; i<=n; i++)            {                if((s&(1<<(i-1))) == 0) continue;                for(j = 1; j<=n; j++)                {                    if((s&(1<<(j-1))) == 0 || i == j || !edge[i][j]) continue;                    for(k = 1; k<=n; k++)                    {                        if((s&(1<<(k-1))) == 0 || j == k || i == k ) continue;                        int news = s - (1<<(i-1));//找出i不在状态内但是j,k在状态内的状况                        if(dp[news][j][k]==-1) continue;                        if(!edge[j][k]) continue;                        tem = v[i]+v[j]*v[i]+dp[news][j][k];                        if(edge[i][k])//如果i与k相连,加上其价值                            tem+=v[i]*v[j]*v[k];                        if(tem>dp[s][i][j])//tem大于dp的话,更新dp同时更新way                        {                            dp[s][i][j] = tem;                            way[s][i][j] = way[news][j][k];                        }                        else if(tem == dp[s][i][j])//dp相等,则加上路数                            way[s][i][j] += way[news][j][k];                    }                }            }        }        __int64 ans = -1,sum = 0;        int p = (1<<n)-1;        for(i = 1; i<=n; i++)        {            for(j = 1; j<=n; j++)            {                if(i == j) continue;                if(ans < dp[p][i][j])                {                    ans = dp[p][i][j];                    sum = way[p][i][j];                }                else if(ans == dp[p][i][j])                    sum += way[p][i][j];            }        }        if(ans == -1)            printf("0 0\n");        else            printf("%I64d %I64d\n",ans,sum/2);    }    return 0;}