At the beginning, it was a brute-force DFS + pruning operation. Later I referred to others' ideas:
First, find the shortest short circuit between each vertex (so that the pre-processed search can be used to determine whether to return a duplicate), and the key is the amputation: 1. Optimum pruning (as best as possible: the Current Status + the expected minimum time> min returns !), 2: feasibility of amputation: if the current status + predicted status is no longer feasible, return. (Consider whether it is continue or return !). And the location! The effect on the exit is generally better (not inside the next loop) (reason: if the status is followed by the status, the front will be DFS to a very deep level, so put it at the top, it is generally reasonable to return if it cannot be determined together .)
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int N; int da [35]; int d [35]; int A [35] [35]; int maxd = 0; const int INF = 0x3f3f3f; int Minn = inf; int bit [31]; void DFS (int x, int lev, int sum, int Allstate) {If (sum + d [x] * (N-lev)> = Minn | D [x]> maxd) {return ;} if (Allstate = (bit [N]-1) {Minn = sum; return;} For (INT I = 2; I <= N; I ++) {If (Allstate & bit [I-1]) = 0 & D [x] + A [x] [I]> da [I]) return;} For (int I = 2; I <= N; I ++) {If (Allstate & bit [I-1]) = 0) {int F = d [I]; d [I] = d [x] + A [x] [I]; DFS (I, column + 1, sum + d [I], allstate | bit [I-1]); D [I] = f ;}return ;}void Init () {int TD = 0; DA [1] = 0x3f3f3f-1; for (INT I = 1; I <= N; I ++) d [I] = inf; d [1] = 0; maxd = 0; Minn = inf; for (INT I = 1; I <= N; I ++) // makes a mistake again! Enumerate excessive points first! For (Int J = 1; j <= N; j ++) for (int K = 1; k <= N; k ++) if (A [J] [I] + A [I] [k] <A [J] [k]) A [J] [k] = A [J] [I] + A [I] [k];} int main () {for (INT I = 0; I <31; I ++) bit [I] = 1 <I; while (scanf ("% d", & N )! = EOF) {for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) scanf ("% d", & A [I] [J]); Init (); For (INT I = 2; I <= N; I ++) {scanf ("% d", & Da [I]); If (DA [I]> maxd) maxd = da [I];} DFS ); if (Minn! = Inf) printf ("% d \ n", Minn); else printf ("-1 \ n") ;}return 0 ;}