POJ 2288 Islands and Bridges


Islands and Bridges
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 10589 Accepted: 2765

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. 

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

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

题意:n个点,m条边,求权值最大的哈密顿路径,相邻点要加上点权值乘积,成环要加环乘积。

思路:其实是一道很简单的状压dp,但是小细节比较多,首先题目要统计的权值不超int但路径数是会超int的。然后就是一个点的情况特判,最后因为同一条路径可以有两种走法,所以最后路径数要/2,剩下的就是常规的状压dp解决哈密顿路径方法。下面给代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<bitset>
#include <utility>
using namespace std;
#define maxn 13
#define inf 0x3f3f3f3f
typedef long long LL;
const int mod = 1e9 + 7;
int value[maxn], dp[1 << maxn][maxn][maxn], vis[maxn][maxn];
LL num[1 << maxn][maxn][maxn];
int main(){
	int t;
	scanf("%d", &t);
	while (t--){
		int n, m;
		memset(dp, 0, sizeof(dp));
		memset(vis, 0, sizeof(vis));
		memset(num, 0, sizeof(num));
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++){
			scanf("%d", &value[i]);
		}
		while (m--){
			int x, y;
			scanf("%d%d", &x, &y);
			x--, y--;
			vis[x][y] = vis[y][x] = 1;
		}
		if (n == 1){
			printf("%d 1\n", value[0]);
			continue;
		}
		for (int i = 0; i < n; i++){
			for (int j = i + 1; j < n; j++){
				if (vis[i][j]){
					dp[(1 << i) | (1 << j)][i][j] = dp[(1 << i) | (1 << j)][j][i] = value[i] + value[j] + value[i] * value[j];
					num[(1 << i) | (1 << j)][i][j] = num[(1 << i) | (1 << j)][j][i] = 1;
				}
			}
		}
		for (int i = 3; i < (1 << n); i++){
			for (int j = 0; j < n; j++){
				if ((1 << j)&i){
					for (int k = 0; k < n; k++){
						if (k == j)
							continue;
						if ((1 << k)&i&&dp[i][j][k]){
							for (int l = 0; l < n; l++){
								if (!((1 << l)&i) && vis[k][l]){
									int sum = dp[i][j][k] + value[l] + value[k] * value[l];
									if (vis[l][j]){
										sum += value[j] * value[k] * value[l];
									}
									if (sum>dp[i | (1 << l)][k][l]){
										dp[i | (1 << l)][k][l] = sum;
										num[i | (1 << l)][k][l] = num[i][j][k];
									}
									else if (sum == dp[i | (1 << l)][k][l]){
										num[i | (1 << l)][k][l] += num[i][j][k];
									}
								}
							}
						}
					}
				}
			}
		}
		LL ans = 0;
		int maxnum = 0;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				if (!vis[i][j])
					continue;
				if (maxnum < dp[(1 << n) - 1][i][j]){
					maxnum = dp[(1 << n) - 1][i][j];
					ans = num[(1 << n) - 1][i][j];
				}
				else if (maxnum == dp[(1 << n) - 1][i][j]){
					ans += num[(1 << n) - 1][i][j];
				}
			}
		}
		printf("%d %lld\n", maxnum, ans >> 1);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值