poj2288 类哈密顿路

来源:互联网 发布:为知笔记 手写 编辑:程序博客网 时间:2024/04/29 07:06

Islands and Bridges
Time Limit: 4000MS Memory Limit: 65536KTotal Submissions: 9297 Accepted: 2418

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

和hdu3001类似,传送门:状压dp 

此题需要三维数组,因为题目要考虑i,j,k的关系,设dp[s][i][j]表示在S状态下,前一个节点在i,现在在j的状态。然后和hdu3001一样先初始化,然后刷表法搞定。注意:题目说了正反向算一条,因此最后答案要除以2。

并且要特判n=1的情况,因为初始就要求i!=j,因此起码要2个点以上。

代码:

#include<cstdio>#include<iostream>#include<cstring>#define ll long long#define Maxn 15using namespace std;int adj[Maxn][Maxn];ll val[Maxn];ll dp[1<<14][14][14],path[1<<14][14][14];int main(){    int t,n,m,a,b;    cin>>t;    while(t--){        cin>>n>>m;        for(int i=1;i<=n;i++)            cin>>val[i];        memset(adj,0,sizeof adj);        for(int i=0;i<m;i++){            cin>>a>>b;            adj[a][b]=adj[b][a]=1;        }        if(n==1) {printf("%d 1\n",val[1]);continue;}        memset(dp,-1,sizeof dp);        memset(path,0,sizeof path);        for(int i=1;i<=n;i++) //i->j            for(int j=1;j<=n;j++){                if(i==j||!adj[i][j]) continue;                dp[(1<<i-1)+(1<<j-1)][i][j]=val[i]*val[j]+val[i]+val[j];                path[(1<<i-1)+(1<<j-1)][i][j]=1;            }        for(int s=0;s<1<<n;s++)            for(int i=1;i<=n;i++){                if(!(s&1<<i-1)) continue;                for(int j=1;j<=n;j++){                    if(!(s&1<<j-1)||i==j||!adj[i][j]) continue;                    for(int k=1;k<=n;k++){                        if(i==k||j==k||!adj[j][k]) continue;                        if((s&1<<k-1)||dp[s][i][j]==-1) continue;                        //s的第k位必须为0,并且是合法状态                        int ns=s+(1<<k-1);                        ll t=dp[s][i][j]+val[k]+val[j]*val[k];                        if(adj[i][k]) t+=val[i]*val[j]*val[k];                        if(t>dp[ns][j][k]){ //答案增大,path重置                            dp[ns][j][k]=t;                            path[ns][j][k]=path[s][i][j];                        }                        else if(t==dp[ns][j][k]){ //答案一样,path累加                            path[ns][j][k]+=path[s][i][j];                        }                    }                }            }        ll ans=0,res=0;        for(int i=1;i<=n;i++){            for(int j=1;j<=n;j++){                if(dp[(1<<n)-1][i][j]>ans){                    ans=dp[(1<<n)-1][i][j];                    res=path[(1<<n)-1][i][j];                }                else if(dp[(1<<n)-1][i][j]==ans)                    res+=path[(1<<n)-1][i][j];            }        }        printf("%lld %lld\n",ans,res/2);    }return 0;}


0 0
原创粉丝点击