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. 【洛谷 P1452】 Beauty Contest (二维凸包,旋转卡壳)

    题目链接 旋转卡壳模板题把. 有时间再补总结吧. #include <cstdio> #include <cmath> #include <algorithm> u ...

  2. Python3 面向对象编程

    小案例: #!/usr/bin/env python # _*_ coding:utf-8 _*_ # Author:Bert import sys class Role(object): n=&qu ...

  3. Linux时间子系统之八:动态时钟框架(CONFIG_NO_HZ、tickless)【转】

    转自:http://blog.csdn.net/droidphone/article/details/8112948 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 数据结 ...

  4. telnet如何保存输出内容到本地

    telnet如何保存输出内容到本地 http://bbs.csdn.net/topics/391023327 一种将程序的标准输出重定向到telnet终端的方法 http://blog.chinaun ...

  5. Java中volatile修饰符,不稳定标记的用法笔记

    今天学java特性时,发现了volatile修饰符,这个修饰符修饰的变量告诉java编译器忽略优化机制,这样的优势是: java优化后,寄存器会缓存内存里的变量,另一个线程修改这个变量的内存时,不会同 ...

  6. WA时查错点

    这篇文章旨在总结可能出错的原因,想到时随时会补充. 查看调试输出语句是否删除 查看数组是否清零 查看是否使用long long 查看是否有的常量应开LL(如1LL << (32) ) 查看 ...

  7. C基础 如何得到文件长度

    引言 有一天看见看到返回文件长度代码返回值都是long,就感觉怪怪的, 一般32位long最大也就2G. 而大文件太多了, 一个Dota2安装包估计都得10多G吧. 一般C得到文件长度代码 /* * ...

  8. eclipse 配置jsp

    1.安装jdk和jre 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...

  9. 生成RSA2公钥、私钥

    RSA2是一种被使用广泛的非对称加密算法. openssl OpenSSL> genrsa -out app_private_key.pem # 私钥RSA2 OpenSSL> rsa - ...

  10. Flask 知识总结

    阅读目录 第一篇:Flask基础知识介绍 第二篇:Flask扩展小结 第三篇:Flask 配置文件 第四篇:ORM.MySQL数据库连接池 第五篇:单例模式.蓝图Blueprint 第六篇:SQLAL ...