N cities and M roads, each of which has its own price. Each city can reach a maximum of two times, and the minimum cost and the starting point of any city can be reached.
Triplicate pressure stores the number of times each city passes in each State.
Transfer equation: DP [I + B [k] [k] = min (DP [I + B [k] [K], DP [I] [J] + dis [J] [k]);
# Include "stdio. H "# include" string. H "const int INF = 0x3f3f3f; int B [15], Mark [60010] [15], DP [60010] [15], DIS [15] [15]; int min (int A, int B) {if (a <B) return a; else return B;} int main () {int I, j, n, m, K, temp, ANS, flag; B [0] = 1; for (I = 1; I <= 10; I ++) B [I] = B [I-1] * 3; for (I = 0; I <B [10]; I ++) // records the passing status of each city in each State. {Temp = I; for (j = 0; j <10; j ++) {mark [I] [J] = TEMP % 3; temp/= 3 ;}} while (scanf ("% d", & N, & M )! = EOF) {memset (DIS, INF, sizeof (DIS); memset (DP, INF, sizeof (DP); While (M --) {scanf ("% d", & I, & J, & K); I --; j --; If (DIS [I] [J]> K) dis [I] [J] = dis [J] [I] = K;} for (I = 0; I <n; I ++) DP [B [I] [I] = 0; ans = inf; for (I = 0; I <B [N]; I ++) {flag = 1; for (j = 0; j <n; j ++) {If (MARK [I] [J] = 0) Flag = 0; if (DP [I] [J] = inf) continue; For (k = 0; k <n; k ++) if (K! = J & Mark [I] [k] <2 & dis [J] [k]! = Inf) DP [I + B [k] [k] = min (DP [I + B [k] [K], DP [I] [J] + dis [J] [k]);} If (flag = 1) For (j = 0; j <n; j ++) ans = min (ANS, DP [I] [J]);} If (ANS = inf) printf ("-1 \ n "); else printf ("% d \ n", ANS);} return 0 ;}
HDU 3001 pressure DP