91. 最短Hamilton路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 20;
int w[N][N];
int f[1<<N][N],n;
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ )
{
for (int j = 0; j < n; j ++ )
{
cin>>w[i][j];
}
}
memset(f,0x3f,sizeof(f));
f[1][0]=0;
for (int i = 1; i < (1<<n); i ++ )
{
for(int j=0;j<n;j++)
{
if(i>>j&1)
{
for(int k=0;k<n;k++)
{
if(i-(1<<j)>>k&1)
{
f[i][j]=min(f[i-(1<<j)][k]+w[k][j],f[i][j]);
}
}
}
}
}
cout<<f[(1<<n)-1][n-1]<<endl;
return 0;
}
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022-2024 CPY
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信