当前位置:网站首页>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 *
*,°:.*( ̄▽ ̄)/$:.°* .
边栏推荐
- Regular expression learning
- @Mapkey usage instructions
- [mindspore] [training warning] warning when executing training code
- 动态规划-01背包滚动数组优化
- Improve static loading dynamic loading
- Upload and download filask files
- Where is the most formal account opening for futures trading? Capital security?
- What is the function of transdata operator and whether it can optimize performance
- Add the two numbers in the linked list of the second question of C language. Ergodic method
- MySQL common basic commands
猜你喜欢

If real-time intersection with line segments in online CAD drawings is realized

The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode

2. Load test

Detailed usage of iperf

Quartus: install cyclone 10 LP device library for quartus version 17.1

Qt学习-利用数据库单例完成 登录匹配 + 注册 功能实现

How to use measurement data to drive the improvement of code review

Principle of data proxy

在混合云中管理数据库:八个关键注意事项

Use es to realize fuzzy search and search recommendation of personal blog
随机推荐
===、==、Object. Is basic package type
启牛商学院靠谱吗?讲课老师推荐开华泰账户安全吗
[mindspore] [mode] spontaneous_ The difference between mode and graph mode
Measurement and Multisim Simulation of volt ampere characteristics of circuit components (engineering documents attached)
Verification of Kirchhoff's law and Multisim Simulation (engineering documents attached)
Number of palindromes in question 5 of C language deduction (two methods)
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
If real-time intersection with line segments in online CAD drawings is realized
LeetCode_ 6124_ The first letter that appears twice
BGP machine room and BGP
Divide 300000 bonus! Deeperec CTR model performance optimization Tianchi challenge is coming
The use of Multimeter in circuit analysis experiment of Shandong University
[leetcode weekly replay] game 83 biweekly 20220723
First experience of flask
3. Pressure test
Daily question 1 · 1260. Two dimensional network migration · simulation
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
The troubleshooting process of a segment error (disassembly address troubleshooting)
Redis memory analysis tool RMA usage
Advanced function of postman