Islands and Bridges
Time Limit: 4000MS | Memory Limit: 65536K | |
---|---|---|
Total Submissions: 13134 | Accepted: 3487 |
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 ViVi+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 ViVi+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
链接
http://poj.org/problem?id=2288
题意:
给出n个点,m条边。每个点有一个权值w。找出一条汉密尔顿路径,使它的值最大。一条汉密尔顿路径的值由三部分组成:
(1) 路径上每个点的权值之和
(2) 路径上每条边u-v,将其权值的积累加起来。即w[u]*w[v]
(3) 如果三个点形成一个三角形,例如i、i+1、i+2,那么将w[i]*w[i+1]*w[i+2]累加起来
一条汉密尔顿路径可能包含多个三角形,一张图中也可能包含多个最好的汉密尔顿路径。输出最大的汉密尔顿路径的值,以及这样的汉密尔顿路径的个数。同一条汉密尔顿路径的两种走法算作一种。
思路:
很明显,此题用二进制状态压缩+DP。
此题和https://www.acwing.com/problem/content/93/很像。
因为本题需要看三条边是否联通,所以需要用一个三维数组。
DP[p][i][j]代表当前状态p(通过的点为1,否则为0,二进制状态压缩),j代表前一个拜访的点,i代表前前个拜访的点。
最终状态为DP[(1<<n)-1][i][j], i和j必须要联通。
初始化
DP[(1<<i)|(1<<j)][i][j] = a[i] + a[j] + a[i]*a[j]; //把每条边先初始化。
way[(1<<i)|(1<<j)][i][j] = 1; //此时路径为1条
状态转移方程
状态DP[p][i][j]后的下一个状态为DP[p|1<<k][k][j]
DP[p|1<<k][j][k] = max{DP[p|1<<k][j][k], DP[p][i][j] + a[k] + a[k]*a[j] (+ a[三角形])}
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 14;
inline int read() {
int s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * f;
}
int n, m;
int a[maxn];
int dp[1<<maxn][maxn][maxn];
bool vis[maxn][maxn];
ll way[1<<maxn][maxn][maxn];
void init()
{
memset(dp,-1, sizeof(dp));
memset(vis, false, sizeof(vis));
memset(way, 0, sizeof(way));
}
void solve()
{
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i != j && vis[i][j]) {
dp[1<<i | 1<<j][i][j] = a[i] + a[j] + a[i]*a[j];
way[1<<i | 1<<j][i][j] = 1;
}
}
}
for(int p = 0; p < (1<<n); p++) {
for(int i = 0; i < n; i++) {
if(p & 1<<i) {
for(int j = 0; j < n; j++) {
if((p & 1 << j) && vis[i][j] && dp[p][i][j] != -1) {
for(int k = 0; k < n; k++) {
if(vis[j][k] && (!(p & 1 << k)) && k != i) {
ll q = dp[p][i][j] + a[k] + a[k]*a[j];
if(vis[i][k]) {
q += a[k]*a[j]*a[i];
}
if(q > dp[p|(1<<k)][j][k]) {
dp[p|(1<<k)][j][k] = q;
way[p|(1<<k)][j][k] = way[p][i][j];
}
else if(q == dp[p|(1<<k)][j][k]) {
// dp[p|(1<<k)][j][k] = q;
way[p|(1<<k)][j][k] += way[p][i][j];
}
}
}
}
}
}
}
}
}
int main()
{
int t;
t = read();
while(t--) {
n = read();m=read();
init();
int x, y;
for(int i = 0; i < n; i++)
a[i] = read();
if(n == 1) {
printf("%d 1\n", a[0]);
continue;
}
for(int i = 0; i < m; i++) {
x = read();
y = read();
x--;
y--;
vis[x][y] = vis[y][x] = true;
}
solve();
int maxx = 0;
ll ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i != j && vis[i][j]) {
if(maxx < dp[(1<<n)-1][i][j]){
maxx = dp[(1<<n)-1][i][j];
ans = way[(1<<n)-1][i][j];
}
else if(maxx == dp[(1<<n)-1][i][j]) {
ans += way[(1<<n)-1][i][j];
}
}
}
}
printf("%d %lld\n", maxx, ans/2);
}
return 0;
}
参考博客: