当前位置:网站首页>Solution to the shortest Hamilton path problem
Solution to the shortest Hamilton path problem
2022-07-25 00:35:00 【Aurora_ yxy】
Title Description
Given a sheet n ( n < = 20 ) n(n<=20) n(n<=20) A weighted undirected graph of points , Point from 0 n − 1 0~n-1 0 n−1 Mark well , Find the starting point 0 0 0 To the end point n − 1 n-1 n−1 The shortest of h a m i l t o n hamilton hamilton route
h a m i l t o n hamilton hamilton The definition of path is from 0 0 0 To n − 1 n-1 n−1 Pass through every point exactly once without weight or leakage .
Input
first line n n n
Next one n ∗ n n*n n∗n The adjacency matrix of , a [ i ] [ j ] a[i][j] a[i][j] From point to point i point-to-point j Distance between points of
Output
a line , The shortest h a m i l t o n hamilton hamilton The length of the path
Examples
The sample input 1
2
0 1
1 0
Sample output 1
1
solution
First, violence , Think of every point d f s dfs dfs
however !
The time complexity of the search is O ( n ! ) O(n!) O(n!), Consider optimizing
We notice every moment
If you know which points have been passed and the current point
You can make the next decision
And the decision has nothing to do with the order in which these points are passed
Make F [ i ] [ S ] F[i][S] F[i][S] The set of points that have been passed is S S S, At present The location is i i i Minimum path length
How to represent a collection :
take S S S As a n n n Bit binary number , The first i i i A point was passed , If and only if S S S The binary number of i i i Is it 1
Solving object : F [ n ] [ ( 1 < < n ) − 1 ] F[n][(1<<n) -1] F[n][(1<<n)−1], It means that all points have been passed
AC code
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
memset(dp,0x3f,sizeof(dp));
dp[1][1]=0;// Select only 1 of course by 0
for(int j=1;j<(1<<n);j++){
for(int i=2;i<=n;i++){
if((1<<(i-1))&j)continue;// state j Already contains i
for(int k=1;k<=n;k++){
if(k==i)continue;//dp[i][j]: This election i, Status as j Length of hour
dp[i][j+(1<<(i-1))]=min(dp[i][j+(1<<(i-1))],dp[k][j]+a[k][i]);
}
}
}
cout<<dp[n][(1<<n)-1];// It's over
return 0;
}
End of the flower *
*,°:.*( ̄▽ ̄)/$:.°* .
边栏推荐
- [acwing周赛复盘] 第 61 场周赛20220723
- Upload and download filask files
- Mobile terminal touch event
- asp adodb.stream对象的方法属性
- 如何创建索引
- Simple operation K6
- [LeetCode周赛复盘] 第 303 场周赛20220724
- Grafana connection tdengine reports an error 535
- C recursively obtains all files under the folder and binds them to the treeview control
- Lambda&Stream
猜你喜欢

Divide 300000 bonus! Deeperec CTR model performance optimization Tianchi challenge is coming
![[mindspore] [training warning] warning when executing training code](/img/5a/978889301bdd02d6474c6e5723ad97.jpg)
[mindspore] [training warning] warning when executing training code
![[mindspore ascend] [running error] graph_ In mode, run the network to report an error](/img/81/9e96182be149aef221bccb63e1ce96.jpg)
[mindspore ascend] [running error] graph_ In mode, run the network to report an error
![[acwing周赛复盘] 第 61 场周赛20220723](/img/8b/df2c8d516db1e7e5f2d50bcf62b2b1.png)
[acwing周赛复盘] 第 61 场周赛20220723

C语言学习之分支与循环语句

Uncaught typeerror: cannot read properties of null (reading 'append') solution

2. Load test

7.24 party notice

Improve static loading dynamic loading
![[untitled]](/img/70/5db8a8df63a3fd593acf7f69640698.png)
[untitled]
随机推荐
Advanced function of postman
[hero planet July training leetcode problem solving daily] 24th line segment tree
Docker container Django + MySQL service
Multi merchant mall system function disassembly Lecture 14 - platform side member level
EF core: self referencing organizational structure tree
LeetCode_ 392_ Judgement subsequence
px rem em
R language plot visualization: plot to visualize the residual analysis diagram of the regression model, the scatter diagram of the predicted value and residual corresponding to the training set and th
MySQL的最左前缀原则
QT project - security monitoring system (function realization of each interface)
C语言学习之分支与循环语句
Codeworks round 650 (Div. 3) ABCD solution
ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
【无标题】
#648 (Div. 2)(A. Matrix Game、B. Trouble Sort、C. Rotation Matching)
LeetCode_392_判断子序列
ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
UXDB在不知道明文密码的情况下重置密码
Redis6.2 SYSTEMd startup prompt redis service: Failed with result ‘protocol‘.
Number of palindromes in question 5 of C language deduction (two methods)