hdu3001——Travelling 三进制TSP, 状态压缩

Linux SMP系统多核启动机制详解 对称多处理(SMP)是现代操作系统支持多核并行计算的基础架构。其核心原理是通过引导处理器(BSP)初始化系统环境后,通过处理器间中断(IPI)机制唤醒应用处理器(AP)。在x86架构中,AP启动时处于实模式,需要特殊的trampoline代码完成保护模式切换和分页设置。这种设计既保证了系统初始化的正确性,又实现了多核并行处理的性能优势。理解SMP启动流程对优化系统启动速度和调试多核问题至关重要,特别是在云计算和嵌入式系统中,高效的SMP初始化能显著提升系统响应能力。本文以Linux内核为例,深入解析了从BS 阅读详情

Travelling

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4106    Accepted Submission(s): 1310


Problem Description
After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn't want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.
 

Input
There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.
 

Output
Output the minimum fee that he should pay,or -1 if he can't find such a route.
 

Sample Input
  
2 1 1 2 100 3 2 1 2 40 2 3 50 3 3 1 2 3 1 3 4 2 3 10
 

Sample Output
  
100 90 7
 

Source
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:   3004  3002  3006  3007  3003


这题一看就是TSP,然后想到要状压,但是这里每个城市最多可以走两次,所以不能用普通的二进制来状压,需要三进制

三进制每一位是0 1 2, 0表示这座城市没到过,1表示到过一次,2表示到过两次
由于一共10座城市,我们先把3^0 --- 3 ^ 10预处理出来,然后化作三进制存好,接着就是dp,但是要注意,可以作为最后状态的状态,它的每一位都不为0(否则就有某个城市没到过),最后枚举所有可以作为尾状态的状态然后求个最小值就ok了

#include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int inf = 0x3f3f3f3f;

int dp[60000][15];
int dist[15][15];
int three[15] = {0, 1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049};
int city[60000][15];

int main()
{
	int n, m, u, v, w;
	memset (city, inf, sizeof(city));
	for (int i = 0; i < 59059; ++i)
	{
		int tmp = i;
		for (int j = 1; j <= 10; ++j)
		{
			city[i][j] = tmp % 3;
			tmp /= 3;
		}
	}
	while (~scanf("%d%d", &n, &m))
	{
		memset (dist, inf, sizeof(dist));
		memset (dp, inf, sizeof(dp));
		for (int i = 1; i <= n; ++i)
		{
			dp[three[i]][i] = 0;
		}
		dp[0][0] = 0;
		for (int i = 1; i <= m; ++i)
		{
			scanf("%d%d%d", &u, &v, &w);
			dist[u][v] = min(dist[u][v], w);
			dist[v][u] = dist[u][v];
		}
		int ans = inf;
		for (int i = 0; i < three[n + 1]; ++i)
		{
			bool is_end = 1;
			for (int j = 1; j <= n; ++j)
			{
				if (city[i][j] == 0)
				{
					is_end = 0;
				}
				for (int k = 1; k <= 10; ++k)
				{
					if (city[i][j] == 2)
					{
						continue;
					}
					dp[i + three[j]][j] = min(dp[i + three[j]][j], dp[i][k] + dist[k][j]);
				}
			}
			if (is_end)
			{
				for (int j = 1; j <= n; ++j)
				{
					ans = min(ans, dp[i][j]);
				}
			}
		}
		if (ans == inf)
		{
			printf("-1\n");
			continue;
		}
		printf("%d\n", ans);
	}
	return 0;
}


ESP32 驱动 SYN6288 语音合成:Arduino IDE 下实现中文数字混合文本的智能播报 本文详细介绍了如何在Arduino IDE环境下使用ESP32驱动SYN6288语音合成模块,实现中文数字混合文本的智能播报。通过硬件选型、开发环境搭建、编码转换原理、命令帧构造及串口通信实战等步骤,帮助开发者快速掌握语音播报技术。文章还提供了混合文本处理技巧和常见问题排查方法,适合智能硬件开发者参考。 阅读详情

