1150 Travelling Salesman Problem (25 point(s))

本文探讨了旅行商问题(Travelling Salesman Problem),一种在组合优化中经典的NP-hard问题。通过给定的城市列表及其相互之间的距离,目标是找到一条访问每个城市一次并返回起点的最短可能路线。文章提供了一种解决方案,使用邻接矩阵存储城市间的距离,并对给定的路径序列进行评估,判断其是否构成旅行商问题的有效解,同时确定最接近最优解的路径。

1150 Travelling Salesman Problem (25 point(s))

The "travelling salesman problem" asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city?" It is an NP-hard problem in combinatorial optimization, important in operations research and theoretical computer science. (Quoted from "https://en.wikipedia.org/wiki/Travelling_salesman_problem".)

In this problem, you are supposed to find, from a given list of cycles, the one that is the closest to the solution of a travelling salesman problem.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<N≤200), the number of cities, and M, the number of edges in an undirected graph. Then M lines follow, each describes an edge in the format City1 City2 Dist, where the cities are numbered from 1 to N and the distance Dist is positive and is no more than 100. The next line gives a positive integer K which is the number of paths, followed by K lines of paths, each in the format:

n C​1​​ C​2​​ ... C​n​​

where n is the number of cities in the list, and C​i​​'s are the cities on a path.

Output Specification:

For each path, print in a line Path X: TotalDist (Description) where X is the index (starting from 1) of that path, TotalDist its total distance (if this distance does not exist, output NA instead), and Description is one of the following:

  • TS simple cycle if it is a simple cycle that visits every city;
  • TS cycle if it is a cycle that visits every city, but not a simple cycle;
  • Not a TS cycle if it is NOT a cycle that visits every city.

Finally print in a line Shortest Dist(X) = TotalDist where X is the index of the cycle that is the closest to the solution of a travelling salesman problem, and TotalDist is its total distance. It is guaranteed that such a solution is unique.

Sample Input:

6 10
6 2 1
3 4 1
1 5 1
2 5 1
3 1 8
4 1 6
1 6 1
6 3 1
1 2 1
4 5 1
7
7 5 1 4 3 6 2 5
7 6 1 3 4 5 2 6
6 5 1 4 3 6 2
9 6 2 1 6 3 4 5 2 6
4 1 2 5 1
7 6 1 2 5 4 3 1
7 6 3 2 5 4 1 6

Sample Output:

Path 1: 11 (TS simple cycle)
Path 2: 13 (TS simple cycle)
Path 3: 10 (Not a TS cycle)
Path 4: 8 (TS cycle)
Path 5: 3 (Not a TS cycle)
Path 6: 13 (Not a TS cycle)
Path 7: NA (Not a TS cycle)
Shortest Dist(4) = 8

结合图论的模拟题。由于需要快速根据边的结点获得距离,因此采用邻接矩阵来存储。

思路:

对于每一个给定的序列,首先计算总距离,按照序列顺序计算每两个相邻的结点的距离,每到一个结点标记该结点,如果边不存在,直接跳出循环。 迭代完成后,如果存在没有标记的结点,说明Not a TS cycle,否则,如果是TS simple cycle,则有序列的长度刚好比结点数多1,如果超过1则说明只是TS cycle。

注意对TS simple cycle和TS cycle记录最短距离和路径编号。

#include<iostream>
#include<vector>
#include<cstring>
#include<set>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 207;
int N,M,K;
bool visit[MAX]={false};
int graph[MAX][MAX];
int main(void){
	cin>>N>>M;int a,b,dist;
	memset(graph,-1,sizeof(graph));
	for(int i=0;i<M;i++){
		cin>>a>>b>>dist;
		graph[a][b]=dist;
		graph[b][a]=dist;
	}
	cin>>K;
	int l,m;vector<int> v;
	int min = INF;int index =-1;
	for(int j=1;j<=K;j++){
		cin>>l;
		v.clear();
		for(int i=0;i<l;i++){
			cin>>m;
			v.push_back(m);
		}
		memset(visit,false,sizeof(visit));
		bool edgeExist = true;int total = 0;
		for(int i=0;i+1<v.size();i++){
			if(graph[v[i]][v[i+1]]==-1) edgeExist = false;
			else{
				total+=graph[v[i]][v[i+1]];
				visit[v[i+1]]=true; 
			}
		}
		cout<<"Path "<<j<<": ";
		if(!edgeExist){
			cout<<"NA "<<"(Not a TS cycle)"<<endl;
			continue;
		}
		bool isTSC = true;
		for(int i=1;i<=N;i++){
			if(!visit[i]){
				isTSC = false;
				break;
			}
		}
		if(!isTSC){
			cout<<total<<" (Not a TS cycle)"<<endl;
		}
		else{
			cout<<total;
			if(v.size()==N+1) cout<<" (TS simple cycle)"<<endl;
			else if(v.size()>N+1) cout<<" (TS cycle)"<<endl;
			if(total<min){
				index=j;
				min = total;
			}
		}
	}
	cout<<"Shortest Dist("<<index<<") = "<<min<<endl; 
	return 0;
}

 

打开链接下载源码: https://pan.quark.cn/s/c43e5bd27521 标题中的“AMD and Nvidia GOP update 1.9.6.rar”表示这是一个包含了AMD与Nvidia显卡的GOP(Graphics Output Protocol)驱动程序升级至1.9.6版本的压缩文件。该更新主要针对显卡在UEFI(统一可扩展固件接口)环境下的图形输出性能进行优化,并致力于提升系统的稳定性。在描述中提及“显卡附加UEFI引导工具,最新版”,表明此次更新内含了一个专为UEFI BIOS环境设计的显卡引导工具,或许表现为一个自启动脚本或程序,例如GOPupd.bat。通过这一工具,用户能够在UEFI模式下对显卡进行精确的配置和初始化,从而保障操作系统能够最大化地发挥显卡的效能。必需的组件包括“colorama-0.4.3”,这是一个在Windows平台上用于管理颜色控制序列的Python模块,可能在更新过程中用于生成彩色命令行显示,以增强用户交互的直观性。此外,“Visual C++Redistributable”是微软提供的运行时支持库,旨在确保基于C++编译的应用程序能够正常运行,此处可能用于更新工具或相关依赖模块。标签“uefi bios”突显了该更新与UEFI BIOS系统的紧密关联,暗示其将作用于计算机的启动序列及硬件初始化过程。压缩包内的文件清单如下: 1. GOPupd.bat - 很有可能是负责执行GPU UEFI引导更新的核心脚本。 2. #Nvidia_ROM_Info.bat 和 #AMD_ROM_Info.bat - 这两个文档可能用于采集Nvidia与AMD显卡的ROM数据,以辅助识别显卡型号并执行适配性验证。 3....
代码下载地址: https://pan.quark.cn/s/a2e2c95e6128 意法半导体(STMicroelectronics)研发的STM32H750是一款性能优越的微控制器,属于STM32H7系列,拥有卓越的处理性能以及多元化的外设接口。在此项工作中,我们将研究如何借助STM32H750达成串口空闲中断(IDLE interrupt)的运用、借助DMA完成UART(通用异步收发传输器)的数据传输,并且探究如何运用STM32CubeMX配置并构建MDK5(Keil uVision5)项目。串口空闲中断是串口通信中的一个核心功能,当串口在一段时间内没有进行数据交换时,会引发该中断。这种功能在需要实时监测串口状态的应用场合中非常有价值,比如,在等待特定指令或需要降低能耗的情况下。在STM32H750中,设定串口空闲中断通常包含以下几个环节: 1. 串口设置:在STM32CubeMX中选定相应的UART接口,并激活中断功能。 2. 中断优先级设定:按照应用需求设定中断优先级。 3. 中断服务函数注册:在程序代码中定义中断服务函数以应对中断事件。 4. 启用串口空闲中断:在初始化代码中激活串口的IDLE位,使能中断。 DMA(Direct Memory Access)传输是一种高效的数据传输机制,它允许外设直接与内存进行交互,无需CPU的介入,从而减轻了CPU的工作负担。在STM32H750中,我们可以运用DMA配合UART来接收数据: 1. DMA配置:在STM32CubeMX中为UART选择合适的DMA通道,并设定传输特性。 2. UART配置:将UART设置为DMA模式,并指定接收缓冲区的地址。 3. 中断配置:开启DMA传输完成中断,以便在数据接收完...
源码直接下载地址: https://pan.quark.cn/s/d64de7ee3e36 STM32CubeIDE是由STMicroelectronics(意法半导体)开发的一款集成开发环境,其核心功能是针对STM32系列微控制器进行优化,并集成了包括源代码编写、编译执行、调试检测以及项目参数设置在内的完整开发工具集。该开发平台依托于Eclipse系统框架构建,旨在为编程人员营造一个便捷且生产力高的工作场景。1.9.0版本属于其产品线中的一个成熟版本,通常包含了若干性能增强措施以及新特性的集成。在嵌入式系统的构建过程中,代码的自动完成机制是一项关键的辅助技术,它能够显著提升工作速率并降低操作失误。专门为这一目的设计的STM32CubeIDE 1.9.0自动代码补全组件,能够有效满足开发者的相关需求。通过将压缩文件中的内容部署到STM32CubeIDE安装路径下的`plugins`子目录中,该插件即可被系统自动检测并激活,从而在代码编写阶段,系统能够基于上下文信息智能地预判并展示潜在的函数名称、变量定义或常量值,进而辅助开发者迅速完成输入任务。基于ARM Cortex-M架构的STM32系列微控制器,在物联网装置、工业自动化系统、个人消费类电子设备等领域具有广泛的部署。在这些应用场景中,单片机扮演着核心角色,而STM32凭借卓越的处理性能、多样化的外部接口配置以及出色的能源控制能力,已成为众多开发者的首选方案。STM32CubeIDE所提供的自动代码补全功能,对于初入行业的开发者而言尤为适宜,因为它能够实时呈现API函数的相关信息,涵盖函数标识符、参数的数据类型与数目,乃至函数的返回类型,从而协助开发者精准地运用STM32的固件库。不仅如此,即便对于已经熟练掌握ST...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值