Given a grid chart, there are some points in the graph that need to go all over. Ask the minimum cost, enter from any boundary, and go out of any boundary. if not all the points can be reached, output 0.
Solution: One spfa starts from all points on the boundary, calculates the shortest path of K treasures, then calculates the shortest path between the two treasures, and compresses the enumeration state.
The Code is as follows:
# Include <cstdlib> # include <cstdio> # include <cstring> # include <iostream> # include <algorithm> using namespace STD; // remember to take away all the items const int INF = 0x3f3f3f3f; int n, m, K; int MP [205] [205]; int idis [15] [15]; // This 15*15 matrix is used to keep the shortest distance between treasures int odis [15]; // The shortest distance from the boundary to k locations struct node {int X, y;} p [15]; int que [1000005]; int dis [40005]; char vis [40005]; int dir [4] [2] = {0, 1, 0,-1, 1, 0,-1, 0}; bool legal (int x, int y) {If (x <0 | x> = n | Y <0 | Y> = m) return false; return true;} void spfa (int sta, int num) {int front = 0, tail = 0; memset (DIS, 0x3f, sizeof (DIS); memset (VIS, 0, sizeof (VIS )); que [tail ++] = sta; DIS [sta] = 0, vis [sta] = 1; while (front <tail) {int cur = que [Front ++], NXT; vis [cur] = 0; int x = cur/m, y = cur % m; int XX, YY; For (int K = 0; k <4; ++ K) {xx = x + dir [k] [0], YY = y + Dir [k] [1]; NXT = XX * m + YY; If (Legal (XX, YY )) {If (DIS [NXT]> dis [cur] + MP [XX] [YY]) {dis [NXT] = dis [cur] + MP [XX] [YY]; if (! Vis [NXT]) {vis [NXT] = 1; que [tail ++] = NXT ;}}}for (INT I = 0; I <K; ++ I) {idis [num] [I] = dis [p [I]. x * m + P [I]. y] ;}} void bspfa () {int front = 0, tail = 0; memset (DIS, 0x3f, sizeof (DIS); memset (VIS, 0, sizeof (VIS); For (INT I = 0; I <n; ++ I) {int k1 = I * m, K2 = I * m + M-1; que [tail ++] = K1, que [tail ++] = k2; DIS [k1] = MP [I] [0], dis [k2] = MP [I] [M-1]; // the boundary points are added with an initial distance of 0. vis [k1] = Vis [k2] = 1;} For (Int J = 1; j <m-1; ++ J) {int k1 = J, K2 = (N-1) * m + J; que [tail ++] = K1, que [tail ++] = k2; DIS [k1] = MP [0] [J], dis [k2] = MP [N-1] [J]; vis [k1] = vis [k2] = 1;} while (front <tail) {int cur = que [Front ++], NXT; vis [cur] = 0; int x = cur/m, y = cur % m; int XX, YY; for (int K = 0; k <4; ++ K) {xx = x + dir [k] [0], YY = Y + dir [k] [1]; NXT = XX * m + YY; If (Legal (XX, YY )) {If (DIS [NXT]> dis [cur] + MP [XX] [YY]) {dis [NXT] = dis [cur] + MP [XX] [YY]; if (! Vis [NXT]) {vis [NXT] = 1; que [tail ++] = NXT ;}}}for (INT I = 0; I <K; ++ I) {odis [I] = dis [p [I]. x * m + P [I]. y] ;}} int f [13] [1 <13]; // F [I] [J] indicates that the status is J, and the last path is the minimum overhead of I int DFS (INT STA, int NXT) {If (~ F [NXT] [sta] & NXT! =-1) {return f [NXT] [sta];} If (STA = 0) {return f [NXT] [sta] = odis [NXT]; // start from NXT} int ret = inf; For (INT I = 0; I <K; ++ I) {If (STA & (1 <I )) {If (NXT! =-1) ret = min (Ret, DFS (STA ^ (1 <I), I) + idis [I] [NXT]); else ret = min (Ret, DFS (STA ^ (1 <I), I) + odis [I]-MP [p [I]. x] [p [I]. y]); // The return f [NXT] [sta] = ret;} int solve () {memset (F, 0x3f, sizeof (f); int mask = 1 <K; For (INT I = 0; I <K; ++ I) f [I] [1 <I] = odis [I]; for (INT I = 2; I <mask; ++ I) {If (! (I-(I & (-I) continue; // if only one digit is 1 for (Int J = 0; j <K; ++ J) {If (! (I & (1 <j) continue; For (int K = 0; k <K; ++ K) {f [J] [I] = min (F [J] [I], F [k] [I ^ (1 <j)] + idis [k] [J]) ;}} int ret = inf; For (INT I = 0; I <K; ++ I) {ret = min (Ret, f [I] [mask-1] + odis [I]-MP [p [I]. x] [p [I]. y]);} return ret;} int main () {int t; // freopen ("1.in"," r ", stdin); scanf (" % d ", & T); While (t --) {scanf ("% d", & N, & M); memset (idis, 0x3f, sizeof (idis )); memset (odis, 0x3f, sizeof (odis); For (INT I = 0; I <n ;++ I) {for (Int J = 0; j <m; ++ J) {scanf ("% d", & MP [I] [J]); If (MP [I] [J] =-1) MP [I] [J] = inf ;}} scanf ("% d", & K); For (INT I = 0; I <K; ++ I) {scanf ("% d", & P [I]. x, & P [I]. y) ;}for (INT I = 0; I <K; ++ I) {spfa (P [I]. x * m + P [I]. y, I);} bspfa (); memset (F, 0xff, sizeof (f); int ret = DFS (1 <k)-1,-1 ); // int ret = solve (); // It can also be if (ret = inf) puts ("0"); else printf ("% d \ n ", RET);} return 0 ;}