Islands and Bridges
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 11116 | Accepted: 2895 |
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
Source
求最优Hamiltonian path
dp[i][j][k]表示在状态为i时,到达k岛且上一个到达的岛为j时的the maximum value
num[i][j][k]表示相应路径的数目
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define maxn 1<<13
int const inf=1000000000;
using namespace std;
int n,m;
long long bri[15][15],val[15];
long long dp[maxn][15][15],num[maxn][15][15];
int main ()
{
int t,i,j,k,g;
long long a,b;
scanf("%d",&t);
while(t--)
{
memset(bri,0,sizeof(bri));
memset(dp,-1,sizeof(dp));
memset(num,0,sizeof(num));
scanf("%d %d",&n,&m);
for(i=0;i<n;i++)
scanf("%lld",&val[i]);
for(i=0;i<m;i++)
{
scanf("%lld %lld",&a,&b);
a--,b--;
bri[a][b]=bri[b][a]=1;
}
if(n==1)
{
printf("%lld 1\n",val[0]);
continue;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j&&bri[i][j])
{
dp[(1<<i)|(1<<j)][i][j]=val[i]+val[j]+val[i]*val[j];
num[(1<<i)|(1<<j)][i][j]++;
}
}
}
for(i=0;i<(1<<n);i++)
{
for(j=0;j<n;j++)
{
if((i&(1<<j)))
{
for(k=0;k<n;k++)
{
if(k!=j && bri[k][j] && dp[i][j][k]!=-1)
{
for(g=0;g<n;g++)
{
if(g!=k && g!=j && bri[k][g] && (i&(1<<g))==0)
{
int tmp=dp[i][j][k]+val[g]+val[k]*val[g];
if(bri[g][j])
tmp+=val[j]*val[k]*val[g];
if(dp[i|(1<<g)][k][g]<tmp)
{
dp[i|(1<<g)][k][g]=tmp;
num[i|(1<<g)][k][g]=num[i][j][k];
}
else if(dp[i|(1<<g)][k][g]==tmp)
{
num[i|(1<<g)][k][g]+=num[i][j][k];
}
}
}
}
}
}
}
}
long long ans1=0,ans2=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j && bri[i][j])
{
if(ans1<dp[(1<<n)-1][i][j])
{
ans1=dp[(1<<n)-1][i][j];
ans2=num[(1<<n)-1][i][j];
}
else if(ans1==dp[(1<<n)-1][i][j])
{
ans2+=num[(1<<n)-1][i][j];
}
}
}
}
printf("%lld %lld\n",ans1,ans2/2);
}
return 0;
}