#include<bits/stdc++.h>
using namespace std;
int n,m,val[12][12],ans,bestn[12],best,ii;
int to[12][13];//数量,12*边。
struct node{
int w,dp[12];//dp[i][j]点i深度为j
}f[65536];
void find(int k)
{
if(k==(1<<n)-1)
{
//printf("#%d&%d\n",f[k].w,best);
ans=min(f[k].w,ans);
return;
}
if(f[k].w+best>=ans)return;
int mk,j;
for(int i=0;i<n;i++)
{
if(!(k&(1<<i)))continue;
if(f[k].w+best*f[k].dp[i]>=ans)continue;
for(int jj=1;jj<=to[i][0];jj++)
{
j=to[i][jj];
if(k&(1<<j))continue;
mk=k|(1<<j);
memcpy(f[mk].dp,f[k].dp,sizeof(f[k].dp));
f[mk].w=f[k].w+f[k].dp[i]*val[i][j];
f[mk].dp[j]=f[k].dp[i]+1;
best-=bestn[j];
find(mk);
best+=bestn[j];
}
}
}
void solve(int rt)
{
memset(f,0,sizeof(f));
f[1<<rt].w=0;
f[1<<rt].dp[rt]=1;
best-=bestn[rt];
find(1<<rt);
best+=bestn[rt];
}
bool cmp(int a,int b)
{
return val[ii][a]<val[ii][b];
}
int main()
{
freopen("test.in","r",stdin);
int m1,m2,m3;
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)val[i][j]=0x7fffffff;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&m1,&m2,&m3);
m1--;m2--;
val[m1][m2]=min(val[m1][m2],m3);
val[m2][m1]=min(val[m2][m1],m3);
}
for(int i=0;i<n;i++)
{
m1=0;
for(int j=0;j<n;j++)
{
if(val[i][j]==0x7fffffff)continue;
to[i][++m1]=j;
}
to[i][0]=m1;
ii=i;
sort(to[i]+1,to[i]+1+m1,cmp);
bestn[i]=val[i][to[i][1]];
best+=bestn[i];
}
ans=0x7fffffff;
for(int i=0;i<n;i++)solve(i);
cout<<ans;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 305.08 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #2 | 589.44 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #3 | 804.09 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #4 | 803.51 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #5 | 806 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #6 | 722.1 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #7 | 651.45 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #8 | 720.51 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #9 | 633.66 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #10 | 693.42 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #11 | 784.77 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #12 | 777.92 us | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #13 | 1.021 ms | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #14 | 1.04 ms | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #15 | 72.221 ms | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #16 | 56.941 ms | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #17 | 178.327 ms | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #18 | 117.379 ms | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #19 | 674.244 ms | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |
Testcase #20 | 574.772 ms | 3 MB + 292 KB | Accepted | Score: 5 | 显示更多 |