(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
题意:传送门
原题目描述在最下面。
最多13个点,点权为vi
一条边的贡献的两个点点权乘积
三角形的贡献是三个点点权乘积
求权值最大的哈密顿路径值,并求此权值下有多少种路线
思路:
状态表示:
dp[sta][i][j] 表示状态sta(二进制下为1表示走过0表示未走过)最后经过边j->i的最大花费。
way[sta][i][j]表示此花费下的路径数量。
初始化:
dp[(1 << i) + (1 << j)] = ari a r i + arj a r j + ari a r i * arj a r j
状态转移:
news经过点 i, j, k,old经过点 j, k
temp = dp[olds][j][k] + ari a r i + ari a r i * arj a r j
if(i 联通 k) temp += ari a r i * arj a r j * ark a r k
dp[news][i][j] = max(temp, dp[news][i][j])
AC代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<assert.h>
#include<bitset>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int N = 9000;
int n, m;
int mp[15][15], ar[15];
LL dp[N][14][14], way[N][14][14];
int main(){
#ifndef ONLINE_JUDGE
freopen("E://ADpan//in.in", "r", stdin);
freopen("E://ADpan//out.out", "w", stdout);
#endif
int tim;
scanf("%d", &tim);
while(tim--){
scanf("%d%d", &n, &m);
memset(mp, 0, sizeof(mp));
for(int i = 0; i < n; ++i){
scanf("%d", &ar[i]);
}
for(int i = 0, u, v; i < m; ++i){
scanf("%d%d", &u, &v);
u--;v--;
mp[u][v] = mp[v][u] = 1;
}
if(n == 1){
printf("%d 1\n", ar[0]);
continue;
}
memset(dp, -1, sizeof(dp));
memset(way, 0, sizeof(way));
int state = 1<<(n);//初始化边界条件
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(i==j||mp[i][j]==0)continue;
int tmp = (1<<(i))+(1<<(j));
dp[tmp][i][j] = ar[i]+ar[j]+ar[i]*ar[j];
way[tmp][i][j] = 1;
}
}
for(int s = 0; s < state; ++s){
for(int i = 0; i < n; ++i){
if((s&(1<<(i)))==0)continue;
for(int j = 0; j < n; ++j){
if((s&(1<<(j)))==0||i==j||mp[i][j]==0)continue;
for(int k = 0; k < n; ++k){
if(i==k||j==k||(s&(1<<(k)))==0)continue;
if(mp[j][k] == 0)continue;
int tmp = s^(1<<(i));
if(dp[tmp][j][k] == -1)continue;
LL temp = ar[i]+ar[i]*ar[j]+dp[tmp][j][k];
if(mp[i][k]){
temp += ar[i]*ar[j]*ar[k];
}
if(temp > dp[s][i][j]){
dp[s][i][j] = temp;
way[s][i][j] = way[tmp][j][k];
}else if(temp == dp[s][i][j]){
way[s][i][j] += way[tmp][j][k];
}
}
}
}
}
LL ans = -1, nums = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(i == j||mp[i][j] == 0)continue;
if(dp[state-1][i][j] > ans){
ans = dp[state-1][i][j];
nums = way[state-1][i][j];
}else if(dp[state-1][i][j] == ans){
nums += way[state-1][i][j];
}
}
}
if(ans==-1){
printf("0 0\n");
}else printf("%lld %lld\n", ans, nums/2);
}
return 0;
}
原题目描述:
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
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