Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3001
The question is still about traveling around the map, but this time the traveler has strict requirements on himself. The number of passes on each point on the map cannot exceed two times.
Idea: it is still the problem of pressure DP, the root is very similar, but this time for each point there are three States, respectively, without passing through, after once, after twice. So we need to compress the state with a three-digit number. This key point is to understand that the other is basically the same as the previous one. For me, it should be noted that the first state that can reach a certain point after two passes is the state that this point has already passed, instead of the State that never goes through this point.
Code:
# Include <algorithm> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstring> # include <ctime> # include <ctype. h> # include <iostream> # include <map> # include <queue> # include <set> # include <stack> # include <string> # include <vector> # define EPS 1e-8 # define Inf (1 <30) -1 # define maxn 100005 # define PI ACOs (-1.0) # define seed 31 // 131,1313 typedef long ll; typedef unsigned long ull; using names Pace STD; int POW [15]; int cost [15] [15]; int DP [59500] [15]; void Init () {for (INT I = 0; I <10; I ++) {for (Int J = 0; j <59500; j ++) DP [J] [I] = inf ;} pow [0] = 1; DP [Pow [0] [0] = 0; For (INT I = 1; I <= 10; I ++) {POW [I] = POW [I-1] * 3; DP [Pow [I] [I] = 0;} For (INT I = 0; I <10; I ++) for (Int J = 0; j <10; j ++) cost [I] [J] = inf; return;} int main () {int N, m, K, Pos; ll ans; while (~ Scanf ("% d", & N, & M) {Init (); ans = inf; For (INT I = 0; I <m; I ++) {int x, y, z; scanf ("% d", & X, & Y, & Z ); if (cost [x-1] [Y-1]> Z) {cost [x-1] [Y-1] = z; cost [Y-1] [x-1] = z ;}} for (INT I = 1; I <POW [N]; I ++) {k = I; Pos = 0; while (k) {If (K % 3 = 1 | K % 3 = 2) {for (int ii = 0; II <n; II ++) {If (II = POS) continue; if (DP [I-pow [POS] [II] + cost [II] [POS] <DP [I] [POS]) DP [I] [POS] = DP [I-pow [POS] [II] + cost [II] [POS] ;}} K/= 3; pos ++ ;}} For (INT I = 1; I <POW [N]; I ++) {bool flag = 0; k = I; int T = 0; while (k) {T ++; If (K % 3 = 0) Flag = 1; k/= 3;} If (flag = 0 & t = N) {for (int ii = 0; II <n; II ++) if (DP [I] [II] <ans) ans = DP [I] [II];} if (ANS! = Inf) printf ("% i64d \ n", ANS); else puts ("-1") ;}return 0 ;}
HDU 3001 travelling pressure DP