Test instructions: A person to travel, he will go to n places, and these n places in each place can walk up to 2 times;
To M-Path, asking for the shortest cost
Obviously state compression, but requires that each point can only walk two times, there is no way to mark the current point walked or did not go through, can only be used to represent the three
1 Representative Location 1 was traversed once,
2 Representative Location 1 was traversed two times,
3 (i.e. 10) represents the location 2 was traversed once,
4 (i.e. 11) represents the location 1 was traversed once, place 2 was traversed once,
5 (i.e. 12) represents the location 1 was traversed two times, place 2 was traversed once,
AC Code included
#include <stdio.h> #include <string.h>int dis[15],dp[80000][15],dpp[80000][15];int map[15][15];int min1 ( int A,int b) {if (a<b) return A;return B;} int main () {int i,j,n,m,k;j=1;for (i=1;i<=11;i++) {dis[i]=j;j*=3;} for (i=0;i<dis[11];i++) {k=i;for (j=1;j<=11;j++) {dpp[i][j]=k%3;k/=3;}} while (scanf ("%d%d", &n,&m)!=eof) {int Sum;memset (map,127,sizeof (map)); Sum=map[0][0];int cnt=map[0][0]; while (m--) {scanf ("%d%d%d", &i,&j,&k), if (K<map[i][j])//heavy-edged map[j][i]=map[i][j]=k;} Memset (Dp,127,sizeof (DP)); for (i=1;i<=n;i++) {dp[dis[i]][i]=0;//}for (k=0;k<dis[n+1];k++) {int tot=1;for (i=1; i<=n;i++) {if (dpp[k][i]==0) tot=0;if (dp[k][i]==cnt) continue;for (j=1;j<=n;j++) {if (i==j) continue;//same location without update if (dpp[k][j]>=2) continue;//two times do not update, because can not go again if (map[i][j]==cnt) continue;//map[i][j]==cnt means there is no way from I to J no update int l=k+ DIS[J];DP [L][j]=min1 (Dp[l][j],dp[k][i]+map[i][j]);}} if (tot) {for (i=1;i<=n;i++) {sum=min1 (sum,dp[k][i]);}}} if (sum==cnt) sum=-1;printf ("%d\n", sum);} return 0; }
HDU 3001 Travelling State compression dp+3 binary