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+1i+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

二进制表示点的到达状态。

状态压缩求哈密顿回路,基本思路如下:

F[i][j] (0<=i<2^n,0<=j<n) 表示所有点的访问状态为i并且目前处于点j时的最短路径。
在i的二进制表示下,第k(0<=k<n)位为1表示已经访问过点k。
F[0][0]=0,Others=+∞,求F[2^n-1][n-1]。
F[i][j]=Min{F[i^1<<k][k]+w(k,j) | 0<=k<n-1且(i>>k&1)=1}

在本题中由于要考虑“三角形”关系,故须开三维,f[到达状态][上一个到达的点][本次到达的点]=最优解

同时要统计方案数,由于方案可能很多,需要开LL

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int f[<<][][];
long long num[<<][][];
int mp[][];
int v[];
int n,m;
int main(){
int Q;
scanf("%d",&Q);
while(Q--){
memset(f,-,sizeof(f));
memset(num,,sizeof(num));
memset(mp,,sizeof(mp));
scanf("%d%d",&n,&m);
int i,j,k,s;
for(i=;i<n;i++){
scanf("%d",&v[i]);
}
int x,y;
for(i=;i<=m;i++){
scanf("%d%d",&x,&y);
x--;y--;
mp[x][y]=mp[y][x]=;
}
if(n==){//单点特判
printf("%d 1\n",v[]);
continue;
}
for(i=;i<n;i++)//边界预处理
for(j=;j<n;j++)
if(i!=j && mp[i][j]){
f[(<<i)|(<<j)][i][j]=v[i]+v[j]+v[i]*v[j];
num[(<<i)|(<<j)][i][j]=;
}
for(i=;i<(<<n);i++)//连通状况
for(j=;j<n;j++)//枚举各岛
if((i&(<<j)))
for(k=;k<n;k++)
if(mp[j][k] && j!=k)
if((i&(<<k)) && f[i][j][k]!=-)//j和k枚举的岛都在i枚举范围内,且有上一个状态
for(s=;s<n;s++){
if(mp[k][s] && k!=s && !(i&(<<s)))
//k到s联通 s之前没走过
{
int val=f[i][j][k]+v[s]+v[k]*v[s];
if(mp[j][s])val+=v[j]*v[k]*v[s];//三角形特判
if(f[i|(<<s)][k][s]<val){//更新状态
f[i|(<<s)][k][s]=val;
num[i|(<<s)][k][s]=num[i][j][k];
}else if(f[i|(<<s)][k][s]==val)
num[i|(<<s)][k][s]+=num[i][j][k];
}
}
int ans=;
long long ansnum=;//数据很大!
for(j=;j<n;j++)
for(k=;k<n;k++){
if(k!=j && mp[j][k]){
s=(<<n)-;
if(ans<f[s][j][k]){
ans=f[s][j][k];
ansnum=num[s][j][k];
}
else if(ans==f[s][j][k])//解相同则累加方案数
ansnum+=num[s][j][k];
}
}
printf("%d %lld\n",ans,ansnum/);
}
return ;
}

POJ2288 Islands and Bridges的更多相关文章

  1. [poj2288] Islands and Bridges (状压dp)

    Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...

  2. POJ2288 Islands and Bridges(TSP:状压DP)

    求一个图的哈密顿路径的最大权及其路径数.显然状态压缩+DP. dp[v][u][S] 表示从v走到当前顶点 u且走过的顶点集合是S的 最大权值和方案数 这题我用记忆化搜索,从终点开始递归进行,感觉这样 ...

  3. 【状压dp】Islands and Bridges

    Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 11034   Accepted: 2 ...

  4. HDU 1668 Islands and Bridges

    Islands and Bridges Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on HDU. Ori ...

  5. Islands and Bridges(POJ2288+状压dp+Hamilton 回路)

    题目链接:http://poj.org/problem?id=2288 题目: 题意:求Hamilton 路径权值的最大值,且求出有多少条权值这么大的Hamilton路径. 思路:状压dp,dp[i] ...

  6. CH0103最短Hamilton路径 & poj2288 Islands and Brigdes【状压DP】

    虐狗宝典学习笔记: 取出整数\(n\)在二进制表示下的第\(k\)位                                                    \((n >> ...

  7. DP:Islands and Bridges(POJ 2288)

    2015-09-21 造桥基建工程 题目大意,就是有n座岛和k座桥,要你找一条哈密顿圈(找完所有的岛,并且每个岛只经过一次),当经过一座岛就加上岛的价值,如果两岛联通,则加上两座岛的价值之积,如果三座 ...

  8. Islands and Bridges(POJ 2288状压dp)

    题意:给你一个图和每个点的价值,边权值为连接两点权值的积,走哈密顿通路,若到达的点和上上个点相连则价值加三点乘积,求哈密顿通路的最大价值,和最大价值哈密顿通路的条数. 分析:开始看这个题很吓人,但想想 ...

  9. poj 2288 Islands and Bridges

    题意: 给你一个双向连通图,求 获得权值最大 的 哈密顿通路的 权值 和 这个权值对应的数目: 其中权值计算方法是  列如 ABCD  权值是a+b+c+d+ab+bc+cd 如果 A,B,C  和B ...

随机推荐

  1. 怎么解决Android studio导入项目卡死

    在使用Android studio的时候常常遇到这样的问题,从github或是其他地方导入项目,Android studio呈现卡死的现象!当遇到这种情况时,可以看看是下面那种情况,在按照方法来解决! ...

  2. Android 屏幕适配(二)增强版百分比布局库(percent-support-lib)

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46767825: 本文出自:[张鸿洋的博客] 一 概述 上周一我们发布了Andr ...

  3. 一篇搞定RSA加密与SHA签名|与Java完全同步

    基础知识 什么是RSA?答:RSA是一种非对称加密算法,常用来对传输数据进行加密,配合上数字摘要算法,也可以进行文字签名. RSA加密中padding?答:padding即填充方式,由于RSA加密算法 ...

  4. codevs 1133 表达式的值

    1133 表达式的值 2011年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descript ...

  5. 07SpringMvc_jsp到jsp的控制器_ParameterizableViewController

    本文主要讲的是控制器,Action继承什么类.记得Springmvc系列的第一篇文章说过.SpirngMVC的实现流程.

  6. CLR执行模式之程序集代码的执行

    所知IL是与CPU无关的机器语言,其能访问和操作对象类型,并提供指令来创建和初始化对象,调用对象上的虚方法以及直接操作数组对象等,故可视为一种面向对象的机器语言.每种语言的存在都有其存在的价值和原因, ...

  7. 微软职位内部推荐-SW Engineer for Skype

    微软近期Open的职位: We are the Skype Beijing team. Skype division drives the communications strategy for Mi ...

  8. .htaccess设置静态资源缓存(即浏览器缓存)

    在HTTP标头中为静态资源设置过期日期或最长存在时间,可指示浏览器从本地磁盘中加载以前下载的资源,而不是通过网络加载.这样, 网站加载速度会更快. 下面的代码都需要放到.htaccess中才能生效. ...

  9. yum标准化安装nginx最新版

    yum标准化安装nginx最新版 cat > /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.o ...

  10. 开源(免费)三维 GIS(地形,游戏)

    先写想法: 想做个简单的地形漫游,于是考虑在ww直接开发,或找个控件形式的开发组件. 最大的期望有: 1. 支持google的sketchup,快速智能三维建模 2. 设计模式做好点,最好先做成组件形 ...