当前位置:网站首页>The connection and solution between the shortest Hamilton path and the traveling salesman problem
The connection and solution between the shortest Hamilton path and the traveling salesman problem
2022-07-05 05:41:00 【Falling spring is only inadvertently】
The shortest Hamilton Path and traveling salesman problem
Preface
I found that many blogs either post code directly , Or it's right dp Explain the formula , It makes people feel speechless without saying why they got this formula , That might be why c standing
The shortest Hamilton route
Title Transfer door
The title means from 0 From point to n-1 The shortest point Hamilton route , We analyze it from the perspective of set
So what are the conditions for this transition
- In the current path state ,i The corresponding position needs to be 1, That is, the end point of the starting point needs to appear on the path .
- For intermediate nodes k for , Each point can only appear once , So let's get rid of i Then the set of corresponding paths needs to have nodes k.
If the above conditions are met, there can be a transfer formula :
d p [ i ] [ j ] = m i n ( d p [ i ] [ j ] , d p [ i − ( 1 < < j ) ] [ k ] + w [ k ] [ j ] dp[i][j] = min(dp[i][j],dp[i-(1<<j)][k] + w[k][j] dp[i][j]=min(dp[i][j],dp[i−(1<<j)][k]+w[k][j]
Let's take the outer cycle as the last , That is to say state State transition means that all points pass through once , The second cycle is n-1 That is, the end of the question , We're enumerating k That is, we are considering which point is better to arrive at the end , If we choose a point , Then there will be the same sub problem at this point .
Code :
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
const int N = 20 ,M = 1<<20;
int f[M][N] ,weight[N][N];
int n;
int main(){
scanf("%d",&n);
for(int i=0;i<n;++i)
for(int j = 0;j<n;++j)
scanf("%d",&weight[i][j]);
memset(f,0x3f,sizeof f);
f[1][0] = 0;
for(int i=0;i< 1<<n;++i)
for(int j=0;j<n;++j)if(i>>j &1) // There are j
for(int k = 0;k<n;++k)if(i-(1<<j)>>k & 1 ) // There are k, But there is no j
f[i][j] = min(f[i][j],f[i-(1<<j)][k] + weight[k][j]);//i-(1<<j) Already in if Prove that the location exists
printf("%d\n",f[(1<<n)-1][n-1]);
return 0;
}
Travel agent problem
Why should I compare the problem of travel agents with Hamilton The paths are written together , The traveling salesman problem is abstractly a starting point arbitrary and the end point is its own Hamilton route .
Let's think back to Hamilton In the path , d p [ i ] [ j ] dp[i][j] dp[i][j] What arrays mean is : from 0 Appear to the end j, And each point only goes once i Shortest path , Then we are finishing the calculation Hamilton After path , We just need to add a line from the end to 0 The path of , Then we can form this starting point arbitrary traveling salesman problem .
So here comes the question , Someone may ask why it is connected to 0 It can't be anything else ,
First of all, we know that if we choose arbitrarily at the starting point, then every point can go once , Then the end is definitely not this point , Then we can specify that the last point is 0( Then add 0 The distance to the starting point ) Because the first point of the starting point is arbitrary , Finally, we get a minimum value in any possibility .
So let's turn over this idea , We can make the point pointing to the starting point arbitrary , Then the first point pointed by the starting point will not be arbitrary ( Recall what probability theory learned ), Therefore, we can set the first point of our starting point 0, Then we can transform this part into Hamilton Path . Here's the code
Code :
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 16, M = 1 << N, INF = 0x3f3f3f3f;
int n, m, g[N][N];
int f[M][N] ;
int main() {
cin >> n >> m;
memset(g, 0x3f, sizeof g );
memset(f, 0x3f, sizeof f);
while (m--) {
int a, b, c;
cin >> a >> b >> c;
g[a][b] = c;
}
for (int i = 0 ; i < n ; ++i)
g[i][i] = 0;
f[1][0] = 0;
for (int i = 0 ; i < 1 << n ; ++i)
for (int j = 0 ; j < n ; ++j)
if (i >> j & 1) {
for (int k = 0 ; k < n ; ++k)
if ((i - (1 << j)) >> k & 1)
f[i][j] = min(f[i][j], f[i - (1 << j)][k] + g[k][j]);
}
int res = INF;
for (int i = 0 ; i < n ; ++i)
res = min(res, f[(1 << n) - 1][i] + g[i][0]);
if (res == INF)
cout << -1 << endl;
else
cout << res << endl;
return 0;
}
边栏推荐
- Wazuh开源主机安全解决方案的简介与使用体验
- Sword finger offer 05 Replace spaces
- 每日一题-无重复字符的最长子串
- lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
- CF1634 F. Fibonacci Additions
- 数仓项目的集群脚本
- 使用Electron开发桌面应用
- Introduction to convolutional neural network
- kubeadm系列-02-kubelet的配置和启动
猜你喜欢
Light a light with stm32
浅谈JVM(面试常考)
CF1634 F. Fibonacci Additions
剑指 Offer 58 - II. 左旋转字符串
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
sync.Mutex源码解读
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
用STM32点个灯
EOJ 2021.10 E. XOR tree
Yolov5 adds attention mechanism
随机推荐
Csp-j-2020-excellent split multiple solutions
Solution to game 10 of the personal field
过拟合与正则化
Control Unit 控制部件
Configuration and startup of kubedm series-02-kubelet
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
Alu logic operation unit
Sword finger offer 05 Replace spaces
Sword finger offer 53 - I. find the number I in the sorted array
R语言【数据集的导入导出】
剑指 Offer 35.复杂链表的复制
API related to TCP connection
Bit mask of bit operation
In this indifferent world, light crying
Yolov5 adds attention mechanism
剑指 Offer 53 - II. 0~n-1中缺失的数字
CF1637E Best Pair
sync.Mutex源码解读
Individual game 12
Add level control and logger level control of Solon logging plug-in