HDU 3001 Travelling state compression dp + 3 hexadecimal, hdudp
A person wants to travel, he wants to go to n places, and each place can walk up to 2 times;
Give m paths and query the minimum cost
Obviously, the status is compressed, but each vertex can only be taken twice at most, so it cannot mark the current vertex that has passed or not passed through. It can only be represented by a three-step mechanism.
1 represents location 1 was taken once,
2 indicates that location 1 has been visited twice,
3 (that is, 10) represents Location 2 was taken once,
4 (11) indicates that location 1 is taken once, Location 2 is taken once,
5 (that is, 12) indicates that location 1 has been walked twice, Location 2 has been walked once,
Attachment AC code
# Include <stdio. h> # include <string. h> int dis [15], dp [80000] [15], dpp [80000] [15]; int map [15] [15]; int min1 (int, 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", & n, & m )! = EOF) {int sum; memset (map, 127, sizeof (map); sum = map [0] [0]; int cnt = map [0] [0]; while (m --) {scanf ("% d", & I, & j, & k); if (k <map [I] [j]) // duplicate 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; // if (dpp [k] [j]> = 2) does not need to be updated in the same location) continue; // No updates are required twice, because if (map [I] [j] = cnt) continue cannot be followed; // map [I] [j] = cnt indicates that there is no route from I to j, so you do not need to 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 ;}