PKU 2288 Islands and Bridges 状态dp
题意:
给你一张地图,上面有一些岛和桥。你要求出最大的三角哈密顿路径,以及他们的数量。
哈密顿路:一条经过所有岛的路径,每个岛只经过一次。
最大三角哈密顿路:满足价值最大的哈密顿路。
价值计算分为以下三部分:
1. 所有点权的和。
2. 对于路径上任意两个连续的点(共享一条边)的点权乘积的和。
3. 对于路径上任意三个连续的点,如果他们构成一个三角形(两两之间有边),那么加上三点点权的乘积
思路:
状态压缩动态规划, dp[st][i][j] 表示状态是st, 前一步在i,现在停在j的最大价值。
cnt[st][i][j] 表示计数。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef __int64 ll; const int MAXN= ; ll dp[<<][MAXN][MAXN];
ll cnt[<<][MAXN][MAXN]; int G[][];
ll val[];
int n, m; void print() {
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++)
printf("%d ", G[i][j]);
puts("");
}
} int main() {
#ifdef Phantom01
freopen("PKU2288.txt", "r", stdin);
#endif // Phantom01 int T;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &n, &m);
memset(dp, , sizeof(dp));
memset(cnt, , sizeof(cnt));
memset(G, , sizeof(G));
for (int i = ; i < n; i++) {
scanf("%I64d", &val[i]);
}
for (int i = ; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u-][v-] += ;
G[v-][u-] += ;
}
//print();
if (==n) {
printf("%I64d 1\n", val[]);
continue;
} for (int i = ; i < n; i++)
for (int j = ; j < n; j++) if (G[i][j]){
dp[(<<i)|(<<j)][i][j] = val[i] + val[j] + val[i]*val[j];
cnt[(<<i)|(<<j)][i][j] += G[i][j];
} for (int i = ; i < (<<n)-; i++)
for (int j = ; j < n; j++) if (i&(<<j))
for (int u = ; u < n; u++) if ((i&(<<u)) && (j!=u) && cnt[i][j][u])
for (int v = ; v < n; v++) if (G[u][v] && !(i&(<<v))) {
ll &now = dp[i][j][u];
ll &next = dp[i|(<<v)][u][v];
ll va = val[v]*( + val[u]);
if (G[j][v]) va += val[j]*val[u]*val[v];
if (next < now+va) {
next = now+va;
cnt[i|(<<v)][u][v] = cnt[i][j][u];
} else if (next==now+va)
cnt[i|(<<v)][u][v] += cnt[i][j][u];
} ll ans = , c = ;
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)
if (ans<dp[(<<n)-][i][j]) {
ans = dp[(<<n)-][i][j];
c = cnt[(<<n)-][i][j];
} else if (ans==dp[(<<n)-][i][j])
c += cnt[(<<n)-][i][j]; printf("%I64d %I64d\n", ans, c/);
} return ;
}
PKU2288
P.s.: 开始多开了一维导致MLE,后来发现读错题了 0 0 结果花了一晚上
PKU 2288 Islands and Bridges 状态dp的更多相关文章
- POJ 2288 Islands and Bridges(状压dp)
http://poj.org/problem?id=2288 题意: 有n个岛屿,每个岛屿有一个权值V,一条哈密顿路径C1,C2,...Cn的值为3部分之和: 第1部分,将路径中每个岛屿的权值累加起来 ...
- POJ 2288 Islands and Bridges (状压DP,变形)
题意: 给一个无向图,n个点m条边,每个点有点权,要求找到一条哈密顿路径,使得该路径的f(path)值最大.输出f值,若有多条最大f值的路径,输出路径数量. f值由如下3点累加而来: (1)所有点权之 ...
- poj 2288 Islands and Bridges ——状压DP
题目:http://poj.org/problem?id=2288 状压挺明显的: 一开始写了(记忆化)搜索,但一直T: #include<iostream> #include<cs ...
- poj 2288 Islands and Bridges (状压dp+Tsp问题)
这道题千辛万苦啊! 这道题要涉及到当前点和前面两个点,那就设dp[state][i][j]为当前状态为state,当前点为i,前一个点为j 这个状态表示和之前做炮兵那题很像,就是涉及到三个点时,就多设 ...
- poj 2288 Islands and Bridges——状压dp(哈密尔顿回路)
题目:http://poj.org/problem?id=2288 不知为什么记忆化搜索就是WA得不得了! #include<iostream> #include<cstdio> ...
- POJ 2288 Islands and Bridges(状压DP)题解
题意:n个点,m有向边,w[i]表示i的价值,求价值最大的哈密顿图(只经过所有点一次).价值为:所有点的w之和,加上,每条边的价值 = w[i] * w[j],加上,如果连续的三个点相互连接的价值 = ...
- poj 2288 Islands and Bridges_状态压缩dp_哈密尔顿回路问题
题目链接 题目描述:哈密尔顿路问题.n个点,每一个点有权值,设哈密尔顿路为 C1C2...Cn,Ci的权值为Vi,一条哈密尔顿路的值分为三部分计算: 1.每一个点的权值之和 2.对于图中的每一条CiC ...
- 【以前的空间】poj 2288 Islands and Bridges
一个不错的题解 : http://blog.csdn.net/accry/article/details/6607703 这是一道状态压缩.每个点有一个值,我们最后要求一个最值sum.sum由三部分组 ...
- poj 2288 Islands and Bridges
题意: 给你一个双向连通图,求 获得权值最大 的 哈密顿通路的 权值 和 这个权值对应的数目: 其中权值计算方法是 列如 ABCD 权值是a+b+c+d+ab+bc+cd 如果 A,B,C 和B ...
随机推荐
- php 添加redis扩展
我主要是按照http://blog.163.com/fan_xy_qingyuan/blog/static/1889877482014111111283265/ 这篇博客来的,但是这篇博客里只有php ...
- pthread 的 api 分类
pthreads defines a set of C programming language types, functions and constants. It is implemented w ...
- vue 键盘监听事件
<template> <div class="hello"> <input v-on:keyup.enter="submit" t ...
- for 的相关用法
forEach() <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- VB创建文件夹
If Dir("D:\Program Files\AutoCAD 2006\Express\", vbDirectory) = "" Then '判断文件夹是否 ...
- springboot ajax返回html
因为拦截器 或者是 shiro 拦截登陆接口
- redis.conf配置文件配置项解析
知识来源于 : https://blog.csdn.net/bsfz_2018/article/details/79061413[Redis在linux下的安装] daemonize:如需要在后台运行 ...
- WPF 内部的5个窗口之 MediaContextNotificationWindow
原文:WPF 内部的5个窗口之 MediaContextNotificationWindow 本文告诉大家在 WPF 内部的5个窗口的 MediaContextNotificationWindow 是 ...
- Unity 制作安装程序和卸载程序
1.最简单的方式通过winrar制作 但是做出来的页面好low的感觉 参考链接:https://www.cnblogs.com/fetty/p/5185913.html 2.通过inno制作安装程序: ...
- 动态语言切换(续)-designer中的retranslateUi(带源码)
本站所有文章由本站和原作者保留一切权力,仅在保留本版权信息.原文链接.原文作者的情况下允许转载,转载请勿删改原文内容, 并不得用于商业用途. 谢谢合作.原文链接:动态语言切换(续)-designer中 ...