poj 2288 Islands and Bridges
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.
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
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
Shanghai 2004
여전히 상태 dp이지만 세 개의 점과 인접하여 삼각형을 형성할 때 보너스가 있기 때문에 현재 노드와 이전 노드를 보존해야 하기 때문에 3차원 그룹을 열어 해결한다.
n==1의 특판 주의, 그리고 int64.
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int val[15];
int map[15][15];
__int64 dp[(1<<14)+2][15][15];
__int64 num[(1<<14)+2][15][15];
__int64 sum,ret,ans;
int main()
{
int i,j,n,T,k,l,m,x,y,p,tag;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
sum=0;
for (i=0;i<n;i++)
{
scanf("%d",&val[i]);
sum+=val[i];
}
memset(map,0,sizeof(map));
for (i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
x--;
y--;
map[x][y]=1;
map[y][x]=1;
}
if (n==1)
{
printf("%d %d
",val[0],1);
continue;
}
memset(dp,-1,sizeof(dp));
memset(num,0,sizeof(num));
dp[0][0][0]=0;
for (i=0;i<(1<<n);i++)
{
for (j=0;j<n;j++)
{
if (i==0)
{
for (k=0;k<n;k++)
{
for (l=0;l<n;l++)
{
if (k==l) continue;
if (map[k][l]==0) continue;
dp[(1<<k) | (1<<l)][l][k]=val[k]*val[l];
num[(1<<k) | (1<<l)][l][k]=map[k][l];
}
}
continue;
}
for (k=0;k<n;k++)
{
if (dp[i][j][k]==-1) continue;
// printf("%d...%d...%d..%d...%d..
",i,j,k,dp[i][j][k],num[i][j][k]);
for (l=0;l<n;l++)
{
if ((i & (1<<l))!=0) continue;
p=(i | (1<<l));
if (map[j][l]==0) continue;
if (dp[p][l][j]==-1)
{
dp[p][l][j]=dp[i][j][k]+val[j]*val[l];
num[p][l][j]=num[i][j][k];
if (map[l][k]!=0) dp[p][l][j]+=val[j]*val[l]*val[k];
}
else
{
tag=dp[i][j][k]+val[j]*val[l];
if (map[l][k]!=0) tag+=val[j]*val[l]*val[k];
if (dp[p][l][j]==tag)
{
num[p][l][j]+=num[i][j][k];
}
if (dp[p][l][j]<tag)
{
num[p][l][j]=num[i][j][k];
dp[p][l][j]=tag;
}
}
}
}
}
}
ans=0;
ret=0;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if (ans<dp[(1<<n)-1][i][j])
{
ans=dp[(1<<n)-1][i][j];
ret=num[(1<<n)-1][i][j];
}
else if (ans==dp[(1<<n)-1][i][j])
{
ret+=num[(1<<n)-1][i][j];
}
}
}
if (ret==0) printf("0 0
");
else printf("%I64d %I64d
",ans+sum,ret/2);
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
cocos2d Lua 학습(一)ios에서 루아 함수 호출 및 전참 방법 lua 코드: 출력 결과: lua 호출 C++ 방법: add 함수: lua 코드: 출력 결과: 함수를 호출합니다. 함수를 호출하려면 다음 협의를 따르십시오. 우선, 호출할 함...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.