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

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

Source

 
题目大意:有N个岛屿,M条边,经过所有的岛屿使点权和最大。对于一个点i和上一个点j的贡献是C[i]+C[j]+C[i]*C[j],特别地,对于构成三角形的一个点i和上一个点j、上上个点k对答案的特别贡献是C[i]*C[j]*C[k](在原来的基础上),并输出有多少条不同的路径(倒过来的两条路径算作一条)
试题分析:dp[S][i][j]表示当前走过的集合是S,现在在i,上一个在j的最大点权和,直接写就好了。需要注意long long以及N=1的情况。
 
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define LL long long
inline LL read(){
LL x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
} const int MAXN=100001;
const int INF=999999;
int N,M;
int T;
LL C[101];
bool e[101][101];
LL dp[9001][15][15];
LL num[9001][15][15]; int main(){
T=read();
while(T--){
LL ans=0;
memset(e,false,sizeof(e));
memset(dp,-1,sizeof(dp));
memset(num,0,sizeof(num));
N=read(),M=read();
for(int i=1;i<=N;i++) C[i]=read();
for(int i=1;i<=M;i++){
int u=read(),v=read();
e[u][v]=e[v][u]=true;
}
if(N==1){
printf("%lld %lld\n",C[1],1);
continue;
}
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
if(i!=j&&e[i][j]) {
dp[(1<<(i-1))+(1<<(j-1))][i][j]=(long long)C[i]+C[j]+C[i]*C[j];
num[(1<<(i-1))+(1<<(j-1))][i][j]=1;
}
for(int i=0;i<(1<<N);i++){
for(int j=1;j<=N;j++){
if(!((i>>(j-1))&1)) continue;
for(int k=1;k<=N;k++){
if(k==j||!e[k][j]||!((i>>(k-1))&1)) continue;
for(int p=1;p<=N;p++){
if(!((i>>(p-1))&1)||p==k||p==j||!e[p][k]) continue;
if(dp[i-(1<<(j-1))][k][p]==-1) continue;
LL t1=(long long)dp[i-(1<<(j-1))][k][p]+C[j]*C[k]+C[j];
if(e[p][j]) t1=(long long)dp[i-(1<<(j-1))][k][p]+C[j]*C[k]+C[j]*C[k]*C[p]+C[j];
if(t1>dp[i][j][k]){
dp[i][j][k]=t1;
num[i][j][k]=num[i-(1<<(j-1))][k][p];
}
else if(dp[i][j][k]==t1) num[i][j][k]+=num[i-(1<<(j-1))][k][p];
}
}
}
}
LL tmp=0;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
if(i!=j)
if(ans<dp[(1<<N)-1][i][j]) ans=dp[(1<<N)-1][i][j],tmp=num[(1<<N)-1][i][j];
else if(ans==dp[(1<<N)-1][i][j]) tmp+=num[(1<<N)-1][i][j];
printf("%lld %lld\n",ans,tmp/2);
}
}

  

【状压dp】Islands and Bridges的更多相关文章

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

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

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

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

  3. 状压DP天秀

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

  4. 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 ...

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

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

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

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

  7. poj3311 TSP经典状压dp(Traveling Saleman Problem)

    题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...

  8. [NOIP2016]愤怒的小鸟 D2 T3 状压DP

    [NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...

  9. 【BZOJ2073】[POI2004]PRZ 状压DP

    [BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...

  10. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

随机推荐

  1. 【洛谷 P2303】 [SDOi2012]Longge的问题 (欧拉函数)

    题目链接 题意:求\(\sum_{i=1}^{n}\gcd(i,n)\) 首先可以肯定,\(\gcd(i,n)|n\). 所以设\(t(x)\)表示\(gcd(i,n)=x\)的\(i\)的个数. 那 ...

  2. for in、each; for 、forEach、map

    1.jQuery.each(object, [callback]) 用于例遍任何对象.回调函数拥有两个参数:第一个为对象的成员或数组的索引,第二个为对应变量或内容.如果需要退出 each 循环可使回调 ...

  3. vue手势解决方案

    1.需求 因为项目中要做一个可以移动.旋转和放缩具有合成图片的功能,例如: 剑可以随意移动,然后把位移.旋转角度和放缩值传给后台进行合成. 2.解决方案 网上搜到手势插件AlloyFinger,htt ...

  4. sublime3 快捷键大全

    Ctrl+Shift+P:打开命令面板Ctrl+P:搜索项目中的文件Ctrl+G:跳转到第几行Ctrl+W:关闭当前打开文件Ctrl+Shift+W:关闭所有打开文件Ctrl+Shift+V:粘贴并格 ...

  5. hdu 1598 find the most comfortable road (并查集+枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/ ...

  6. 用例图(Use Case Diagram)

    用例图(Use Case Diagram) 执行者/参与者(Actor): 表示与您的应用程序或系统进行交互的用户.组织或外部系统.用一个小人表示. 用例(Use Case): 即系统具有的功能,在用 ...

  7. vue.js将一个对象的所有属性作为prop进行传递

    1.方法一:使用不带参数的v-bind写法 <div id="app"> <child v-bind="todo"></child ...

  8. Python3 shelve模块(持久化)

    shelve模块 也可以序列化Python所有数据类型,而且可以多次序列化;shelve模块通过key-value方式持久化 1.序列化 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  9. ZOJ 3537 Cake 求凸包 区间DP

    题意:给出一些点表示多边形顶点的位置(如果多边形是凹多边形就不能切),切多边形时每次只能在顶点和顶点间切,每切一次都有相应的代价.现在已经给出计算代价的公式,问把多边形切成最多个不相交三角形的最小代价 ...

  10. python写一段脚本代码自动完成输入(目录下的所有)文件的数据替换(修改数据和替换数据都是输入的)【转】

    转自:http://blog.csdn.net/lixiaojie1012/article/details/23628129 初次尝试python语言,感觉用着真舒服,简单明了,库函数一调用就OK了 ...