poj 2288 Islands and Bridges(状压dp,好题)
来源:互联网 发布:大鱼大肉知乎 编辑:程序博客网 时间:2024/05/05 00:09
Description
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
Output
Note: A path may be written down in the reversed order. We still think it is the same path.
Sample Input
23 32 2 21 22 33 14 61 2 3 41 21 31 42 32 43 4
Sample Output
22 369 1
Source
题意:给n个点和m条边(双向边),每个点有一个权值Vi,找一条哈密顿路径(只经过每个点一次),路径的权值来自三条:1 路径上的Vi之和 2 所有相邻点对ij的Vi*Vj之和 3 相邻连续三点i,j,k (并且三点要构成三角形)Vi*Vj*Vk之和。
题解:
这是一道典型的利用状态压缩dp求最优Hamilton回路的题目。
取d[state][i][j]表示state状态下倒数第二个岛为i,最后一个岛为j时的最优解,cnt[state][i][j]为相应的路径数目,其中state的二进制表示的i位为1表示岛i被访问过,反之为0。
则显然当有边(i,j)存在时,有如下初值可赋:
d[(1<<i)+(1<<j)][i][j]=val[i]+val[j]+val[i]*val[j],cnt[(1<<i)+(1<<j)][i][j]=1。
如果状态(state,i,j)可达,检查岛k,如果此时k没有被访问过并且有边(j,k)存在,则做如下操作:
1)设tmp为下一步访问岛k时获得的总利益,r=state+(1<<k)。
2)如果tmp>d[r][j][k],表示此时可以更新到更优解,则更新:
d[r][j][k]=tmp,cnt[r][j][k]=cnt[state][i][j]。
3)如果tmp==d[r][j][k],表示此时可以获得达到局部最优解的更多方式,则更新:
cnt[r][j][k]+=cnt[state][i][j]。
最后检查所有的状态((1<<n)-1,i,j),叠加可以得到最优解的道路数。
需要注意的是,题目约定一条路径的两种行走方式算作一种,所以最终结果要除2。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int MAXN=13;typedef long long ll;ll d[1<<MAXN][MAXN][MAXN],cnt[1<<MAXN][MAXN][MAXN];ll val[MAXN];int map[MAXN][MAXN];int main(){int cas;scanf("%d",&cas);while(cas--){int n,m;scanf("%d%d",&n,&m);memset(map,0,sizeof(map));for(int i=0;i<n;i++) scanf("%I64d",&val[i]);while(m--){int p,q;scanf("%d%d",&p,&q);p--,q--;map[p][q]=map[q][p]=1;}if(n==1){printf("%I64d 1\n",val[0]);continue;}memset(d,-1,sizeof(d));memset(cnt,0,sizeof(cnt));for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(i!=j&&map[i][j]) { d[(1<<i)|(1<<j)][i][j]=val[i]+val[j]+val[i]*val[j]; cnt[(1<<i)|(1<<j)][i][j]=1; } int tot=(1<<n)-1;for(int i=0;i<=tot;i++) for(int j=0;j<n;j++) if(i&(1<<j)) for(int k=0;k<n;k++) if(j!=k&&map[j][k]&&d[i][j][k]!=-1&&(i&(1<<k))) for(int x=0;x<n;x++) if(j!=x&&k!=x&&map[k][x]&&(i&(1<<x))==0)//注意不能写成if(i&(1<<x)==0),位运算符优先级很低。。。 { ll tmp=d[i][j][k]+val[x]+val[k]*val[x]; if(map[j][x]) tmp+=val[j]*val[k]*val[x]; if(tmp>d[i|(1<<x)][k][x]) { d[i|(1<<x)][k][x]=tmp; cnt[i|(1<<x)][k][x]=cnt[i][j][k]; } else if(tmp==d[i|(1<<x)][k][x]) { cnt[i|(1<<x)][k][x]+=cnt[i][j][k]; } }ll ans1=0,ans2=0;for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(i!=j) { if(ans1<d[tot][i][j]) { ans1=d[tot][i][j]; ans2=cnt[tot][i][j]; } else if(ans1==d[tot][i][j]) ans2+=cnt[tot][i][j]; }printf("%I64d %I64d\n",ans1,ans2/2); //两个完全逆向的路径可看成一条 }}
- poj 2288 Islands and Bridges(状压dp,好题)
- Islands and Bridges - POJ 2288 状压dp
- 状压DP poj 2288 Islands and Bridges
- poj 2288 Islands and Bridges(状压dp)
- poj 2288 Islands and Bridges(状压dp)
- poj 2288 Islands and Bridges 状压dp
- poj 2288 Islands and Bridges 状压dp
- POJ 2288 Islands and Bridges(状压dp)
- POJ 2288 Islands and Bridges (状压DP)
- POJ 2288 Islands and Bridges 状压DP
- poj 2282 Islands and Bridges(状压DP)
- POJ 2288 Islands and Bridges(状压dp)
- POJ 2288 Islands and Bridges(状压DP)
- POJ 2288 Islands and Bridges - 状压dp【TSP】
- POJ 2288 Islands and Bridges(状压dp)
- POJ 2288-Islands and Bridges (状压DP)
- POJ 2288 Islands and Bridges (状压DP)
- poj 2288 Islands and Bridges
- bic&orr
- JAVA基础6.37——静态的应用范围
- 7.5.2 基数排序
- C++ 学习笔记(五):高级编程:文件和流,异常处理,动态内存,命名空间
- python大盘点:全局变量、局部变量、类变量、实例变量
- poj 2288 Islands and Bridges(状压dp,好题)
- C语言基础学习基本数据类型-字符专属的输入输出函数
- 32位操作系统寻址范围与支持的最大内存空间
- 软件文档简介
- PHP 单例设计模式
- 演示Calender,日历
- SourceInsight高亮度显示查找字符串
- Easyui Datagid导出Excel
- 回归和分类区别,及模型的选择