当前位置:网站首页>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 *
*,°:.*( ̄▽ ̄)/$:.°* .
边栏推荐
- Sql文件导入数据库-保姆级教程
- EF core: self referencing organizational structure tree
- Pain and happiness -nio programming
- BGP机房和BGP
- [acwing周赛复盘] 第 61 场周赛20220723
- [untitled]
- Install K6 test tool
- [help] mindspire training based on ascend910 cannot reproduce the model effect on GPU
- LeetCode_6124_第一个出现两次的字母
- Grafana connection tdengine reports an error 535
猜你喜欢

Automated test series selenium three kinds of waiting for detailed explanation

Grafana - influxdb visual K6 output

Managing databases in a hybrid cloud: eight key considerations

Implement a avatar looping control

C # "learning code snippet" - recursively obtain all files under the folder

Oracle is not null cannot filter null values

ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置

QT learning - using database singleton to complete login matching + registration function
![[LeetCode周赛复盘] 第 83 场双周赛20220723](/img/db/c264c94ca3307d4363d3cf7f5d770b.png)
[LeetCode周赛复盘] 第 83 场双周赛20220723

Upload and download filask files
随机推荐
Current events on July 20
Simple use of mongodb database
Docker container Django + MySQL service
Promtool Check
Managing databases in a hybrid cloud: eight key considerations
Wechat applet development learning 5 (custom components)
Redis memory analysis tool RMA usage
Click the "native practice" search box to expand the special effect so that you can realize it. How will you realize it?
SQL file import database - Nanny level tutorial
Quartus: install cyclone 10 LP device library for quartus version 17.1
Moonpdflib Preview PDF usage record
2022 Henan Mengxin League game 2: Henan University of technology K - Rice
Number of palindromes in question 5 of C language deduction (two methods)
[mindspore] [xception model] script statement is suspected to be wrong
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
[untitled]
Verification of Kirchhoff's law and Multisim Simulation (engineering documents attached)
GUI basic application
Exception, import package and file operation
动态规划-01背包滚动数组优化