[NOIp2017提高组]宝藏

时间:2023-11-24 13:23:26
 #include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int inf=0x7fffffff;
const int N=;
int n,w[N][N],dep[N],f[<<N];
void dfs(const int state) {
for(register int i=;i<n;i++) {
if(!(state&(<<i))) continue;
for(register int j=;j<n;j++) {
if(state&(<<j)) continue;
if(w[i][j]==inf) continue;
if(f[state]+w[i][j]*(dep[i]+)<f[state|(<<j)]) {
const int tmp=dep[j];
dep[j]=dep[i]+;
f[state|(<<j)]=f[state]+w[i][j]*(dep[i]+);
dfs(state|(<<j));
dep[j]=tmp;
}
}
}
}
int main() {
n=getint();
for(register int i=;i<n;i++) {
for(register int j=;j<n;j++) {
w[i][j]=inf;
}
}
for(register int m=getint();m;m--) {
const int u=getint()-,v=getint()-;
w[u][v]=w[v][u]=std::min(w[u][v],getint());
}
int ans=inf;
for(register int i=;i<n;i++) {
for(register int i=;i<n;i++) dep[i]=inf;
for(register int i=;i<(<<n);i++) f[i]=inf;
dep[i]=f[<<i]=;
dfs(<<i);
ans=std::min(ans,f[(<<n)-]);
}
printf("%d\n",ans);
return ;
}