本题目用状态压缩搜索,注意要从0状态开始搜索,否则一些不合法的状态也会一直搜下去,无法进行剪枝。
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1<<13;
int f[maxn],l[15],pic[14][14],i,j,n,m;
inline void dfs(int x){
for(int i=1;i<=n;i++){
if(!((1<<(i-1))&x))continue;
for(int j=1;j<=n;j++)
if((((1<<(j-1))|x)>x)&&pic[i][j]!=inf){
if(f[x|(1<<(j-1))]>f[x]+l[i]*pic[i][j]){
f[x|(1<<(j-1))]=f[x]+l[i]*pic[i][j];
l[j]=l[i]+1;
dfs(x|(1<<(j-1)));
}
}
}
}
int main(){
int a,b,c;
memset(f,0x3f,sizeof(f));
memset(pic,0x3f,sizeof(pic));
memset(l,0x3f,sizeof(l));
cin>>n>>m;
for (i=1;i<=m;++i){
scanf("%d%d%d",&a,&b,&c);
pic[a][b]=min(pic[a][b],c);
pic[b][a]=pic[a][b];
}
int ans=inf;
for(int i=1;i<=n;i++){
memset(l,0x3f,sizeof(l));
memset(f,0x3f,sizeof(f));
l[i]=1;
f[1<<(i-1)]=0;
dfs(1<<(i-1));
ans=min(ans,f[(1<1]);
}
cout<return 0;
}