当前位置:网站首页>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 :
边栏推荐
- asciinema 搭配 asciicast2gif 实现高效的命令行终端录制能力
- How to obtain ECS metadata
- Special topic of IM code scanning login Technology (III): easy to understand. A detailed principle of IM code scanning login function is enough
- Is it safe for futures companies to open accounts
- Teach you how to view version information with mongodb
- CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
- 高速公路服务区智能一体机解决方案
- 国产最长寿的热销手机,苹果也不是对手,总算让国产手机找回面子
- leetcode 139. Word break word split (medium)
- Recommend several super practical data analysis tools
猜你喜欢

How to expand disk space on AWS host

如何扩展aws主机上的磁盘空间

Vim编辑器的最常用的用法

一文详解JackSon配置信息

MongoDB入门实战教程:学习总结目录

The penetration of 5g users of operators is far slower than that of 4G. The popularity of 5g still depends on China Radio and television

Nifi from introduction to practice (nanny level tutorial) - environment

nifi从入门到实战(保姆级教程)——环境篇
![clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]](/img/f0/42f394dbc989d381387c7b953d2a39.jpg)
clang: warning: argument unused during compilation: ‘-no-pie‘ [-Wunused-command-line-argument]
![[C language questions -- leetcode 12 questions] take you off and fly into the garbage](/img/ca/a356a867f3b7ef2814080fb76b9bfb.png)
[C language questions -- leetcode 12 questions] take you off and fly into the garbage
随机推荐
Understanding openstack network
Tencent cloud native intelligent data Lake Conference will be held, revealing the panoramic matrix of Tencent cloud data Lake products for the first time
Solution of intelligent all in one machine in expressway service area
MySQL binlog
Istio practical skill: enable accesslog locally
Summary of common tools and usage
CIA security model - use PGP to describe privacy and integrity of network security CIA model
VNC Viewer方式的远程连接树莓派
Linux记录-4.22 MySQL5.37安装(补充)
[C language questions -- leetcode 12 questions] take you off and fly into the garbage
Flink Kubernetes Application部署
【Prometheus】4. Monitoring cases
Detailed explanation of estab of Stata regression table output
Linux record -4.22 MySQL 5.37 installation (supplementary)
Firefox browser uses plug-ins to set up proxy
CAP:多重注意力机制,有趣的细粒度分类方案 | AAAI 2021
【云原生 | Kubernetes篇】Kubernetes基础入门(三)
10 hands-free idea plug-ins. These codes do not need to be written (the second bullet)
个人常用的高效工具
Nature刊登量子计算重大进展:有史以来第一个量子集成电路实现