POJ 2288 Islands and Bridges(状压DP)题解
题意: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)题解的更多相关文章
- poj 2288 Islands and Bridges ——状压DP
题目:http://poj.org/problem?id=2288 状压挺明显的: 一开始写了(记忆化)搜索,但一直T: #include<iostream> #include<cs ...
- poj 2288 Islands and Bridges——状压dp(哈密尔顿回路)
题目:http://poj.org/problem?id=2288 不知为什么记忆化搜索就是WA得不得了! #include<iostream> #include<cstdio> ...
- [poj2288] Islands and Bridges (状压dp)
Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...
- POJ 1185 炮兵阵地(状压DP)
炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26426 Accepted: 10185 Descriptio ...
- POJ 2411 Mondriaan's Dream -- 状压DP
题目:Mondriaan's Dream 链接:http://poj.org/problem?id=2411 题意:用 1*2 的瓷砖去填 n*m 的地板,问有多少种填法. 思路: 很久很久以前便做过 ...
- POJ 2411 Mondriaan's Dream ——状压DP 插头DP
[题目分析] 用1*2的牌铺满n*m的格子. 刚开始用到动规想写一个n*m*2^m,写了半天才知道会有重复的情况. So Sad. 然后想到数据范围这么小,爆搜好了.于是把每一种状态对应的转移都搜了出 ...
- 【POJ 2923】Relocation(状压DP+DP)
题意是给你n个物品,每次两辆车运,容量分别是c1,c2,求最少运送次数.好像不是很好想,我看了网上的题解才做出来.先用状压DP计算i状态下,第一辆可以运送的重量,用该状态的重量总和-第一辆可以运送的, ...
- POJ 1185 炮兵阵地 (状压DP)
题目链接 题意 : 中文题不详述. 思路 :状压DP,1表示该位置放炮弹,0表示不放.dp[i][j][k],代表第 i 行的状态为k时第i-1行的状态为 j 时放置的最大炮弹数.只是注意判断的时候不 ...
- 动态规划晋级——POJ 3254 Corn Fields【状压DP】
转载请注明出处:http://blog.csdn.net/a1dark 分析:刚开始学状压DP比较困难.多看看就发现其实也没有想象中那么难.这道题由于列数较小.所以将行压缩成二进制来看.首先处理第一行 ...
- POJ 1185 炮兵阵地 【状压DP】
<题目链接> 题目大意: 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平 ...
随机推荐
- USB过压保护芯片,高输入电压充电器(OVP)
PW2606B是一种前端过电压和过电流保护装置.它实现了广泛的输入电压范围从2.5VDC到40VDC.过电压阈值可在外部或外部编程设置为内部默认设置.集成功率路径nFET开关的低电阻确保了更好的性能电 ...
- Django - WebSocket:dwebsocket
Django - WebSocket:dwebsocket 什么是WebSocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器之间的数据交换 ...
- 1.Spring的基本应用
1.1概述 1.1.1 Spring是什么 Spring一个轻量级的框架,以IOC(控制反转)和AOP(面向切面编程)为内核,Spring在表现层提供了Spring MVC的框架整和功能,在业务逻辑层 ...
- 网络编程-I/O复用
I/O模型 Unix下可用的I/O模型有五种: 阻塞式I/O 非阻塞式I/O I/O复用(select和poll.epoll) 信号驱动式I/O(SIGIO) 异步I/O(POSIX的aio_系列函数 ...
- testng学习笔记-- beforesuit和aftersuit
一.定义 测试套件,主要关注执行顺序 套件:suit可以包含多个class 二.代码标签 三.运行结果
- PyPy CPython C++ connects programs written in C and C++ with a variety of high-level programming languages
PyPy 为什么会比 CPython 还要快? - 知乎 https://www.zhihu.com/question/19588346/answer/131977984 有个名词在现有的回答下面都没 ...
- pthon之变量
1.变量由三部分组成: 变量名 = 值 如:name = 'xiaohan' sex='男' age = 20 2.变量名的规范 2.1 变量名只能是字母,数字或下划线的任意组合 2 ...
- Mysql 5.5升级5.8
前言,因为升级跳板机,需要将mariadb 升级到10.2,也就是对应MySQL的5.8,废话不多说下面开始进行mariadb 5.5 的升级 Welcome to the MariaDB monit ...
- java切割~~百万 十万 万 千 百 十 个 角 分
/** * @param value * @return */ @SuppressWarnings("unused") public static void convertLoan ...
- 网际互连__TCP/IP三次握手和四次挥手
在TCP/IP协议中,TCP协议提供可靠的连接服务. 位码即tcp标志位,有6种标示: SYN(synchronous建立联机).ACK(acknowledgement 确认).PSH(push传送) ...