当前位置:网站首页>Shortest Hamilton path (pressure DP)
Shortest Hamilton path (pressure DP)
2022-07-06 12:57:00 【Classmate Chen_】
subject
Given a sheet n n n A weighted undirected graph of points , Point from 0 ∼ n − 1 0∼n−1 0∼n−1 label , 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 repetition or leakage .
Input format
Enter the integer in the first line n n n.
Next n Every line n n n It's an integer , Among them the first i i i Xing di j j j An integer represents a point i i i To j j j Distance of ( Write it down as a [ i , j ] a[i,j] a[i,j]).
For arbitrary x , y , z x,y,z x,y,z, Data assurance a [ x , x ] = 0 a[x,x]=0 a[x,x]=0, a [ x , y ] = a [ y , x ] a[x,y]=a[y,x] a[x,y]=a[y,x] also a [ x , y ] + a [ y , z ] ≥ a [ x , z ] a[x,y]+a[y,z]≥a[x,z] a[x,y]+a[y,z]≥a[x,z].
Output format
Output an integer , Means shortest Hamilton The length of the path .
Data range
1 ≤ n ≤ 20 1≤n≤20 1≤n≤20
0 ≤ a [ i , j ] ≤ 1 0 7 0≤a[i,j]≤10^7 0≤a[i,j]≤107
sample input :
5
0 2 4 5 1
2 0 6 5 3
4 6 0 8 3
5 5 8 0 5
1 3 3 5 0
sample output :
18
Pressure DP
f [ i ] [ j ] : f [ i ] [ j ] surface in from 0 go To j , go too Of the Yes spot yes i Of the Yes road path f[i][j]:f[i][j] From 0 Go to the j, All the points passed are i All the paths f[i][j]:f[i][j] surface in from 0 go To j, go too Of the Yes spot yes i Of the Yes road path
1. According to the meaning of the above state , We'll think about it very well
2. Since each f[i][j] It means from 0 Go to the j, All the points reached are i The path of , How to divide sets ?
3. Find the last difference of each scheme according to , We can find that each point can come from other points
4. If we start from a certain point k Come to the point j, Then our current state does not include j Of , Because I haven't reached j
5. according to 4 We can draw f[i][j] Can be f[i-(j)][k]+g[j][k]. explain (j):j Binary representation of this point ,g[j][k]:j Go to the k Distance of
6. Define according to the state : initialization f For negative infinity ——————————f[1][0]=0:(0 The distance to oneself must be 0)
7. The final answer is to go to n-1 Number point , And all points have passed :f[(1<<n)-1][n-1]
AC Code (C++)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=20,M=1<<N;
int f[M][N]; //f[i][j] From 0 Go to the j, All the points passed are i All the paths example :i=110011(0 It means you haven't walked ,1 Representative walk , And on the far right is 0 This point )
int n;
int w[N][N]; //w[i][j] Express i To j The distance from this point
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&w[i][j]);
}
}
memset(f,0x3f,sizeof f); // Because the shortest path is required , Initialize to positive infinity
f[1][0]=0; // The first point is that there is no cost
for(int i=0;i<1<<n;i++){
// Enumerate all States of points
for(int j=0;j<n;j++){
// Number of enumeration points
if(i>>j&1){
//i>>j: seek i Of the j Digit number . If &1 establish , Then represent j This point has passed
for(int k=0;k<n;k++){
//k->j
if(i>>k&1 && j!=k){
// Just like the above judgment , Express k This point goes by and is not yourself
f[i][j]=min(f[i][j],f[i-(1<<j)][k]+w[k][j]); // Judge whether this scheme is the best or from i->k, Again from k->j The optimal
}
}
}
}
}
printf("%d",f[(1<<n)-1][n-1]); // Optimal solution
return 0;
}
边栏推荐
- Programming homework: educational administration management system (C language)
- Combination of fairygui check box and progress bar
- (课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
- C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
- [算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
- FairyGUI复选框与进度条的组合使用
- Introduction to the daily practice column of the Blue Bridge Cup
- [rtklib 2.4.3 B34] version update introduction I
- In 2020, the average salary of IT industry exceeded 170000, ranking first
- Itext 7 生成PDF总结
猜你喜欢
![[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once](/img/f7/23ffc81ec8e9161c15d863c1a67916.png)
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
![[algorithm] sword finger offer2 golang interview question 5: maximum product of word length](/img/e0/cea31070d6365eb57013cdead4a175.png)
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length

KF UD分解之UD分解基础篇【1】

Design and implementation of general interface open platform - (39) simple and crude implementation of API services

Theoretical derivation of support vector machine
![[算法] 劍指offer2 golang 面試題2:二進制加法](/img/c2/6f6c3bd4d70252ba73addad6a3a9c1.png)
[算法] 劍指offer2 golang 面試題2:二進制加法

Naive Bayesian theory derivation

Unity3D,阿里云服务器,平台配置

Derivation of logistic regression theory
![[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数](/img/64/0f352232359c7d44f12b20a64c7bb4.png)
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
随机推荐
编辑距离(多源BFS)
FairyGUI条子家族(滚动条,滑动条,进度条)
Fairygui gain buff value change display
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
[algorithm] sword finger offer2 golang interview question 4: numbers that appear only once
Fairygui character status Popup
[GNSS data processing] Helmert variance component estimation analysis and code implementation
Prove the time complexity of heap sorting
Unity3D,阿里云服务器,平台配置
Teach you to release a DeNO module hand in hand
GPS高程拟合抗差中误差的求取代码实现
FairyGUI人物状态弹窗
Novatel board oem617d configuration step record
KF UD decomposition pseudo code implementation advanced [2]
Fabrication d'un sac à dos simple fairygui
异常:IOException:Stream Closed
What are the functions and features of helm or terrain
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践