[POJ 2288] Islands and Bridges【状压DP】

  • 2018-01-23
  • 0
  • 0

Problem:

Time Limit: 4000MS Memory Limit: 65536K

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

Solution:

本题是经典的状态压缩 DP(话说我刚开始还打了记忆化搜索,结果 T 掉了。。)

我们用 dp[s][i][j] 表示已访问点状态为 s,当前点为 i,前驱点为 j 时的最大权值cnt[s][i][j] 表示最大权值下的路径条数,则

  • 边界条件:dp[ (1 << i) + (1 << j) ][ i ][ j ] = v[ i ] + v[ j ]cnt[ (1 << i) + (1 << j) ][ i ][ j ] = 1,其中 (j,  i) ∈E
  • 转移方程:
    • 对于 (j, i), (k, j) ∈ E,包含点 i 的状态 s 有
      1. 前驱状态 s' = s ^ 1 << i
      2. 新增权值 dv = v[ i ] + v[ i ] * v[ j ] + ((k, i) ∈ E ? v[ i ] * v[ j ] * v[ k ] : 0)
    • dp[ s ][ i ][ j ] = max{dp[ s ][ i ][ j ], dp[ s' ][ j ][ k ] + dv},cnt 同步更新即可。

注意 n == 1 时需要特判,以及转移时不要忘记判断是否有边。

最大的数据需要开 long long 才能过。

Code: O(2nn3q) [29556K, 610MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

int q, n, m, S;
int v[15];
bool ed[15][15];
ll dp[8200][15][15], cnt[8200][15][15];

int main(){
	scanf("%d", &q);
	while(q--){
		scanf("%d%d", &n, &m), S = 1 << n;
		for(register int i = 0; i < n; i++) scanf("%d", v + i);
		if(n == 1){
			printf("%d 1\n", v[0]);
			continue;
		}  // Special judge for only one island
		memset(ed, 0, sizeof(ed));
		for(register int i = 0; i < m; i++){
			int u, v;
			scanf("%d%d", &u, &v), u--, v--;
			ed[u][v] = ed[v][u] = 1;
		}
		memset(dp, 0xc0, sizeof(dp));
		for(register int i = 0; i < n; i++)
			for(register int j = 0; j < n; j++)
				if(ed[j][i]){
					dp[(1 << i) + (1 << j)][i][j] = v[i] + v[j] + v[i] * v[j];
					cnt[(1 << i) + (1 << j)][i][j] = 1;
				}  // Boundary condition
		for(register int s = 0; s < S; s++)
			for(register int i = 0; i < n; i++)
				if(s & 1 << i)
					for(register int j = 0; j < n; j++){
						int s_ = s ^ 1 << i;
						if(ed[j][i] && s_ & 1 << j)  // Don't forget to check the existence of edge j -> i
							for(register int k = 0; k < n; k++)
								if(ed[k][j] && s_ & 1 << k){
									ll nv = dp[s_][j][k] + v[i] + v[i] * v[j] + (ed[i][k] ? v[i] * v[j] * v[k] : 0);
									if(nv > dp[s][i][j]) dp[s][i][j] = nv, cnt[s][i][j] = cnt[s_][j][k];
									else if(nv == dp[s][i][j]) cnt[s][i][j] += cnt[s_][j][k];
								}
					}
		ll mxdp = 0xc0c0c0c0c0c0c0c0, mxcnt;
		for(register int i = 0; i < n; i++)
			for(register int j = 0; j < n; j++)
				if(dp[S - 1][i][j] > mxdp) mxdp = dp[S - 1][i][j], mxcnt = cnt[S - 1][i][j];
				else if(dp[S - 1][i][j] == mxdp) mxcnt += cnt[S - 1][i][j];
		if(mxdp < 0) puts("0 0");
		else printf("%lld %lld\n", mxdp, mxcnt >> 1);
	}
	return 0;
}

评论

还没有任何评论,你来说两句吧



新博客地址: darkleafin.cf

常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
Please refresh the page if the code cannot be displayed normally.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai