当前位置:网站首页>April 23, 2021: there are n cities in the TSP problem, and there is a distance between any two cities
April 23, 2021: there are n cities in the TSP problem, and there is a distance between any two cities
2022-06-24 15:53:00 【Fuda scaffold constructor's daily question】
2021-04-23:TSP problem Yes N Cities , There is a distance between any two cities , The distance from any city to itself is 0. All point to point distances There is one for each N*N Two dimensional array of matrix in , That is, the whole graph is represented by adjacency matrix . We now require a travel agent to start from k City You must go through every city and only stay in one city once , Finally return to the departure k city , Return to the path with the shortest total distance distance . The parameter is given a matrix, Given k.
Fuda answer 2021-04-23:
Dynamic programming .
The code to use golang To write . The code is as follows :
package main import ( "fmt" "math" ) func main() { matrix := [][]int{ {0, 2, 4, 5}, {2, 0, 4, 4}, {4, 4, 0, 2}, {5, 4, 2, 0}} ret := t4(matrix) fmt.Println(ret) } func t4(matrix [][]int) int { N := len(matrix) // 0...N-1 statusNums := 1 << N dp := make([][]int, statusNums) for i := 0; i < statusNums; i++ { dp[i] = make([]int, N) } for status := 0; status < statusNums; status++ { for start := 0; start < N; start++ { if (status & (1 << start)) != 0 { if status == (status & (^status + 1)) { dp[status][start] = matrix[start][0] } else { min := math.MaxInt32 //Integer.MAX_VALUE; // start Cities in status After removing the , The state of preStatus := status & (^(1 << start)) // start -> i for i := 0; i < N; i++ { if (preStatus & (1 << i)) != 0 { cur := matrix[start][i] + dp[preStatus][i] min = getMin(min, cur) } } dp[status][start] = min } } } } return dp[statusNums-1][0] } func getMin(a int, b int) int { if a < b { return a } else { return b } }
The results are as follows :
边栏推荐
- The 30 pictures bring the network protocol layer by layer to life. It's really fragrant!
- 用 Oasis 开发一个跳一跳(一)—— 场景搭建
- Step by step import RHEL image to Tencent cloud
- 如何扩展aws主机上的磁盘空间
- Summary of common tools and usage
- In 2021, big companies often ask IOS interview questions -- runloop
- Leetcode 139. Mot break word Split (medium)
- Three solutions for Jenkins image failing to update plug-in Center
- 刚刚阿里面软件测试回来,3+1面任职阿里P7,年薪28*15薪
- 期货公司开户安全吗
猜你喜欢
60 divine vs Code plug-ins!!
I just came back from the Ali software test. I worked for Alibaba P7 in 3+1, with an annual salary of 28*15
Mongodb Getting started Practical Tutoriel: Learning Summary Table des matières
国产最长寿的热销手机,苹果也不是对手,总算让国产手机找回面子
FreeRTOS新建任务不执行问题解决办法
Most common usage of vim editor
Several common DoS attacks
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
Using alicloud RDS for SQL Server Performance insight to optimize database load - first understanding of performance insight
运营商5G用户渗透远远比4G慢,5G的普及还得看中国广电
随机推荐
Improving the classification of motor imagery by combining EEG and MEG signals in BCI
Jenkins 镜像无法更新插件中心的3种解决方法
使用阿里云RDS for SQL Server性能洞察优化数据库负载-初识性能洞察
"Industry foresight" future development trend of intelligent security monitoring industry
How to optimize performance
Logging is not as simple as you think
【应用推荐】最近大火的Apifox & Apipost 上手体验与选型建议
60 divine vs Code plug-ins!!
PHP export data as excel table
如何实现容器内的SqlServer的数据库迁移
One article explains Jackson configuration information in detail
This website teaches you to imitate more than 100 well-known websites!
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
07. Tencent cloud IOT device side learning - Data Template
Installer la Bibliothèque imagemagick 7.1 et l'extension imagick de PHP
不忘初心
Several common DoS attacks
【云原生 | Kubernetes篇】Kubernetes基础入门(三)
Design of CAN bus controller based on FPGA (Part 2)
asciinema 搭配 asciicast2gif 实现高效的命令行终端录制能力