http://poj.org/problem?id=2288

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

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

 

题意:求按照3个部分之和计算的哈密顿路径(经过所有的点一次的路径)的最大路径权和及该权和的路径条数。

3部分分别是:所有点的v(i),路径相邻点的v(i)*v(i+1),如果i与i+2有边相连再加上v(i)*v(i+1)*v(i+2)。

思路:每个点会受到前面两个点的影响,所以设f(i,j,s):当前在i点,上一个点在j,当前已访问的点集。cnt()表示这个状态该权和的数量。还是裸的TSP转移方式。

我初始化的方式是:加一个虚拟点,权值为0,与每个单点相连,初始化f(i,虚拟点,1<<i)=v(i),cnt(i,虚拟点,1<<i)=1。

这坑B题有两个陷阱:①只有1个点,我跟ac代码对拍出来的。②题目有说:相反的顺序走的同一条路径算一条,而非两条。

我看那个ac代码的初始化方式是:枚举i,j,f(i,j,1<<i+1<<j)...,不用加虚拟点。这个比较自然。

还有,cnt数组要开longlong,原因:13!

#include<cstdio>
#include<iostream>
#include<cstring> 
using namespace std;
#define ll long long

int q,n,m,val[15],f[14][14][1<<14];
ll cnt[14][14][1<<14];
bool link[15][15];

void dp()
{
	memset(f,-1,sizeof(f));
	memset(cnt,0,sizeof(cnt));
	for(int i=0;i<n;i++)f[i][n][1<<i]=val[i],cnt[i][n][1<<i]=1;
	for(int s=0;s<(1<<n);s++)
	{									//w->u->v
		for(int v=0;v<n;v++)if(s&(1<<v))
		{
			for(int u=0;u<n;u++)if((s&(1<<u))&&link[u][v])
			{
				for(int w=0;w<=n;w++)if(((s&(1<<w))||w==n)&&link[w][u])
				{
					if(f[u][w][s-(1<<v)]==-1)continue;
					int sum=f[u][w][s-(1<<v)]+val[v]+val[u]*val[v]+(link[w][v]?val[w]*val[u]*val[v]:0);
					if(sum==f[v][u][s])cnt[v][u][s]+=cnt[u][w][s-(1<<v)];
					else if(sum>f[v][u][s])f[v][u][s]=sum,cnt[v][u][s]=cnt[u][w][s-(1<<v)];
				}				
			}
		}
	}
	ll maxx=0,cntt=0;
	for(int i=0;i<n;i++)for(int j=0;j<n;j++)if(f[i][j][(1<<n)-1]>maxx)maxx=f[i][j][(1<<n)-1];
	for(int i=0;i<n;i++)for(int j=0;j<n;j++)if(f[i][j][(1<<n)-1]==maxx)cntt+=cnt[i][j][(1<<n)-1];
	printf("%lld %lld\n",maxx,cntt/2);
}

int main()
{		
	//freopen("input.in","r",stdin);
	cin>>q;
	int x,y;
	while(q--)
	{	
		cin>>n>>m;
		memset(link,0,sizeof(link));
		memset(val,0,sizeof(val));	
		for(int i=0;i<n;i++)cin>>val[i];
		while(m--)
		{
			cin>>x>>y;x--;y--;
			link[x][y]=1;
			link[y][x]=1;
		}
		for(int i=0;i<n;i++)link[n][i]=1;
		if(n>1)dp();
		else printf("%d %d\n",val[0],1);
	}
	return 0;
}