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) ...
随机推荐
- linux程序设计——个人总结
linux程序设计--个人总结 到今天为止,<linux程序设计>学习基本完毕了.从五月下旬開始接触linux,学习安装Ubuntu14.04,六月份開始学习<linux程序设计&g ...
- CSS透明度设置支持IE,Chrome,Firefox浏览器
CSS文件里设置例如以下就可以 filter:alpha(opacity=60); //支持IE opacity:0.6; //支持Chrome.Firefox
- 【RQNOJ】460 诺诺的队列
[题目大意] 求全部数对(i,j)满足随意a[k]<=a[i]且a[k]<=a[j]. 形象地说,就是有一群人站成一列.每一个人有一定的身高,然后问有多少对人能够互相看得到. 把数对(i, ...
- Android 进程常驻(0)----MarsDaemon使用说明
版权声明:本文为博主原创文章,未经博主允许不得转载. 这是一个轻量级的库,配置几行代码,就可以实现在Android上实现进程常驻,也就是在系统强杀下,以及360获取root权限下,clean mast ...
- 登录那些事儿+ Session原理
http://cnodejs.org/topic/5671441a1d2912ce2a35aaa1 登录那些事儿 http://www.jianshu.com/p/2b7c10291aad Sess ...
- [NOIP2015模拟10.27] 挑竹签 解题报告(拓扑排序)
Description 挑竹签——小时候的游戏夏夜,早苗和诹访子在月光下玩起了挑竹签这一经典的游戏.挑竹签,就是在桌上摆上一把竹签,每次从最上层挑走一根竹签.如果动了其他的竹签,就要换对手来挑.在所有 ...
- A string is a sequence
A string is a sequence of characters. You can access the characters one at a time with the bracket o ...
- webi和universe
Universe是一个包含以下内容的文件: 1 一个或多个数据库中间件的连接参数. 2 称为对象的SQL结构,映射到数据库中的实际SQL结构,如列,表和数据库函数.其中对象是按类分组的.用户既可以看到 ...
- Android框架-Volley(三)
经过前面两篇文章的学习,我们已经掌握了Volley各种Request的使用方法,包括StringRequest.JsonRequest.ImageRequest等.其中StringRequest用于请 ...
- 《剑指offer》矩形覆盖
一.题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 二.输入描述 输入n 三.输出描述 输出有多少种不同的覆 ...