[NOIp2017提高组]宝藏

 1 #include<cstdio>
 2 #include<cctype>
 3 #include<algorithm>
 4 inline int getint() {
 5     register char ch;
 6     while(!isdigit(ch=getchar()));
 7     register int x=ch^'0';
 8     while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
 9     return x;
10 }
11 const int inf=0x7fffffff;
12 const int N=12;
13 int n,w[N][N],dep[N],f[1<<N];
14 void dfs(const int state) {
15     for(register int i=0;i<n;i++) {
16         if(!(state&(1<<i))) continue;
17         for(register int j=0;j<n;j++) {
18             if(state&(1<<j)) continue;
19             if(w[i][j]==inf) continue;
20             if(f[state]+w[i][j]*(dep[i]+1)<f[state|(1<<j)]) {
21                 const int tmp=dep[j];
22                 dep[j]=dep[i]+1;
23                 f[state|(1<<j)]=f[state]+w[i][j]*(dep[i]+1);
24                 dfs(state|(1<<j));
25                 dep[j]=tmp;
26             }
27         }
28     }
29 }
30 int main() {
31     n=getint();
32     for(register int i=0;i<n;i++) {
33         for(register int j=0;j<n;j++) {
34             w[i][j]=inf;
35         }
36     }
37     for(register int m=getint();m;m--) {
38         const int u=getint()-1,v=getint()-1;
39         w[u][v]=w[v][u]=std::min(w[u][v],getint());
40     }
41     int ans=inf;
42     for(register int i=0;i<n;i++) {
43         for(register int i=0;i<n;i++) dep[i]=inf;
44         for(register int i=1;i<(1<<n);i++) f[i]=inf;
45         dep[i]=f[1<<i]=0;
46         dfs(1<<i);
47         ans=std::min(ans,f[(1<<n)-1]);
48     }
49     printf("%d\n",ans);
50     return 0;
51 }

 

posted @ 2017-12-05 20:48  skylee03  阅读(229)  评论(0编辑  收藏  举报