POJ 2288 Islands and Bridges

POJ 2288 Islands and Bridges (TSP 状态压缩DP) Islands and Bridges(POJ 2288) Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 11563 Accepted: 3034 Description Given a map of islands and bridges that connect these islands,... 阅读详情


Islands and Bridges
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 10589 Accepted: 2765

Description

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 CiC i+1 in the path, we add the product Vi*V i+1. And for the third part, whenever three consecutive islands CiC i+1C i+2 in the path forms a triangle in the map, i.e. there is a bridge between Ci and C i+2, we add the product Vi*V i+1*V i+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

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. 

Output

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.

Sample Input

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

Sample Output

22 3
69 1

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

题意:n个点,m条边,求权值最大的哈密顿路径,相邻点要加上点权值乘积,成环要加环乘积。

思路:其实是一道很简单的状压dp,但是小细节比较多,首先题目要统计的权值不超int但路径数是会超int的。然后就是一个点的情况特判,最后因为同一条路径可以有两种走法,所以最后路径数要/2,剩下的就是常规的状压dp解决哈密顿路径方法。下面给代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<bitset>
#include <utility>
using namespace std;
#define maxn 13
#define inf 0x3f3f3f3f
typedef long long LL;
const int mod = 1e9 + 7;
int value[maxn], dp[1 << maxn][maxn][maxn], vis[maxn][maxn];
LL num[1 << maxn][maxn][maxn];
int main(){
	int t;
	scanf("%d", &t);
	while (t--){
		int n, m;
		memset(dp, 0, sizeof(dp));
		memset(vis, 0, sizeof(vis));
		memset(num, 0, sizeof(num));
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++){
			scanf("%d", &value[i]);
		}
		while (m--){
			int x, y;
			scanf("%d%d", &x, &y);
			x--, y--;
			vis[x][y] = vis[y][x] = 1;
		}
		if (n == 1){
			printf("%d 1\n", value[0]);
			continue;
		}
		for (int i = 0; i < n; i++){
			for (int j = i + 1; j < n; j++){
				if (vis[i][j]){
					dp[(1 << i) | (1 << j)][i][j] = dp[(1 << i) | (1 << j)][j][i] = value[i] + value[j] + value[i] * value[j];
					num[(1 << i) | (1 << j)][i][j] = num[(1 << i) | (1 << j)][j][i] = 1;
				}
			}
		}
		for (int i = 3; i < (1 << n); i++){
			for (int j = 0; j < n; j++){
				if ((1 << j)&i){
					for (int k = 0; k < n; k++){
						if (k == j)
							continue;
						if ((1 << k)&i&&dp[i][j][k]){
							for (int l = 0; l < n; l++){
								if (!((1 << l)&i) && vis[k][l]){
									int sum = dp[i][j][k] + value[l] + value[k] * value[l];
									if (vis[l][j]){
										sum += value[j] * value[k] * value[l];
									}
									if (sum>dp[i | (1 << l)][k][l]){
										dp[i | (1 << l)][k][l] = sum;
										num[i | (1 << l)][k][l] = num[i][j][k];
									}
									else if (sum == dp[i | (1 << l)][k][l]){
										num[i | (1 << l)][k][l] += num[i][j][k];
									}
								}
							}
						}
					}
				}
			}
		}
		LL ans = 0;
		int maxnum = 0;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				if (!vis[i][j])
					continue;
				if (maxnum < dp[(1 << n) - 1][i][j]){
					maxnum = dp[(1 << n) - 1][i][j];
					ans = num[(1 << n) - 1][i][j];
				}
				else if (maxnum == dp[(1 << n) - 1][i][j]){
					ans += num[(1 << n) - 1][i][j];
				}
			}
		}
		printf("%d %lld\n", maxnum, ans >> 1);
	}
}


STM32与K210串口通信 这篇文章是为了填上一篇埋下的坑,k210和STM32通过串口通信,我是采用数据包的形式发送数据的,因为K210发送的数据有几种,采用数据包的形式发送比较安全。本文代码运行的平台是STM32F103C8T6和K210 Dock,只涉及到K210发送数据给STM32,stm32将接受到的数据显示到0.96寸OLED屏上。 阅读详情

相关推荐

ESP32搭配LVGL显示图片的两种方法详解:内部存储 vs 外部文件

本文详细介绍了ESP32搭配LVGL显示图片的两种方法:内部存储与外部文件。内部存储方案适合快速响应的小图片,而外部文件方案则适用于需要灵活更新和大容量图片的场景。通过对比分析,帮助开发者根据项目需求选择最优方案,提升嵌入式系统的UI性能。

weixin_30399821的博客 519

POJ-2288: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.

soulandlove的博客 342

agent-browser OpenClaw 的最佳搭档

agent-browser 是专为 AI Agent 设计的浏览器自动化工具,目前被公认为 OpenClaw 的最佳配套方案。这份技术文档完整记录了其架构、协议、性能调优与常见问题处理,非常适合想让OpenClaw 真正“上手”互联网的开发者与老用户。

liu_zhiyi的博客 1781

poj2288之求有多少条哈密顿路

Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 8427   Accepted: 2174 Description Given a map of islands and bridges that connect these

Stephen 1461

RANSAC地面点分割:从原理到C++实战优化

本文深入解析RANSAC算法在点云地面分割中的原理与C++实战优化,涵盖动态迭代次数、自适应阈值等关键技巧,通过并行计算和内存访问优化提升性能,适用于激光雷达点云处理。

