标签:pre stat ring 压缩 namespace line ios 路径 哪些
题目描述:n个点的带权无向图,从0-n-1,求从起点0到终点n-1的最短Hamilton路径(Hamilton路径:从0-n-1不重不漏的每个点恰好进过一次)
#include <iostream>
#include <cstring>
using namespace std;
int f[1<<20][20];//0-19 location; status,loction
int w[20][20];
int main(){
int n;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));
f[1][0]=0;
int im=1<<n;
int ps;
for(int i=1;i<im;i++)
for(int j=0;j<n;j++)if((i>>j)&1)//the previous status is available
{
ps=i^(1<<j);//the previous status
for (int k = 0; k < n;k++)if(ps&(1<<k))//find the previous available location
f[i][j] = min(f[i][j], f[ps][k] + w[k][j]);
}
printf("%d",f[(1<<n)-1][n-1]);
return 0;
}
标签:pre stat ring 压缩 namespace line ios 路径 哪些
原文地址:https://www.cnblogs.com/sstealer/p/11112392.html