相关推荐

滑模控制实战:用Python从零实现SMC算法(附完整代码)

本文通过Python实战,从零详解滑模控制(SMC)算法的核心原理与代码实现。文章聚焦于一阶系统,使用NumPy和Matplotlib逐步构建控制器,并针对SMC固有的抖振问题,详细讲解了边界层法的原理与实现,提供了完整的代码和参数调优指南,帮助读者直观理解并掌握这一鲁棒性极强的非线性控制方法。

weixin_29285605的博客 466

HDU 3001 三进制状压DP

N个城市,M条道路,每条道路有其经过的代价,每个城市最多可以到达两次,求走完所有城市最小代价,起点任意。 三进制状压,存储每个状态下每个城市经过的次数。 转移方程: dp[i+b[k]][k]=Min(dp[i+b[k]][k],dp[i][j]+dis[j][k]); #include "stdio.h" #include "string.h" const int i

836

手机流畅的秘密:拆解LPDDR5X在骁龙8 Gen2平台上的电源与信号完整性设计

本文深入解析了LPDDR5X内存与骁龙8 Gen2处理器在旗舰手机中的协同设计,重点探讨了0.5V超低电压下的电源完整性设计以及8.5Gbps高速传输中的信号完整性挑战。通过详细的电源树架构、关键器件选型和布线策略分析,揭示了如何实现高性能与低功耗的完美平衡,为手机流畅体验提供硬件保障。

weixin_30235225的博客 484

Travelling (HDU 3001)

一开始看成了一个城市最多只旅行一次,还以为是简单题,所以直接DFS,可是一直不对。 后来仔细看,是至少旅行两次,这样用DFS就没法了。 后来查了才知道是TSP问题,用的是DP,其实我也不太会写,先是看了一下别人的原理,代码, 自己也是边写边模仿的吧。,就这样了 #include #include #include #include using

jiangjiashi的博客 853

ENS160传感器原理图设计,已量产(空气质量传感器)

ENS160 是一款集成式数字空气质量传感器,核心优势源于 “MEMS 传感元件 + 内置微处理器” 的架构。

不脱发的程序猿 572

金三银四!风控建模岗面试题库大全:从理论到实战避坑指南

风控建模面试大全

阿黎逸阳的博客 767

HDU3001Travelling(状压dp)

题目链接 dp[i][j]代表i节点为端点,点集状态为j的最小花费,j用三进制来操作就好了dp[i][j]代表i节点为端点,点集状态为j的最小花费,j用三进制来操作就好了dp[i][j]代表i节点为端点,点集状态为j的最小花费,j用三进制来操作就好了 #include<bits/stdc++.h> using namespace std; typedef long long LL; L...

IDrandom的专栏 309

信创服务器(麒麟 V10 SP3)安装 NVIDIA GeForce RTX 5070 驱动

本文介绍了在麒麟V10 SP3系统上安装NVIDIA RTX 5070显卡驱动的详细步骤。首先需要关闭nouveau驱动,安装必要的编译环境和工具,确保内核版本与kernel-devel完全一致。最后运行NVIDIA驱动安装程序,关键选择包括启用DKMS支持、编译模块、禁用32位兼容和nouveau驱动。整个流程适用于海光CPU和Blackwell架构GPU的国产操作系统环境。

猿小白的博客 683

PAT甲级——A1087 All Roads Lead to Rome【30】

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness. Input Specification: ...

weixin_30632089的博客 194

Nacos 从困惑到蒙圈(9)Nacos 适配 达梦数据库【最新版】

介绍Nacos适配达梦数据库的详细步骤,包括对最新版本的支持改造。

甘蓝的专栏 1378

【OpenFOAM】-olaFlow-算例10-wavemakerTank

采用 Flap和Piston两种方式的动网格进行造波

ou_no的博客 2168

HDU3001-Travelling(三进制+状态压缩)

After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any

