Acwing shortest Hamilton distance (pressure DP)


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″>n1Pass 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]=0a[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];
 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<


Recommended Today

Android advanced learning (XXIII) story of adding clickablespan to textview

In Android, if you want to realize that some text in a text can be clicked, you can use clickablespan in the approximate way tv = (TextView) findViewById(; Spannablestringbuilder builder = new spannablestringbuilder (“this is a connection”); builder.setSpan(new ClickableSpan() { @Override public void onClick(@NonNull View widget) { tv.setBackgroundColor(Color.GREEN); } }, 3, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv.setText(builder); tv.setMovementMethod(LinkMovementMethod.getInstance()); […]