There are n cities. You can select any city as the starting point. Each city cannot be accessed more than 2 times,
The minimum value required to access all N cities.
Idea: because each city can be accessed for up to two times, the access status is represented in a three-digit format.
For details, see code comments !!!!
# Include <cstdio> # include <stdlib. h> # include <string. h> # include <string> # include <map> # include <cmath> # include <iostream> # include <queue> # include <stack> # include <algorithm> # include <set> using namespace STD; # define INF 1e8 # define INF 0x3f3f3f # define EPS 1e-8 # define ll long # define n 100001 # define mol limit int DP [60000] [15]; // DP [I] [J] indicates that the min value of J is accessed in status I. Int G [15] [15]; int n, m; int st [2, 60000] [11]; // d P [I] [J] the number of times that int bit [12] can be accessed in the J City of the I state; // all initial possible states int main () {bit [0] = 0; bit [1] = 1; for (INT I = 2; I <= 11; I ++) bit [I] = 3 * bit [I-1]; for (INT I = 0; I <59050; I ++) {int T = I; for (Int J = 1; j <= 10 & T; j ++) {st [I] [J] = T % 3; T/= 3 ;}} while (~ Scanf ("% d", & N, & M) {int U, V, C; memset (DP, 0x3f, sizeof (DP); memset (G, 0x3f, sizeof (g); For (INT I = 0; I <= N; I ++) DP [bit [I] [I] = 0; // The initial start point is 0 while (M --) {scanf ("% d", & U, & V, & C ); if (C <G [u] [v]) {G [u] [v] = C; G [v] [u] = C ;}} int ans = inf; for (INT I = 0; I <bit [n + 1]; I ++) {int flag = 1; for (Int J = 1; j <= N; j ++) {If (! St [I] [J]) // I status does not pass through J City {flag = 0; continue;} if (I = J) continue; // The I status only goes through J City (J City as the starting point) for (int K = 1; k <= N; k ++) {int L = I-bit [J]; // The status of the I state before it passes through the J City (the status before it reaches the J City) if (ST [I] [k] = 0) continue; // If the I status does not pass through K cities, the value of DP [I] [J] = min (DP [I] [J], DP [l] [k] + G [k] [J]); // L status changes to I status after passing through J City} If (FLAG) // The I status passes through all N cities for (Int J = 1; j <= N; j ++) ans = min (ANS, DP [I] [J]);} If (ANS! = Inf) printf ("% d \ n", ANS); else printf ("-1 \ n") ;}return 0 ;}