POJ2288 Islands and Bridges

Description

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 CiC i+1 in the path, we add the product Vi*V i+1. And for the third part, whenever three consecutive islands CiC i+1i+2 in the path forms a triangle in the map, i.e. there is a bridge between Ci and C i+2, we add the product Vi*V i+1*V i+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.

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

Shanghai 2004

二进制表示点的到达状态。

状态压缩求哈密顿回路,基本思路如下:

F[i][j] (0<=i<2^n,0<=j<n) 表示所有点的访问状态为i并且目前处于点j时的最短路径。
在i的二进制表示下,第k(0<=k<n)位为1表示已经访问过点k。
F[0][0]=0,Others=+∞,求F[2^n-1][n-1]。
F[i][j]=Min{F[i^1<<k][k]+w(k,j) | 0<=k<n-1且(i>>k&1)=1}

在本题中由于要考虑“三角形”关系,故须开三维,f[到达状态][上一个到达的点][本次到达的点]=最优解

同时要统计方案数,由于方案可能很多,需要开LL

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 int f[1<<13][13][13];
 9 long long num[1<<13][13][13];
10 int mp[13][13];
11 int v[16];
12 int n,m;
13 int main(){
14     int Q;
15     scanf("%d",&Q);
16     while(Q--){
17         memset(f,-1,sizeof(f));
18         memset(num,0,sizeof(num));
19         memset(mp,0,sizeof(mp));
20         scanf("%d%d",&n,&m);
21         int i,j,k,s;
22         for(i=0;i<n;i++){
23             scanf("%d",&v[i]);
24         }
25         int x,y;
26         for(i=1;i<=m;i++){
27             scanf("%d%d",&x,&y);
28             x--;y--;
29             mp[x][y]=mp[y][x]=1;
30         }
31         if(n==1){//单点特判
32             printf("%d 1\n",v[0]);
33             continue;
34         }
35         for(i=0;i<n;i++)//边界预处理
36           for(j=0;j<n;j++)
37               if(i!=j  && mp[i][j]){
38                   f[(1<<i)|(1<<j)][i][j]=v[i]+v[j]+v[i]*v[j];
39                   num[(1<<i)|(1<<j)][i][j]=1;
40               }
41         for(i=0;i<(1<<n);i++)//连通状况
42           for(j=0;j<n;j++)//枚举各岛
43               if((i&(1<<j)))
44                 for(k=0;k<n;k++)
45                   if(mp[j][k] && j!=k)
46                   if((i&(1<<k)) && f[i][j][k]!=-1)//j和k枚举的岛都在i枚举范围内,且有上一个状态
47                   for(s=0;s<n;s++){
48                       if(mp[k][s] && k!=s && !(i&(1<<s)))
49                       //k到s联通      s之前没走过
50                       {
51                           int val=f[i][j][k]+v[s]+v[k]*v[s];
52                           if(mp[j][s])val+=v[j]*v[k]*v[s];//三角形特判
53                         if(f[i|(1<<s)][k][s]<val){//更新状态
54                             f[i|(1<<s)][k][s]=val;
55                             num[i|(1<<s)][k][s]=num[i][j][k];
56                         }else if(f[i|(1<<s)][k][s]==val)
57                             num[i|(1<<s)][k][s]+=num[i][j][k];
58                       }
59                   }
60         int ans=0;
61         long long ansnum=0;//数据很大!
62         for(j=0;j<n;j++)
63           for(k=0;k<n;k++){
64               if(k!=j && mp[j][k]){
65                   s=(1<<n)-1;
66                   if(ans<f[s][j][k]){
67                       ans=f[s][j][k];
68                       ansnum=num[s][j][k];
69                 }
70                 else if(ans==f[s][j][k])//解相同则累加方案数
71                     ansnum+=num[s][j][k];
72               }
73           }
74         printf("%d %lld\n",ans,ansnum/2);
75     }
76     return 0;
77 }
时间: 2024-12-29 01:55:26

POJ2288 Islands and Bridges的相关文章

poj2288(Islands and Bridges) 状压DP

题目链接:http://poj.org/problem?id=2288 题意:每个点有一个权值Vi,找一条哈密顿路径,路径的权值来自三条:1 路径上的Vi之和 2 所有相邻点对ij的Vi*Vj之和 3 相邻连续三点i,j,k(并且三点要构成三角形)Vi*Vj*Vk之和. 解法:dp[st][i][j]表示从j走到i并且剩下集合st没有走的最大权值.关于路径书,在转移的时候顺便计算即可:这道题令自己恶心了好久,最后原因是自己犯了一个严重错误,题目读错了,没有读到Vi*Vj*Vk要保证ijk能够构成

POJ2288:Islands and Bridges(状态压缩)

Description 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 e

【汉密尔顿、DP|状态压缩】POJ-2288 Islands and Bridges

Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K       Description 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.

HDU 1668 Islands and Bridges

Islands and Bridges Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 166864-bit integer IO format: %I64d      Java class name: Main Given a map of islands and bridges that connect these islands, a Hamilton pat

POJ 2288 Islands and Bridges(状压dp)

Language: Default Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 9312   Accepted: 2424 Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the b

POJ 2288 Islands and Bridges 哈密尔顿路 状态压缩DP

找最长的其实是很裸的状态压缩DP,棘手的地方是要统计数量,其实只要再来一个数组存就好. 不过代码比较长,细节要注意的地方毕较多,wa了很多发,还是要仔细啊 用递推和记忆化搜索分别写了一遍 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <

poj 2288 Islands and Bridges

题意: 给你一个双向连通图,求 获得权值最大 的 哈密顿通路的 权值 和 这个权值对应的数目: 其中权值计算方法是  列如 ABCD  权值是a+b+c+d+ab+bc+cd 如果 A,B,C  和B,C,D 可构成三角形分别加上abc,bcd: 这个题 和poj 3311  很相像: 那个需要记录一个最后到达的地方   这个需要记录俩个罢了 DP[i][a][b]其中 i  二进制 中1表示这个点走过了   最后走的的 的是b>>a 因为对于已经走过了{1,2,3,4,,5,6,..,N}

【以前的空间】poj 2288 Islands and Bridges

一个不错的题解 : http://blog.csdn.net/accry/article/details/6607703 这是一道状态压缩.每个点有一个值,我们最后要求一个最值sum.sum由三部分组成:①每个点的值②每个点与他相邻的点的乘积③如果存在三个点成环,还要加上这三个点的值的乘积. 状态转移方程为:dp[i][j][k]=max(dp[i,j,k],dp[i'][k][l]+temp) j表示当前点,k表示上一个点,l表示上上一个点. 其中i,i'表示可以走到i点的状态,temp表示这

DP:Islands and Bridges(POJ 2288)

2015-09-21 造桥基建工程 题目大意,就是有n座岛和k座桥,要你找一条哈密顿圈(找完所有的岛,并且每个岛只经过一次),当经过一座岛就加上岛的价值,如果两岛联通,则加上两座岛的价值之积,如果三座岛之间构成三角联通,则再加上三岛之积,问最大价值的哈密顿圈和最大价值和哈密顿圈的个数 哈密顿圈是是一个NP完全的问题,用DP就可以解决这个问题,现在的问题就是,怎么解决呢? 首先我们要明确,这一题要用DP做什么,首先这一题的最后肯定要求到最后岛全部都通过的情况,然后还需要保留前两个岛的信息 那么这个