Islands and Bridges
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 10168 | Accepted: 2648 |
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+1C i+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.
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+1C i+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.
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
题意:每个点有一个权值Vi,找一条哈密顿路径,路径的权值来自三条:1 路径上的Vi之和 2 所有相邻点对ij的Vi*Vj之和 3 相邻连续三点i,j,k (并且三点要构成三角形)Vi*Vj*Vk之和。
状态压缩dp和图结合,当一个图的题数据比较小的时候就要适当考虑是否能用状态压缩了。
这题的关键在于如何dp,通常的图的题都是dp[i][j]表示状态i下以j结尾就ok,可是这题要用dp[i][j][k]来表示i状态的下以i结尾,同时i的上一个结尾的是k。因为这题要求有连续三点相邻,而我们原来表示的方法只能表示两个点,所以要加上一维数据来表示三个点。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
ll dp[1<<14][15][15],way[1<<14][15][15],v[15];
int m,n;
bool tu[15][15];
void init()
{
memset(dp,-1,sizeof(dp));
memset(tu,0,sizeof(tu));
memset(way,0,sizeof(way));
memset(v,0,sizeof(v));
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m;
init();
for(int i=0; i<n; i++)
scanf("%I64d",&v[i]);
for(int i=0; i<m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
tu[a-1][b-1]=tu[b-1][a-1]=1;
}
if(n==1)///卡n==1的数据。。。。无语
{
printf("%I64d 1\n",v[0]);
continue;
}
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
if(i!=j&&tu[i][j])
dp[(1<<i)+(1<<j)][j][i]=v[i]+v[j]+v[i]*v[j],way[(1<<i)+(1<<j)][j][i]=1;
///状态压缩dp标准一大波流!
for(int i=0; i<1<<n; i++)///状态i
for(int j=0; j<n; j++)///以j结束
if(i&(1<<j))
for(int k=0; k<n; k++)///j的上一个是k
if(j!=k&&tu[j][k]&&(i&(1<<k)))
for(int h=0; h<n; h++)///k的上一个是h
if(h!=j&&h!=k&&tu[k][h]&&(i&(1<<h))&&dp[i-(1<<j)][k][h]!=-1)
{
ll tem=dp[i-(1<<j)][k][h]+v[j]+v[j]*v[k];
if(tu[j][h])tem+=v[j]*v[k]*v[h];
if(tem==dp[i][j][k])
way[i][j][k]+=way[i-(1<<j)][k][h];
else if(tem>dp[i][j][k])
dp[i][j][k]=tem,way[i][j][k]=way[i-(1<<j)][k][h];
}
ll ans=-1,t=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
if(i!=j&&dp[(1<<n)-1][i][j]>ans)
ans=dp[(1<<n)-1][i][j],t=way[(1<<n)-1][i][j];
else if(i!=j&&dp[(1<<n)-1][i][j]==ans)
t+=way[(1<<n)-1][i][j];
}
if(ans==-1)
printf("0 0\n");
else printf("%I64d %I64d\n",ans,t/2);
}
return 0;
}