Title Description
Given onen”>nWeighted undirected graph of points from0∼n−1″>0∼ n − 1 label, find the starting point0″>0To the endn−1″>n−1The shortest Hamiltonian path.
Hamilton path is defined from0″>0Ton−1″>n−1Pass each point exactly once without repetition and leakage.
Input format
Enter an integer on the first linen”>n。
Nextn”>nLine per linen”>N # integers, where thei”>iLine numberj”>J) an integer representing a pointi”>I # toj”>J , distance (recorded asa[i,j]”>a[i,j])。
For arbitraryx,y,z”>x,y,zData assurancea[x,x]=0,a[x,y]=a[y,x]”>a[x,x]=0,a[x,y]=a[y,x]A [x, x] = 0, a [x, y] = a [y, x] anda[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 representing the length of the shortest Hamilton path.
The shape pressing DP question is also a wonderful question
Because it’s not heavy and doesn’t miss all points, 1<
Equation: F [i] [J] = min{f [I ^ (1)<>j)&1)=1 i^(1<2*2n。
The code is short, but it’s hard to think about.
1 #include
2 using namespace std;
3 int a[21][21],f[1<<20][20];
4
5 int main(){
6 int n;cin>>n;
7 for(int i=0;i>a[i][j];
10 memset(f,0x3f,sizeof(f));
11 f[1][0]=0;
12 for(int i=0;i>j&1)
14 for(int k=0;k>k&1)
15 f[i][j]=min(f[i][j],f[i^1<