2013
12-21

# Islands and Bridges

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.

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.

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.

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

22 3
69 1

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<bitset>

using namespace std;

#define INF 0xFFFFFF

int n,m;
long long  dp[1<<14][14][14];
long long num[1<<14][14][14];
int maps[14][14];
int v[14];

bool jud(int s,int i)
{
if(i==0) return true;
if(s&(1<<(i-1))) return true;
return false;
}

void solve()
{
int x,y,tmps,tmp;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&v[i]);
memset(maps,0,sizeof(maps));
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
maps[x][y]=maps[y][x]=1;
}
if(n==1)
{
cout<<v[1]<<" "<<1<<endl;
return;
}
memset(dp,-1,sizeof(dp));
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
{
int j=0;
j|=(1<<(i-1));
dp[j][i][0]=v[i];
num[j][i][0]=1;
}
for(int i=0;i<(1<<n);i++)
{
for(int j=1;j<=n;j++)
{
if(!jud(i,j)) continue;
tmps=i^(1<<(j-1));
for(int k=1;k<=n;k++)
{
if(k==j) continue;
if(!jud(i,k)) continue;
if(!maps[k][j]) continue;
for(int q=0;q<=n;q++)
{
if(q==k || q==j) continue;
if(!maps[q][k] && q) continue;
if(!jud(tmps,q)) continue;
if(dp[tmps][k][q]==-1) continue;
tmp=v[j]+v[j]*v[k]+dp[tmps][k][q];
if(maps[q][j]) tmp+=v[q]*v[j]*v[k];
if(tmp==dp[i][j][k])
num[i][j][k]+=num[tmps][k][q];
if(tmp>dp[i][j][k])
{
dp[i][j][k]=tmp;
num[i][j][k]=num[tmps][k][q];
}
}
}
}
}
long long ans1=-1,ans2=0;
for(int s=(1<<n)-1,i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j) continue;
if(ans1==dp[s][i][j])
ans2+=num[s][i][j];
if(dp[s][i][j]>ans1)
{
ans1=dp[s][i][j];
ans2=num[s][i][j];
}
}
}
if(n==1)
{
cout<<ans1<<" "<<1<<endl;
return;
}
if(ans1==-1) printf("0 0\n");
else
cout<<ans1<<" "<<ans2/2<<endl;
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
solve();
return 0;
}`