题意:n个点,m有向边,w[i]表示i的价值,求价值最大的哈密顿图(只经过所有点一次)。价值为:所有点的w之和,加上,每条边的价值 = w[i] * w[j],加上,如果连续的三个点相互连接的价值 = w[i] * w[j] * w[k]。不存在输出0 0。n <= 13。

思路:dp[state][i][j]表示state状态下,最后两个为i,j。

代码:

#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-8;
const int maxn = 15 + 10;
const int M = maxn * 30;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 1e4 + 7;
int w[maxn];
int n, m;
int g[maxn][maxn];
int dp[(1 << 13) + 10][maxn][maxn];
ll way[(1 << 13) + 10][maxn][maxn];
void solve(){
memset(dp, -1, sizeof(dp));
memset(way, 0, sizeof(way));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i == j) continue;
if(g[i][j] == INF) continue;
dp[(1 << i) | (1 << j)][i][j] = w[i] + w[j] + g[i][j];
way[(1 << i) | (1 << j)][i][j]++;
}
}
for(int t = 0; t < (1 << n) - 1; t++){
for(int i = 0; i < n; i++){
if(!((1 << i) & t)) continue;
for(int j = 0; j < n; j++){
if(!((1 << j) & t)) continue;
if(g[i][j] == INF) continue;
if(dp[t][i][j] == -1) continue;
for(int k = 0; k < n; k++){
if((1 << k) & t) continue;
if(g[j][k] == INF) continue;
ll ret = dp[t][i][j] + w[k] + g[j][k];
if(g[i][k] != INF) ret += w[i] * w[j] * w[k];
if(dp[(1 << k) | t][j][k] < ret){
dp[(1 << k) | t][j][k] = ret;
way[(1 << k) | t][j][k] = way[t][i][j];
}
else if(dp[(1 << k) | t][j][k] == ret){
way[(1 << k) | t][j][k] += way[t][i][j];
}
}
}
}
}
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++)
scanf("%d", &w[i]);
memset(g, INF, sizeof(g));
for(int i = 0; i < m; i++){
int u, v;
scanf("%d%d", &u, &v);
u--, v--;
g[u][v] = g[v][u] = w[u] * w[v];
}
if(n == 1){
printf("%d 1\n", w[0]);
continue;
}
solve();
ll ans = 0, num = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(i == j) continue;
if(g[i][j] == INF) continue;
if(dp[(1 << n) - 1][i][j] > ans){
ans = dp[(1 << n) - 1][i][j];
num = way[(1 << n) - 1][i][j];
}
else if(dp[(1 << n) - 1][i][j] == ans){
num += way[(1 << n) - 1][i][j];
}
}
}
printf("%lld %lld\n", ans, num / 2);
}
return 0;
}
/*
3
3 1
2 2 2
1 2
*/

POJ 2288 Islands and Bridges(状压DP)题解的更多相关文章

  1. poj 2288 Islands and Bridges ——状压DP

    题目:http://poj.org/problem?id=2288 状压挺明显的: 一开始写了(记忆化)搜索,但一直T: #include<iostream> #include<cs ...

  2. poj 2288 Islands and Bridges——状压dp(哈密尔顿回路)

    题目:http://poj.org/problem?id=2288 不知为什么记忆化搜索就是WA得不得了! #include<iostream> #include<cstdio> ...

  3. [poj2288] Islands and Bridges (状压dp)

    Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...

  4. POJ 1185 炮兵阵地(状压DP)

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26426   Accepted: 10185 Descriptio ...

  5. POJ 2411 Mondriaan's Dream -- 状压DP

    题目:Mondriaan's Dream 链接:http://poj.org/problem?id=2411 题意:用 1*2 的瓷砖去填 n*m 的地板,问有多少种填法. 思路: 很久很久以前便做过 ...

  6. POJ 2411 Mondriaan's Dream ——状压DP 插头DP

    [题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出 ...

  7. 【POJ 2923】Relocation(状压DP+DP)

    题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...

  8. POJ 1185 炮兵阵地 (状压DP)

    题目链接 题意 : 中文题不详述. 思路 :状压DP,1表示该位置放炮弹,0表示不放.dp[i][j][k],代表第 i 行的状态为k时第i-1行的状态为 j 时放置的最大炮弹数.只是注意判断的时候不 ...

  9. 动态规划晋级——POJ 3254 Corn Fields【状压DP】

    转载请注明出处:http://blog.csdn.net/a1dark 分析:刚开始学状压DP比较困难.多看看就发现其实也没有想象中那么难.这道题由于列数较小.所以将行压缩成二进制来看.首先处理第一行 ...

  10. POJ 1185 炮兵阵地 【状压DP】

    <题目链接> 题目大意: 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平 ...

随机推荐

  1. Django QuerySet API---数据库接口

    基本的创建与查询 # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import T ...

  2. 我教你如何解决 Docker 下载 mcr.microsoft.com 镜像慢的办法

    我教你如何解决 Docker 下载 mcr.microsoft.com 镜像慢的办法 一.介绍 最近,我在写有关使用 Jenkins 搭建企业级持续集成环境的文章,准备了四台服务器,企业级别嘛,一台就 ...

  3. 风险识别系统-大数据智能风控管理平台-企业风控解决方案– 阿里云 https://www.aliyun.com/product/saf

    风险识别系统-大数据智能风控管理平台-企业风控解决方案– 阿里云 https://www.aliyun.com/product/saf

  4. 数据水印 watermark

    外发数据创建水印 产品通过对外发数据进行添加数据标记.自动生成水印.数据源追溯等功能,避免了内部人员外发数据泄露无法对事件追溯,提高了数据传递的安全性和可追溯能力. 数据水印系统_数据安全管理工具_[ ...

  5. memory ordering 内存排序

    Memory ordering - Wikipedia https://en.wikipedia.org/wiki/Memory_ordering https://zh.wikipedia.org/w ...

  6. MySQL 高性能优化规范建议

    数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用 MySQL 保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名 ...

  7. springboot2.2.2集成6.5 Elasticsearch

    1.0POM文件 <!-- spring-boot --> <dependency> <groupId>org.springframework.boot</g ...

  8. 单体架构、SOA架构、微服务架构

  9. centos 7_本地源制作

    1.安装工具 yum install yum-utils createrepo yum-plugin-priorities   2.自己创建一个阿里源 vim /etc/yum.repos.d/ope ...

  10. ajax 用fom提交

    $.ajax({ type : "POST", url : "${ctx}/credit/LoanauditCtrl/qwe.do?hetong="+heton ...