Islands and Bridges
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 8925 | Accepted: 2319 |
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
题意:给出每个岛屿的权值,要求找出哈密顿路(即每个点只访问一次的路径),路径的权值分为三部分。1、所有岛屿的权值和 2、路径中相邻两个岛屿的乘积累加和(这里的相邻不是编号相邻,只是路径中的相邻点而已)3、路径中相邻的三个点i,j,k,若满足存在路径[i,j],[j,k],[k,i]的三角关系(这里只是有这么一个回路,不是判断是否是三角形),则权值加上i,j,k三个岛屿的权值
思路:定义一个4维数组,dp[i][j][k][l],i用状压表示当前已经有哪些岛屿访问过用1表示,j表示该状态中倒数第二个点,k是倒数第一个点,l有两个值0跟1,用来表示该状态下的最大权值和路径数
一开始先给只有一座岛屿的状态赋值,它的倒数第二个点我都用0表示,其他点我用大于0的下标表示,这样就有
for(int i=1;i<=n;i++)
dp[1<<(i-1)][0][i][0]=num[i],dp[1<<(i-1)][0][i][1]=1;
然后从状态1一直推到(1<<n)-1,分成当前状态中岛屿是1个还是两个以上,1个就只需再增加一个点,而不需要考虑三角情况,多个点的话,就得枚举该状态中的两个点然后与另一个当前状态没有的点,推出一个新的状态,然后判断是否存在三角关系,求出新的和并进行状态的转移
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long num[14];
long long dp[1<<13][14][14][2];
bool ok[14][14];
int count(int x){
int ans=0;
while(x){
ans++;
x-=(x&-x);
}
return ans;
}
int main(void){
int t;
int n,m;
int u,v;
int have;
int flag;
long long res,cou;
scanf("%d",&t);
while(t--){
memset(dp,-1,sizeof(dp));
memset(ok,false,sizeof(ok));
flag=0,res=-1,cou=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%lld",&num[i]);
while(m--){
scanf("%d%d",&u,&v);
ok[v][u]=ok[u][v]=true;
}
if(n==1){
printf("%lld 1\n",num[1]);
continue;
}
for(int i=1;i<=n;i++)
dp[1<<(i-1)][0][i][0]=num[i],dp[1<<(i-1)][0][i][1]=1;
for(int i=1;i<(1<<n);i++){
have=count(i);
if(have==1){
for(int j=1;j<=n;j++)//拥有j
if((i&(1<<(j-1)))&&dp[i][0][j][1]!=-1)
for(int k=1;k<=n;k++)//添加新k
if((i&(1<<(k-1)))==0&&ok[j][k]) {
long long temp=dp[i][0][j][0]+num[k]+num[j]*num[k];//新的权值
int news=(i|(1<<(k-1)));//新的状态
if(temp>dp[news][j][k][0])
dp[news][j][k][0]=temp,dp[news][j][k][1]=dp[i][0][j][1];
else if(temp==dp[news][j][k][0])
dp[news][j][k][1]+=dp[i][0][j][1];
if(count(news)==n){
flag=1;
if(dp[news][j][k][0]>res)
res=dp[news][j][k][0],cou=dp[news][j][k][1];
else if(dp[news][j][k][0]==res)
cou+=dp[news][j][k][1];
}
}
}
else {
for(int j=1;j<=n;j++)
if(i&(1<<(j-1)))
for(int k=1;k<=n;k++)
if((j!=k)&&(i&(1<<(j-1)))&&dp[i][j][k][1]!=-1)
for(int l=1;l<=n;l++)
if((i&(1<<(l-1)))==0&&ok[k][l]){
int gai=0;//判断下面的 dp[news][k][l]是否修改过
long long temp=dp[i][j][k][0]+num[l]+num[k]*num[l];
if(ok[j][l])
temp+=num[j]*num[k]*num[l];
int news=(i|(1<<(l-1)));
if(temp>dp[news][k][l][0])
dp[news][k][l][0]=temp,dp[news][k][l][1]=dp[i][j][k][1],gai=1;
else if(temp==dp[news][k][l][0])
dp[news][k][l][1]+=dp[i][j][k][1],gai=1;
if(count(news)==n&&gai){
flag=1;
if(dp[news][k][l][0]>res)
res=dp[news][k][l][0],cou=dp[news][k][l][1];
else if(dp[news][k][l][0]==res)//gai主要针对这条
cou+=dp[i][j][k][1];
//可能news的状态原本值就是res,然后是通过dp[i][j][k]转移过来
}
}
}
}
if(flag)
printf("%lld %lld\n",res,cou/2);
//方案数我求的是cou,看了题解才知道是cou/2,可能是头跟尾可以反过来算一条的缘故吧
else printf("0 0\n");
}
return 0;
}