weixin_43357583的博客 253

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Travelling HDU - 3001 三进制状态压缩 tsp问题

After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being...

weixin_41544329的博客 186

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

HDU 3001 Travelling (状态压缩/三进制)

Travelling(HDU 3001) Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8931 Accepted Submission(s): 2914 Problem Description After coding ...

爱玲姐姐的博客 870

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

hdu 3001 Travelling (TSP问题,状压dp)

After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being

Vision 1373

HDU 3001 Travelling (三进制状态压缩DP+BFS)

题意:有n个点(n 分析:每个点最多经过两次,则用三进制保存状态,一个好的技巧就是开个pos数组,记录每个状态用三进制表示时,每一位的情况。    枚举每个点为起点,然后每次sum值是不需要清空的,清空了反而造成了诸多重复遍历.................... #include #include #include #include #define INF 0x7FFFFF

Paradiser 1511

HDU 3001--Travelling(状态压缩+tsp)

来源:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意:n个点,m个边,求遍历所有边的最短路径,并且每点最多访问两次。 分析:很经典的TSP问题,但是题目又有改变,每个旅游地可以走两遍,故进行扩展2状态的状态压缩DP伸展为3状态的状态压缩问题。0表示没去过此点,1 表示去过一次,2表示去过两次,状态转移方程基本和以前一样dp[i][j]=min

Xcockroach的专栏 684

hdu3001 Travelling(TSP+三进制)

题目链接:点这里!!! 题意: 给你n(n 题解: 你可以可以利用三进制表示这个点你经过了几次,在加上基础的TSP就可以解决了!!! 代码: #include #include #include #include #include #include #include #include #include #include #include #include #in

Zsss的博客 379

HDU3001 Travelling

状压DP TSP

a free man 554

HDU3001——Travelling(状态压缩DP)

Travelling Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2393    Accepted Submission(s): 692 Problem Description After coding so many

JIA_char 617

HDU 3001 Travelling

Problem Description After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not

XDU_PYL的博客 522

HDU 3001 Travelling:TSP(旅行商)【节点最多经过2次】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题意:   有n个城市,m条双向道路,每条道路走一次需要花费路费v。你可以将任意一个城市作为起点出发,然后遍历每一个城市,并保证同一个城市最多经过2次。问你遍历这些城市的最小费用是多少。 题解:   传统的TSP问题中,每个城市只能经过一次,做法为三重for循环,分别枚举...

a1392136的博客 277

Travelling(HDU3001+状压dp+三进制+最短路)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目: 题意:n个城市,m条边,每条边都有一个权值,问你经过所有的城市且每条边通过次数不超过两次的最短距离。 思路:状压dp+三进制,dp[i][j]表示在状态i下以j为目标城市的最短距离,转移方程为nw = i + fi[k];dp[nw][k] = min(dp[nw][...

di6499的博客 148

HDU 3001 - Travelling TSP

http://acm.hdu.edu.cn/showproblem.php?pid=3001#include #include #include #include #include #include using namespace std; template _T Max(_T a , _T b){ return (a>b)?(a):(b); } template _T Max

生命有一种绝对 796

hdu 3001 Travelling 状压dp TSP变形

题目:点击打开链接 题意:给定n 个城市已经 m 条路 以及对应路费c,要求遍历所有城市最少的路费,每个城市不能超过2次 分析:要求每个点最多走两边,不是只可以走一次,所以要用三进制的状态压缩解决这个问题。可以预处理每个状态的第k位是什么。 状态的表示 f[st][i],表示现在在i点,状态是st的最短路程。那么状态转移显而易见:f[st][i]=min( f[{st}-i][j]+

01的世界 394

HDU 3001 Travelling (三进制状压dp)

n(n<=10)个城市,知道每个城市间的旅行费用,但每个城市最多走两遍。问最小花费是多少 。 也就是每个城市可以走两次的tsp问题。

Follow My Heart 608
上一篇: hdu2151——Worm
下一篇: hdu1506——Largest Rectangle in a Histogram
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值