HDU 1668 Islands and Bridges
Islands and Bridges
This problem will be judged on HDU. Original ID: 1668
64-bit integer IO format: %I64d Java class name: Main
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
Output
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
#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的更多相关文章
- POJ2288 Islands and Bridges
Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...
- 【状压dp】Islands and Bridges
Islands and Bridges Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 11034 Accepted: 2 ...
- Hdu 4738 Caocao's Bridges (连通图+桥)
题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这 ...
- [poj2288] Islands and Bridges (状压dp)
Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...
- HDU 4738 Caocao's Bridges(Tarjan求桥+重边判断)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4738——Caocao's Bridges——————【求割边/桥的最小权值】
Caocao's Bridges Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- hdu 4738 Caocao's Bridges 图--桥的判断模板
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4738 Caocao's Bridges
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4738 Caocao's Bridges (2013杭州网络赛1001题,连通图,求桥)
Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- Session、Cookie总结
什么是sessnion,session存在哪,能存多久.怎么设置他的存储时间 一.什么是session 1.session 被翻译为会话.当client(一般都是浏览器作为client)訪问serve ...
- SVN管理工具的使用(实习第9天)
最后就是我进行提交代码的时候要记得写注释,自己写了那些方法要标明,或者自己修改了哪些功能也要表明.不然我会被骂的. 2>代码冲突解决的问题: 服务器上的代码跟自己代码改动的地方不同 这种情况比较 ...
- less14 颜色函数2
less div{ // hue()色相值 z-index: hue(hsl(90,100%,50%)); //90 ////saturation()饱和度 z-index: saturation(h ...
- bzoj1497: [NOI2006]最大获利(最大权闭合子图)
1497: [NOI2006]最大获利 题目:传送门 题解: %%%关于最大权闭合子图很好的入门题 简单说一下什么叫最大权闭合子图吧...最简单的解释就是正权边连源点,负权边连汇点(注意把边权改为正数 ...
- hdoj--2282--Chocolate(最小费用)
Chocolate Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- <Sicily>Funny Game
一.题目描述 Two players, Singa and Suny, play, starting with two natural numbers. Singa, the first player ...
- oracle(sql)基础篇系列(四)——数字字典、索引、序列、三范式
数字字典表 --查看当前用户下面有哪些张表 select * from user_tables; select table_name from user_tables; --查看当前用户下面有哪些视图 ...
- PUBG
题目描述 最近,喜爱ACM的PBY同学沉迷吃鸡,无法自拔,于是又来到了熟悉的ERANGEL.经过一番搜寻,PBY同学准备动身前往安全区,但是,地图中埋伏了许多LYB,PBY的枪法很差,希望你能够帮他找 ...
- Windows Server 2012 r2 显示计算机图标
在 Windows Server 2012 R2 系统中,微软取消了服务器桌面个性化选项,如何重新调出配置界面,在桌面上显示计算机图标,本文为大家介绍一下! Win2012我的电脑怎么显示到桌面? 一 ...
- 洛谷2474 [SCOI2008] 天平 差分约束->枚举
题目描述 你有n个砝码,均为1克,2克或者3克.你并不清楚每个砝码的重量,但你知道其中一些砝码重量的大小关系.你把其中两个砝码A 和B 放在天平的左边,需要另外选出两个砝码放在天平的右边.问:有多少种 ...