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. HDU-3577-Fast Arrangement-区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 好吧,我认为这道题有必要说一下题目意思:毕竟我刚開始是没有看太懂,原谅我这个英语渣渣...ORZ ...

  2. Vue源代码笔记(一)数据绑定

    VUE数据绑定介绍 数据绑定是vue的基础核心之一,本文以Vue对象(当然也包含VueComponent)里的data数据绑定为例,阐述整个绑定的过程. Vue的数据绑定由三部分组成, Observe ...

  3. 学习bootstrap

    菜鸟教程 bootstrap开发框架 伍华聪 Bootstrap——一款超好用的前端框架

  4. Linux系统下安装redis

    Linux 下安装 下载地址:http://redis.io/download,下载最新文档版本. 本教程使用的最新文档版本为 2.8.17,下载并安装: $ wget http://download ...

  5. Kafka框架基础

    * Kafka框架基础 官网:kafka.apache.org 框架简介 Apache Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.Kaf ...

  6. 【DNN引用包】

    <%@ Register TagPrefix="dnn" TagName="address" Src="~/controls/address.a ...

  7. GPU Command Buffer

    For Developers‎ > ‎Design Documents‎ > ‎ GPU Command Buffer This are mostly just notes on the ...

  8. Java ——代理模式[转发]

    1.  简介 代理模式(Proxy Pattern)是GoF 23种Java常用设计模式之一.代理模式的定义:Provide a surrogate or placeholder for anothe ...

  9. MySql系列之初识

    数据库管理软件的由来 基于我们之前所学,数据要想永久保存,都是保存于文件中,毫无疑问,一个文件仅仅只能存在于某一台机器上. 如果我们暂且忽略直接基于文件来存取数据的效率问题,并且假设程序所有的组件都运 ...

  10. 题解 CF383C 【Propagating tree】

    这道题明明没有省选难度啊,为什么就成紫题了QAQ 另:在CF上A了但是洛谷Remote Judge玄学爆零. 思路是DFS序+线段树. 首先这道题直观上可以对于每一次修改用DFS暴力O(n),然后对于 ...