POJ 2288 Islands and Bridges (状压dp)

POJ 2288 Islands and Bridges (状压dp)

西安EC Final 之后,我第一学年的ICPC-CCPC之旅就算结束了。一年的比赛暴露了自己太多的问题,希望自己能坚持写博客来提升自己的能力,也为了督促自己备战2021-2022赛季的比赛。

原题面

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 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.

输入

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.

输出

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.

样例输入

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

样例输出

22 3
69 1

题意解释

给出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(数据只有13呀),第一维就肯定得维护已经走的岛屿的情况咯,因为走到的下一个岛屿所加上的权值只与前一个岛屿和前第二个岛屿有关,所以我们必须开2维分别记录前一个岛屿和前第二个岛屿是哪个岛屿。状态转移方程如下

// i表示状态压缩之后的已经到的所有岛屿的情况
// j表示上一个到的岛屿,也可以理解为现在所在的岛屿
// k表示上一个岛屿之前的岛屿。
// vv表示下一个我们要去的岛屿
dp[i|(1<<vv-1)][j][vv]=max(dp[i|(1<<vv-1)][j][vv],dp[i][k][j])

注意:因为需要统计数量,所以在这里我犯了一个错误,必须到所有状态都记录完成再统计,不能说我所有岛屿都跑完了就统计,这样就会多统计!!!

题解代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
int t,n,m;
const int maxn=13+7;
int mapp[maxn][maxn];
void add(int u,int v){
	mapp[u][v]=1;
}
ll val[maxn];
ll dp[1<<13][17][17];
ll num[1<<13][17][17];
int main(){
	scanf("%d",&t);
	int cnts=0;
	while(t--){
//		cnts++;
//		n=cnts;
		scanf("%d%d",&n,&m);
		memset(mapp,0,sizeof(mapp));
		memset(dp,-1,sizeof(dp));
		memset(num,0,sizeof(num));
		memset(val,0,sizeof(val));
		for(int i=1;i<=n;i++){
			scanf("%d",&val[i]);
		}
//		for(int i=1;i<=n;i++){
//			for(int j=1;j<=n;j++) mapp[i][j]=1;
//		}
		while(m--){
			int xx,yy;
			scanf("%d%d",&xx,&yy);
			add(xx,yy);
			add(yy,xx);
		}
		int maxlen=(1<<n);
		int nowedges=0;
		dp[0][0][0]=0;
		num[0][0][0]=1;
		ll ans=-1;
		ll ansnum=0;
		for(int i=0;i<maxlen;i++){
			nowedges=0;
			int tmp=i;
			while(tmp){
				if(tmp&1) nowedges++;
				tmp/=2;
			}
			for(int ppre=0;ppre<=n;ppre++){
				if(ppre==0&&nowedges>=2) continue;
				int ppre_t=(1<<ppre-1);
				if(ppre!=0&&((ppre_t&i)==0)) continue;
				for(int pre=0;pre<=n;pre++){
					if(pre!=0&&pre==ppre) continue;
					if(pre==0&&nowedges>=1) continue;
					if(dp[i][ppre][pre]<0) continue;
					int pre_t=(1<<pre-1);
					if(pre!=0&&((pre_t&i)==0)) continue;
						for(int v=1;v<=n;v++){
							if(pre!=0&&mapp[pre][v]==0) continue;
							int vv=(1<<v-1);
							if(i&vv) continue; 
							ll tmpsum=dp[i][ppre][pre]+val[v]+val[v]*val[pre];
							if(mapp[ppre][v]) tmpsum+=val[v]*val[pre]*val[ppre];
							if(dp[i|vv][pre][v]<tmpsum){
								num[i|vv][pre][v]=num[i][ppre][pre];
								dp[i|vv][pre][v]=tmpsum;
							}else if(dp[i|vv][pre][v]==tmpsum){
								num[i|vv][pre][v]+=num[i][ppre][pre];
							}
						}
				}
			}
		}
		if(n==1){
			cout<<val[1]<<" 1\n";
			continue;
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(ans<dp[maxlen-1][i][j]){
					ans=dp[maxlen-1][i][j];
					ansnum=num[maxlen-1][i][j];
				}else if(ans==dp[maxlen-1][i][j]) ansnum+=num[maxlen-1][i][j];
			}
		} 
		if(ans==-1) cout<<"0 0\n";
		else cout<<ans<<" "<<ansnum/2<<endl;
	}
	
} 
/*
4
5 10
2 2 2 2 2
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
*/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值