Test instructions
Give n points m without an edge.
Each point is required to walk up to two times, to access all points given the minimum sum of weights required for the side of the route.
Ideas:
Tri-State Compression dp,0 representative walked 0 times, by analogy.
The first time to get the three-state compression DP, the sense of focus is the preprocessing of data, the use of the array decomposition of the individual bits, so as to achieve the purpose of similar binary.
Then is the representation of the state, Dp[s][i] represents the optimal value of the state s when it reaches I.
State transfer also at a glance, not nonsense.
#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;Const intinf=0x3f3f3f3f;intpho[ the][ the];intunit[ A];intdic[60000][ One];intdp[60000][ One];inlinevoidinit () {memset (dic,0,sizeof(DIC)); unit[0]=1; for(intI=1; i<= One; i++) {Unit[i]=3*unit[i-1]; } for(intI=0; i<=59049; i++) { inttmp=i; for(intj=0; tmp;j++) {Dic[i][j]=tmp%3; TMP/=3; } }}intMain () {intn,m; Init (); while(SCANF ("%d%d", &n,&m)! =EOF) { for(intI=1; i<=n;i++) { for(intj=1; j<=n;j++) {Pho[i][j]=inf; } Pho[i][i]=0; } for(intI=1; i<=m;i++) { intA,b,c; scanf ("%d%d%d",&a,&b,&c); PHO[A][B]=pho[b][a]=min (pho[a][b],c); } intEd=1; for(intI=1; i<=n;i++) {Ed*=3; } Ed--; for(ints=0; s<=ed;s++) { for(intI=1; i<=n;i++) {Dp[s][i]=inf; } } BOOLOK; intans=inf; for(ints=0; s<=ed;s++) {OK=1; for(intI=1; i<=n;i++) { if(dic[s][i-1]) { if(s==unit[i-1]) {Dp[s][i]=0; } Else { for(intk=1; k<=n;k++) { if(i!=k&& (dic[s][k-1])) {Dp[s][i]=min (dp[s][i],dp[s-unit[i-1]][k]+Pho[k][i]); } } } } Else{OK=0; } } if(OK) { for(intI=1; i<=n;i++) {ans=min (ans,dp[s][i]); } } } if(ans>=inf) ans=-1; printf ("%d\n", ans); }}
HDU 3001 "state compression DP"