weixin_29015051的博客 277

紫光同创PDS软件操作指南:如何为FPGA新增自定义Flash型号

本文详细介绍了在紫光同创PDS软件中为FPGA新增自定义Flash型号的完整操作指南。针对官方支持列表之外的SPI Flash芯片,文章从理解FPGA配置逻辑入手,逐步讲解如何通过扫描Flash ID、在Flash Operator中精准填写参数、转换.sbit文件,最终完成固化的全过程,并分享了高级技巧与常见问题排查方法,帮助工程师摆脱对预设型号的依赖,实现灵活选型。

weixin_29204749的博客 325

poj 2288 Islands and Bridges

Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the bridges suc

喵呜的Blog 1214

Ubutntu下使用realsense d435i(二):获取某二维像素点的三维坐标

00 准备工作 准备工作包括安装驱动以及pyrealsense2,具体可参考上一篇: Ubutntu下使用realsense d435i(一):准备工作以及安装pyrealsense2 https://blog.youkuaiyun.com/gyxx1998/article/details/121461293 01 通过二维像素点获得物体三维坐标 这个在上一篇已经介绍了代码: realsense d435i获取某像素点三维坐标(计算深度和点云两种方法) https://blog.youkuaiyun.com/gyxx1998/

gyxx1998的博客 9943

poj 2288

<br /> Islands and BridgesTime Limit: 4000MS Memory Limit: 65536KTotal Submissions: 5733 Accepted: 1455<br />DescriptionGiven 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

志当存高远 715

核心组件大换血:Backbone与Neck魔改篇:YOLO26替换EfficientNetV2主干:神经架构搜索(NAS)的最优解嫁接

YOLO26引入EfficientNetV2作为Backbone的改进方案探索了计算机视觉模型优化的前沿方向。文章首先分析了YOLO26默认C3k2模块的局限性,指出其在通道利用效率和缩放策略上的不足。随后重点介绍了EfficientNetV2的核心优势,包括Fused-MBConv模块、训练感知的NAS搜索空间和渐进式学习策略,这些特性使其在精度和效率上保持领先。最后提出了将EfficientNetV2嫁接至YOLO26的具体方案,强调通道对齐这一关键挑战,并给出两种解决方案。这一改进思路展现了&quot

ZDQ58818的博客 309

Python爬虫抓取学术期刊数据(Springer、Elsevier)

本文详细介绍了如何使用Python爬虫抓取学术期刊网站(如Springer、Elsevier)上的文章信息,并进行数据清洗和存储。我们使用了requestsSeleniumpandas等技术,通过模拟浏览器行为来绕过反爬虫机制,并将数据保存为Excel文件,方便后续分析。未来,我们可以通过加入更多的期刊网站数据源,扩展爬虫的功能,抓取更多的学术数据。同时,结合自然语言处理(NLP)技术,可以对抓取的文章进行自动化的摘要提取和主题分类,为学术研究提供更多的支持。

2201_76125261的博客 1487

POJ2288 Islands and Bridges

状压DP 哈密顿路径

a free man 608

Windows 下 OpenClaw(小龙虾) 安装全记录:从零到一的保姆级图文教程(持续更新中)

本文主要介绍了Windows 下 OpenClaw 安装全记录:从零到一的保姆级图文教程,希望能对学习使用OpenClaw 的同学们有所帮助。 文章目录 1. 背景 2. 实战教程 2.1 步骤一 安装Node.js 2.2 步骤二:安装并配置OpenClaw 2.3 步骤三:配置模型 2.4 步骤四:对话 3. 后续使用步骤

weixin_43178406的博客 16万+

poj 2288 Islands and Bridges

#include #include #include #include using namespace std; //poj 2288 Islands and Bridges int n,m; int a[15]; int con[15][15]; __int64 dp[1ans) { ans=dp[(1

birdforever的专栏 530

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

poj -- 2288 Islands and Bridges

题意:给你n个岛屿,m条路,每个

风儿-zsj的专栏 564

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

POJ2288 Islands and Bridges

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

dezhen7015的博客 134

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

poj 2288 Islands and Bridges ——状压DP

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

aodan5477的博客 160

POJ 2288 Islands and Bridges (状压dp)

文章目录POJ 2288 Islands and Bridges (状压dp)原题面Islands and Bridges输入输出样例输入样例输出题意解释题解题解代码 POJ 2288 Islands and Bridges (状压dp) 西安EC Final 之后,我第一学年的ICPC-CCPC之旅就算结束了。一年的比赛暴露了自己太多的问题,希望自己能坚持写博客来提升自己的能力,也为了督促自己备战2021-2022赛季的比赛。 原题面 Islands and Bridges Given a map of

weixin_44539230的博客 424

poj 2288 Islands and Bridges

Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 7821   Accepted: 2003 Description Given a map of islands and bridges that connect these isl

记录成长的点点滴滴. . . . . 1426

poj 2288Islands and Bridges

#include<stdio.h> #include<string.h> #include<algorithm> #include<vector> #include<cmath> #include<iostream> using namespace std; typedef long long ll; const int maxn=100100; typedef long long ll;int w

qq_31664927的博客 318
上一篇: CodeForces 149D Coloring Brackets
下一篇: HDU 3681 Prison Break
overload1997
博客等级 码龄11年 328粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值