POJ 2288-Islands and Bridges (状压DP)

来源:互联网 发布:犀牛软件官网 编辑:程序博客网 时间:2024/05/18 17:03
Islands and Bridges
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 7729 Accepted: 1977

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 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. 

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

23 32 2 21 22 33 14 61 2 3 41 21 31 42 32 43 4

Sample Output

22 369 1

Source

题意:求一条哈密顿回路,但是权值计算不同,包括三部分:1经过个所有点的权值相加。2经过的连续两个点的权值的乘积。3,能够构成三角型的连续三个点的乘积。这些全部加起来就是这条回路的总权值。输出最大权值和这个最大权值的路线有多少条。(哈密顿回路:哈密顿回路

题解:开始搞这道题的时候,题意理解了半天,首先是不知道有哈密顿回路这个东西,于是百度了一发,学啊学,知道什么是哈密顿回路了,还是懵逼,想了半天,觉得要搞个三维dp,因为由题上的权值计算规则可知,当前点只与之前的两个点有关,也就是三个点便可确定该状态,呢其实并不难办,其实和之前的状压几乎一样,就是要统计最大哈密顿回路的条数比较烦(其实开个数组记一下就好了)

dp[i][j][k]:状态为i时,以j结尾且上一个点为k的最大权值

num[i][j][k]:状态为i时,以j结尾且上一个点为k的最大权值出现的次数


#include<map>      #include<stack>      #include<queue>      #include<vector>      #include<math.h>      #include<stdio.h>      #include<iostream>      #include<string.h>      #include<stdlib.h>      #include<algorithm>      using namespace std;      typedef long long  ll;      #define inf 1000000000     #define mod 100000000       #define  maxn  1<<13    #define  lowbit(x) (x&-x)      #define  eps 1e-10  ll dp[maxn][14][14],num[maxn][14][14],a[14][14],v[14];bool judge(int x,int y)//判断位置是否合法{if(y==0)return 1;if(x&(1<<(y-1)))return 1;return 0;}void work(int n){ll tmp,ans1,ans2;int i,j,k,old,q;if(n==1){printf("%lld 1\n",v[1]);return;}for(i=1;i<=n;i++)//初始化可能的起点{dp[1<<(i-1)][i][0]=v[i];num[1<<(i-1)][i][0]=1;}for(i=1;i<(1<<n);i++){for(j=1;j<=n;j++){old=i^(1<<(j-1));//找到当j还没有出现的位置if(!judge(i,j))continue;for(k=1;k<=n;k++){if(k!=j && a[j][k] && judge(i,k)){for(q=0;q<=n;q++){if(q && !a[k][q])continue;if(q!=j && q!=k && judge(old,q) && dp[old][k][q]!=-1){tmp=v[j]+v[j]*v[k]+dp[old][k][q];if(a[j][q])tmp+=v[j]*v[k]*v[q];if(tmp==dp[i][j][k])num[i][j][k]+=num[old][k][q];if(tmp>dp[i][j][k]){dp[i][j][k]=tmp;num[i][j][k]=num[old][k][q];}//printf("%lld %d %d %d\n",dp[i][j][k],i,j,k);}}}}}}ans1=-1;ans2=0;for(i=1;i<=n;i++)for(j=1;j<=n;j++){if(i==j)continue;if(ans1==dp[(1<<n)-1][i][j])ans2+=num[(1<<n)-1][i][j];if(ans1<dp[(1<<n)-1][i][j]){ans1=dp[(1<<n)-1][i][j];ans2=num[(1<<n)-1][i][j];}}if(ans1==-1)printf("0 0\n");else printf("%lld %lld\n",ans1,ans2/2);//题目约定一条路的两种行走方式算一种,故答案要除以2return;}int  main(void){int n,m,T,i,x,y;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);for(i=1;i<=n;i++)scanf("%lld",&v[i]);memset(a,0,sizeof(a));memset(dp,-1,sizeof(dp));memset(num,0,sizeof(num));for(i=1;i<=m;i++){scanf("%d%d",&x,&y);a[x][y]=a[y][x]=1;}work(n);}return 0;}