2288

/*
maybe DP can solve it

状态DP问题,计数

利用状态DP求最优hamilton回路

这个问题可以分成两问,一个求hamilton回路的最大值,还有一个是这种最大值的路径有几个,前者很明显的DP,后者是DP上的计数
*/

// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>

#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>

#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>

using namespace std;

// typedef
typedef __int64 LL;

// 
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)

#define Z(a,b) (a<<b)
#define Y(a,b) (a>>b)

const double eps = 1e-6;
const double INFf = 1e100;
const int INFi = 1000000000;
const LL INFll = (LL)1<<62;
const double Pi = acos(-1.0);

template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
	if(x>y) return x;
	return y;
}
template<class T> inline T TMIN(T x,T y)
{
	if(x<y) return x;
	return y;
}
template<class T> inline T MMAX(T x,T y,T z)
{
	return TMAX(TMAX(x,y),z);
}
template<class T> inline T MMIN(T x,T y,T z)
{
	return TMIN(TMIN(x,y),z);
}
template<class T> inline void SWAP(T &x,T &y)
{
	T t = x;
	x = y;
	y = t;
}


// code begin
int Cas,N,M;
int ver[15];
bool G[15][15];
int DP[1<<13][13][13]; //记录回路的最大值。为什么要构造这样的DP状态呢? 首先第一层,是状态,即有哪些点,但是我们不知道这些些点的顺序啊。第二层和第三层是倒数第二和倒数第一访问的点,两点决定一个方向,这样的构造可以唯一的确定一个 hamilton回路,这样的设计很巧妙,佩服
LL DP2[1<<13][13][13]; //状态的设计和上一样,是来记录最大值出现了几次

inline int count(int n)
{
	int ans = 0;
	while(n)
	{
		ans+=n&1;
		n=Y(n,1);
	}
	return ans;
}

int main()
{
	read;
	write;
	int a,b;
	scanf("%d",&Cas);
	while(Cas--)
	{
		scanf("%d %d",&N,&M);
		for(int i=0;i<N;i++)
		{
			scanf("%d",ver+i);
		}
		memset(G,0,sizeof(G));
		memset(DP,0,sizeof(DP));
		memset(DP2,0,sizeof(DP2));
		//初始时,每条边都是hamilton回路的开始
		while(M--)
		{
			scanf("%d %d",&a,&b);
			--a;--b;
			G[a][b] = G[b][a] = true;
			DP[Z(1,a)+Z(1,b)][a][b] = ver[a]+ver[b]+ver[a]*ver[b];
			DP2[Z(1,a)+Z(1,b)][a][b] = 1;
			DP[Z(1,b)+Z(1,a)][b][a] = ver[a]+ver[b]+ver[a]*ver[b];
			DP2[Z(1,b)+Z(1,a)][b][a] = 1;
		}
		if(N==1)
		{
			printf("%d 1\n",ver[0]);
			continue;
		}
		//printf("%d\n",Z(1,1) );
		
		//
		int tmp;
		for(int i=2;i<N;i++) //在循环N-2次就行了,因为总共N-1条边,已经添加了一个了
		{
			//printf("%d\n",i);
			for(int j=(Z(1,N))-Z(1,N-i);j>=Z(1,i)-1;j--)
			{
				if(count(j)==i) for(int k=0;k<N;k++)
				{
					if(Z(1,k)&j) for(int p=0;p<N;p++) if(p!=k)
					{
						//DP[j][k][p];
						if(Z(1,p)&j) if(DP[j][k][p]) for(int q=0;q<N;q++)
						{
							if( (j&Z(1,q))==0 && G[p][q]  )
							{
								tmp = DP[j][k][p] + ver[q] + ver[p]*ver[q] + (G[k][q]?(ver[k]*ver[p]*ver[q]):0);
								//printf(" %d %d %d %d %d\n",k,p,q,tmp,DP2[j][k][p]);
								if(tmp>DP[j|Z(1,q)][p][q])
								{
									DP[j|Z(1,q)][p][q] = tmp;
									DP2[j|Z(1,q)][p][q] = DP2[j][k][p];
								}
								else if(tmp==DP[j|Z(1,q)][p][q])
								{
									DP2[j|Z(1,q)][p][q] += DP2[j][k][p];
								}
							}
						}
					}
				}
			}

		}

		//
		LL ans[2]={0,0};
		for(int i=0;i<N;i++)
		{
			for(int j=0;j<N;j++) if(j!=i)
			{
				//printf("%d %d %d %lld\n",i,j,DP[Z(1,N)-1][i][j],DP2[Z(1,N)-1][i][j]);
				if(DP[Z(1,N)-1][i][j]>ans[0])
				{
					ans[0] = DP[Z(1,N)-1][i][j];
					ans[1] = DP2[Z(1,N)-1][i][j];
					//printf("%d\n",ans[1]);
				}
				else if(DP[Z(1,N)-1][i][j]==ans[0])
				{
					ans[1] += DP2[Z(1,N)-1][i][j];
				}
			}
		}

		printf("%I64d %I64d\n",ans[0],ans[1]/2);
	}
	return 0;
}

posted @ 2011-04-15 21:00  AC2012  阅读(1285)  评论(0编辑  收藏  举报