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 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
Source
Shanghai 2004
依旧是状态dp,不过因为相邻三个点形成三角形的时候会有加成,所以说要保存当前节点和前一个节点,因此开三维数组解决。
注意n==1的特判,还有int64.
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int val[15];
int map[15][15];
__int64 dp[(1<<14)+2][15][15];
__int64 num[(1<<14)+2][15][15];
__int64 sum,ret,ans;
int main()
{
int i,j,n,T,k,l,m,x,y,p,tag;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
sum=0;
for (i=0;i<n;i++)
{
scanf("%d",&val[i]);
sum+=val[i];
}
memset(map,0,sizeof(map));
for (i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
x--;
y--;
map[x][y]=1;
map[y][x]=1;
}
if (n==1)
{
printf("%d %d\n",val[0],1);
continue;
}
memset(dp,-1,sizeof(dp));
memset(num,0,sizeof(num));
dp[0][0][0]=0;
for (i=0;i<(1<<n);i++)
{
for (j=0;j<n;j++)
{
if (i==0)
{
for (k=0;k<n;k++)
{
for (l=0;l<n;l++)
{
if (k==l) continue;
if (map[k][l]==0) continue;
dp[(1<<k) | (1<<l)][l][k]=val[k]*val[l];
num[(1<<k) | (1<<l)][l][k]=map[k][l];
}
}
continue;
}
for (k=0;k<n;k++)
{
if (dp[i][j][k]==-1) continue;
// printf("%d...%d...%d..%d...%d..\n",i,j,k,dp[i][j][k],num[i][j][k]);
for (l=0;l<n;l++)
{
if ((i & (1<<l))!=0) continue;
p=(i | (1<<l));
if (map[j][l]==0) continue;
if (dp[p][l][j]==-1)
{
dp[p][l][j]=dp[i][j][k]+val[j]*val[l];
num[p][l][j]=num[i][j][k];
if (map[l][k]!=0) dp[p][l][j]+=val[j]*val[l]*val[k];
}
else
{
tag=dp[i][j][k]+val[j]*val[l];
if (map[l][k]!=0) tag+=val[j]*val[l]*val[k];
if (dp[p][l][j]==tag)
{
num[p][l][j]+=num[i][j][k];
}
if (dp[p][l][j]<tag)
{
num[p][l][j]=num[i][j][k];
dp[p][l][j]=tag;
}
}
}
}
}
}
ans=0;
ret=0;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if (ans<dp[(1<<n)-1][i][j])
{
ans=dp[(1<<n)-1][i][j];
ret=num[(1<<n)-1][i][j];
}
else if (ans==dp[(1<<n)-1][i][j])
{
ret+=num[(1<<n)-1][i][j];
}
}
}
if (ret==0) printf("0 0\n");
else printf("%I64d %I64d\n",ans+sum,ret/2);
}
return 0;
}