Islands and Bridges

Time Limit: 4000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 1668
64-bit integer IO format: %I64d      Java class name: Main

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.

 

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

 
解题:一道状压dp题啊,dp[i][j][k]表示当前状态i且当前在k,上一个状态在j
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
bool arc[maxn][maxn];
int dp[<<maxn][maxn][maxn],val[],n,m;
LL cnt[<<maxn][maxn][maxn];
int main() {
int cs;
scanf("%d",&cs);
while(cs--) {
scanf("%d %d",&n,&m);
memset(arc,false,sizeof arc);
for(int i = ; i < n; ++i) scanf("%d",val+i);
for(int u,v, i = ; i < m; ++i) {
scanf("%d %d",&u,&v);
arc[u-][v-] = arc[v-][u-] = true;
}
if(n == ) {
printf("%d 1\n",val[]);
continue;
}
memset(dp,-,sizeof dp);
memset(cnt,,sizeof cnt);
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j)
if(i != j && arc[i][j]) {
dp[(<<i)|(<<j)][i][j] = val[i] + val[j] + val[i]*val[j];
cnt[(<<i)|(<<j)][i][j] = ;
}
for(int i = ; i < (<<n); ++i) {
for(int j = ; j < n; ++j) {
if(i&(<<j)) {
for(int k = ; k < n; ++k) {
if(j != k && (i&(<<k)) && arc[j][k] && dp[i][j][k] != -) {
for(int t = ; t < n; ++t) {
if((i&(<<t)) == && arc[k][t] && j != t && k != t) {
int tmp = dp[i][j][k] + val[t] + val[k]*val[t];
if(arc[j][t]) tmp += val[j]*val[k]*val[t];
if(dp[i|(<<t)][k][t] == tmp)
cnt[i|(<<t)][k][t] += cnt[i][j][k];
else if(dp[i|(<<t)][k][t] < tmp) {
dp[i|(<<t)][k][t] = tmp;
cnt[i|(<<t)][k][t] = cnt[i][j][k];
}
}
}
}
}
}
}
}
int ret = ;
LL ret2 = ;
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j)
if(i != j && arc[i][j]) {
if(ret < dp[(<<n)-][i][j]) {
ret = dp[(<<n)-][i][j];
ret2 = cnt[(<<n)-][i][j];
} else if(ret == dp[(<<n)-][i][j])
ret2 += cnt[(<<n)-][i][j];
}
printf("%d %I64d\n",ret,ret2>>);
}
return ;
}

最新文章

  1. log4j的配置
  2. jq冒泡之——点击其他地方隐藏
  3. 1Z0-053 争议题目解析481
  4. B树、B-树、B+树、B*树
  5. Html技巧 语义化你的代码
  6. MyBatis学习总结
  7. 解决中64位Win7系统上PLSQL无法连接ORACLE的方法(PLSQL无法识别ORACLE_HOME的配置)
  8. HTML5 Web Storage概述
  9. java流布局管理器
  10. windows条件下,Ping加上时间戳,并保存到文件,适用于测试网络
  11. PowerDesigner的样式设置
  12. WebApi(一)-实现跨域返回格式支持json
  13. 用 Google 挖掘赚钱思路
  14. 浅析JavaScript解析赋值、浅拷贝和深拷贝的区别
  15. PHP调用Python接口过程中所遇到的问题
  16. VueJs(6)---V-on指令
  17. BZOJ.3720.Gty的妹子树(树分块)
  18. elasticsearch基本操作之--使用QueryBuilders进行查询
  19. Linux平台 获取程序依赖文件
  20. 朽木第一至三季/全集Deadwood迅雷下载

热门文章

  1. 计算机系统之汇编---IA32处理器数据格式及数据操作
  2. SharePoint Search之(七)Search result- 结果源
  3. nj08---process、console
  4. BZOJ 3600 替罪羊树+线段树
  5. 未能加载文件或程序集&ldquo;MICROSOFT.REPORTVIEWER.WEBFORMS &hellip;
  6. SQL 循环30日
  7. jQuery新浪微博表情插件教程
  8. ES6中includes、startsWith、endsWith
  9. UI Framework-1: Aura Layout Managers
  10. php八大设计模式之装饰器模式