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 ViVi+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 ViVi+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

Solution

一看数据(13)就知道是状压。。。

分析题目发现需要知道前两个岛是什么,那就暴力枚举就好了

最后统计最大值与对应方案(方案数跟据题目要求要/2)

注意特判1qwq

Code

//By Menteur_Hxy
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define M(a,b) memset(a,(b),sizeof(a))
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
using namespace std;
typedef long long LL; LL read() {
LL x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f; c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
} int n,m;
int val[13],edg[13][13];
LL dp[1<<13][13][13],num[1<<13][13][13]; int main() {
int T=read();
while(T--) {
M(edg,0);M(dp,-1);M(num,0);
n=read(),m=read();
F(i,0,n-1) val[i]=read();
if(n==1) {printf("%d 1\n",val[0]);continue;}// WA*1
F(i,1,m) {int u=read()-1,v=read()-1;
edg[u][v]=edg[v][u]=1;
}
F(i,0,n-1) F(j,0,n-1) if(i!=j && edg[i][j])
dp[(1<<i)|(1<<j)][i][j]=val[i]+val[j]+val[i]*val[j],num[(1<<i)|(1<<j)][i][j]=1;
F(i,0,(1<<n)-1) F(j,0,n-1) if((1<<j)&i)
F(k,0,n-1) if(edg[j][k] && j!=k && (i&(1<<k)) && dp[i][j][k]!=-1)
F(x,0,n-1) if(edg[k][x] && k!=x && j!=x && !(i&(1<<x))) {
int tmp=dp[i][j][k]+val[x]+val[k]*val[x];
if(edg[j][x]) tmp+=val[j]*val[k]*val[x];
if(dp[i|(1<<x)][k][x]<tmp) {
dp[i|(1<<x)][k][x]=tmp;
num[i|(1<<x)][k][x]=num[i][j][k];
} else if(dp[i|(1<<x)][k][x]==tmp)
num[i|(1<<x)][k][x]+=num[i][j][k];
}
LL ans1=0,ans2=0;
F(i,0,n-1) F(j,0,n-1) if(i!=j && edg[i][j]) {
if(ans1<dp[(1<<n)-1][i][j]) ans1=dp[(1<<n)-1][i][j],ans2=num[(1<<n)-1][i][j];
else if(ans1==dp[(1<<n)-1][i][j]) ans2+=num[(1<<n)-1][i][j];
}
printf("%lld %lld\n",ans1,ans2>>1);
}
return 0;
}

[poj2288] Islands and Bridges (状压dp)的更多相关文章

  1. poj 2288 Islands and Bridges ——状压DP

    题目:http://poj.org/problem?id=2288 状压挺明显的: 一开始写了(记忆化)搜索,但一直T: #include<iostream> #include<cs ...

  2. poj 2288 Islands and Bridges——状压dp(哈密尔顿回路)

    题目:http://poj.org/problem?id=2288 不知为什么记忆化搜索就是WA得不得了! #include<iostream> #include<cstdio> ...

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

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

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

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

  5. 状压DP天秀

    状压DP,依靠的是把状态用某种压缩方式表示出来进而DP,大多数时候是二进制状压. 直接看例题吧. 一双木棋     九尾狐吃棉花糖     islands and bridges 愤怒的小鸟   芯片 ...

  6. 状压dp之位运算

    ## 一.知识 1.我们知道计算机中数据由二进制数存储,一个二进制数的一位就是计算机中数据的最小单位bit,我们有一种运算符可直接对二进制数进行位运算,所以它的速度很快. 2.C++中的位运算符有6种 ...

  7. BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS

    BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS Description Farmer John has taken the cows to a va ...

  8. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  9. nefu1109 游戏争霸赛(状压dp)

    题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...

随机推荐

  1. [bzoj1452][JSOI2009]Count_树状数组

    Count bzoj-1452 JSOI-2009 题目大意:请维护一个平面内的数据结构,支持:单点修改,查询矩形内给定权值出现次数. 注释:$1\le n,m\le 300$,$1\le Q \le ...

  2. Thread.yield()方法表示交出主动权,join表示等待当前线程,可以指定秒数

    Thread.yield()方法表示交出主动权,join表示等待当前线程,可以指定秒数 学习了:http://www.importnew.com/14958.html 膜拜一下 源码膜拜: Threa ...

  3. 实现icon和文字垂直居中的两种方法-(vertical-align and line-height)

    方法一:vertical-align 在w3school定义:该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐 百思不得骑姐 然后Google,反正在w3schools上面并没有找到定义 仅 ...

  4. Android基础新手教程——3.4 TouchListener PK OnTouchEvent + 多点触碰

    Android基础新手教程--3.4 TouchListener PK OnTouchEvent + 多点触碰 标签(空格分隔): Android基础新手教程 本节引言: 如题,本节给大家带来的是To ...

  5. chrome 插件开发2

    登录 | 注册   基础文档 综述 调试 Manifest 文件 代码例子 模式匹配 分类索引 改变浏览器外观 Browser Actions 右键菜单 桌面通知 Omnibox 选项页 覆写特定页 ...

  6. luogu2518 [HAOI2010] 计数

    题目大意 给出一个数字$n$,求满足下列条件的数$x$的个数: $x<n$ 对于来自于$x$十进制各个数位上的非零数字,它们的种类与个数都与$n$的相同. 思路 入手点 设$n$有$t$位数字, ...

  7. <vim实用技巧>学习笔记

    第三章插入模式 1.插入模式下的删除  2.返回普通模式                                 3.复制 yt, //复制当前光标到逗号(,)之前的内容 第四章 可视模式 1 ...

  8. k8s Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry

    环境介绍: 说明 节点 ip 系统 Gitlab Server git.ds.com 10.0.1.179 CentOS 7.5.1804 Gitlab Runner   10.0.1.178 Cen ...

  9. 排序系列 之 冒泡排序及其改进算法 —— Java实现

    冒泡排序算法 冒泡排序算法 改进一 冒泡排序算法 改进二 冒泡排序算法 改进三 冒泡排序算法 基本思想: 在要排序的一组数中,对当前还未排好序的范围内的全部数据,自上而下对相邻的两个数依次进行比较和调 ...

  10. 计算机网络自顶向下方法第2章-应用层(application-layer).2

    2.4 DNS:因特网的目录服务 2.4.1 DNS提供的服务 DNS的定义 实体层面看,DNS是一个由分层的DNS服务器实现的分布式数据库 协议层面看,DNS是一个使得主机能够查询分布式数据库的应用 ...