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. jQuery右侧悬浮楼层滚动 电梯菜单

    http://www.kaiu.net/effectCon.aspx?id=2198 <!doctype html> <html> <head> <meta ...

  2. linux===linux后台运行和关闭、查看后台任务(转)

    fg.bg.jobs.&.ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的 一.& 最经常被用到这个用在一个命令的最后,可以把这个命令放 ...

  3. sicily 4699. 简单哈希

    Description 使用线性探测法(Linear Probing)可以解决哈希中的冲突问题,其基本思想是:设哈希函数为h(key) = d, 并且假定哈希的存储结构是循环数组, 则当冲突发生时,  ...

  4. 【VIPM技巧】多版本LabVIEW无法连接问题

    前言 今天小编突然用到一个Toolkit,遂去VIPM上搜索,虽然可以找到但是无法连接成功LabVIEW,配置好一阵才解决.这里记录一下整个思路,供需要的人参考 问题记录 VIMP连接超时 问题解决 ...

  5. c# 多线程多文件批量下载

    废话少说,先演示一张效果图 简单说下过程喽 开发过程中其实总是会碰到项目想应用下载文件~ 看其他语言有很多封装好的类库可以使用~~ 作为小白的我并没有找到很多c#的案例可参考 后找到一款“MutThr ...

  6. 【python】日志系统

    来源: http://blog.csdn.net/wykgf/article/details/11576721 http://www.jb51.net/article/42626.htm http:/ ...

  7. liunx命令大全

    Linux常用命令大全   Linux常用命令大全(非常全!!!) 最近都在和Linux打交道,感觉还不错.我觉得Linux相比windows比较麻烦的就是很多东西都要用命令来控制,当然,这也是很多人 ...

  8. mac下安装golang

    1.安装homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/in ...

  9. Django 如何实现文件下载

    1. 思路: 文件,让用户下载 - a标签+静态文件 - 设置响应头(django如何实现文件下载) 2. a标签实现 <a href="/static/xxx.xlsx"& ...

  10. redis之(十八)redis的支持水平扩容的集群特性,以及插槽的相关操作

    [一]主从集群的缺点,客户端分片的缺点 (1)主从+哨兵的redis集群,只是做主从备份,数据冗余的一种处理.但在存储空间的扩展上还是有限制.因为集群中的节点都是存储同样的数据.单一节点的容量,就可以 ...