Test instructions: 10 points, several edges, side has spent, each point up to two times, beg to pass all points, spend the least
Analysis: Because each point up to two times, so Lenovo to 3 binary, and then enumerate the state, it is OK (I also follow the online God code written)
#include <cstdio>#include<iostream>#include<cstring>#include<cstdlib>#include<vector>#include<string>#include<cmath>#include<algorithm>#include<map>using namespaceStd;typedefLong LongLL;Const intn=6e4;Const intinf=0x3f3f3f3f;intbit[ A]= {0,1,3,9, -,Bayi,243,729,2187,6561,19683,59049};intvis[n][ A],dp[n][ A],mp[ A][ A];intMain () { for(intI=1; i<59049; ++i) {inttmp=i; for(intj=1; j<=Ten; ++j) Vis[i][j]=tmp%3, tmp/=3; } intn,m; while(~SCANF ("%d%d",&n,&m)) { for(intI=1; i<bit[n+1]; ++i) for(intj=1; j<=n; ++j) Dp[i][j]=INF; for(intI=1; i<=n; ++i) for(intj=1; j<=n; ++j) Mp[i][j]=INF; for(intI=0; i<m; ++i) {intu,v,w; scanf ("%d%d%d",&u,&v,&W); MP[U][V]=min (mp[u][v],w); Mp[v][u]=min (mp[v][u],w); } for(intI=1; i<=n; ++i) dp[bit[i]][i]=0; intans=INF; for(intI=1; i<bit[n+1]; ++i) {BOOLflag=1; for(intj=1; j<=n; ++j) {if(!Vis[i][j]) {Flag=0; Continue; } if(Dp[i][j]==inf)Continue; for(intk=1; k<=n; ++k) {if(K==J)Continue; if(mp[j][k]==inf| | vis[i][k]>=2) Continue; Dp[i+bit[k]][k]=min (dp[i+bit[k]][k],dp[i][j]+Mp[j][k]); } } if(flag) for(intj=1; j<=n; ++j) Ans=min (ans,dp[i][j]); } if(Ans==inf) printf ("-1\n"); Elseprintf"%d\n", ans); } return 0;}
View Code
HDU 3001 Travelling 3 binary pressure DP