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 ;
}

HDU 1668 Islands and Bridges的更多相关文章

  1. POJ2288 Islands and Bridges

    Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...

  2. 【状压dp】Islands and Bridges

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

  3. Hdu 4738 Caocao's Bridges (连通图+桥)

    题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这 ...

  4. [poj2288] Islands and Bridges (状压dp)

    Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...

  5. HDU 4738 Caocao's Bridges(Tarjan求桥+重边判断)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】

     Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. hdu 4738 Caocao's Bridges 图--桥的判断模板

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 4738 Caocao's Bridges

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)

    Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. 使用client对象模型读取SharePoint列表数据

    使用client对象模型读取SharePoint列表数据 client对象模型提供了强有力的方式.从远程client应用程序管理列表. 1. 管理员身份打开VS,新建项目Windows窗口应用程序,命 ...

  2. eclipse:报错信息The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

    JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bui ...

  3. List operations

    The + operator concatenates lists: Similarly, the * operator repeats a list a given number of items: ...

  4. 浅谈微信smali注入

    作者:郭少雷 搞android搞了几年也没搞出个啥牛逼app出来,眼看时下最火的app微信如此火热,实在想搞搞它,索性就想着给它加点东西进去. 以下内容纯属本人个人爱好,仅限个人学习android用途 ...

  5. PostgreSQL Replication之第三章 理解即时恢复(1)

    到现在为止,您已经掌握了一定的理论.因为生活不仅由理论组成(它可能同样重要),是时候深入实际的工作了. 本章的目标是让您明白如何恢复数据到一个给定的时间点.当您的系统崩溃或者有人意外地删除了一个表,不 ...

  6. 如何用Java实现反转排序

    摘要:反转排序是将原先已经排序好了的重新排序,是原来的数组元素的顺序反转过来.假设原来的数组顺序是{6,5,4,3,2,1},反转之后的顺序就是{1,2,3,4,5,6}.这个排序的算法不是很难,代码 ...

  7. 51Nod 不重叠的线段(贪心)

    X轴上有N条线段,每条线段有1个起点S和终点E.最多能够选出多少条互不重叠的线段.(注:起点或终点重叠,不算重叠). 例如:[1 5][2 3][3 6],可以选[2 3][3 6],这2条线段互不重 ...

  8. ReactiveCocoa 中 RACSignal 所有变换操作底层实现分析(上)

    前言 在上篇文章中,详细分析了RACSignal是创建和订阅的详细过程.看到底层源码实现后,就能发现,ReactiveCocoa这个FRP的库,实现响应式(RP)是用Block闭包来实现的,而并不是用 ...

  9. MySql系列表之间的关系

    foreign key 快速理解foreign key   员工信息表有三个字段:工号  姓名  部门 公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费 数据 ...

  10. [HNOI2004]高精度开根

    题目:洛谷P2293.BZOJ1213. 题目大意:给你$n,k(n\leq 10^{10000},k\leq 50)$,求$\lfloor \sqrt[k]{n}\rfloor$. 解题思路:高精度 ...