Question link: Click the open link
Question:
Undirected graph with M edge given n points
Calculate the minimum distance from 1 point to at least 1 point after each edge.
Obviously, it is to find a repeatable Euler loop.
Ideas:
First, the Euler's loop is concluded that the degrees of all vertices are even.
Because all sides pass through the question at least once, you can convert the question into the minimum number of sides to make the graph meet the above conclusion.
The purpose of Edge Addition is to convert odd degrees to even degrees. First, Floyd to get the minimum cost of Edge Addition between any points.
DP [I] indicates the minimum cost of state I with an even number.
Press DP to select two odd degrees from all unselected vertices in the I state.
# Include <cstdio> # include <cstring> # include <algorithm> # include <vector> # include <iostream> # include <map> # include <set> # include <math. h> using namespace STD; # define ll int # define INF 1000000007 # define n 16int dis [N] [N], n, m; void Floyd () {for (ll k = 0; k <n; k ++) for (ll I = 0; I <n; I ++) for (LL J = 0; j <n; j ++) dis [I] [J] = min (DIS [I] [J], dis [I] [k] + dis [k] [J]); For (ll I = 0; I <n; I ++) dis [I] [I] = 0;} I NT du [N], ANS, DP [1 <n]; // DP [I] indicates the minimum cost of ll work () {ll I, j, k; for (I = 1; I <n; I ++) // If the vertex I has edge connections, but the vertex 0 does not reach this vertex, the edge connected to this vertex cannot go, that is, the graph is not connected to If (Du [I] & dis [0] [I] = inf) Return-1; ll all = (1 <n)-1; for (I = 0; I <= all; I ++) DP [I] = inf; DP [0] = 0; For (k = 0; k <= all; k ++) {for (I = 0; I <n; I ++) // find an odd degree point IIF (Du [I] & 1) & (K & (1 <I) break; if (I = N) DP [k] = 0; for (I = 0; I <N; I ++) // enumerative odd degree point I if (Du [I] & 1 )&&! (K & (1 <I) for (j = I + 1; j <n; j ++) // enumerate odd degree points J If (Du [J] & 1 )&&! (K & (1 <j) & dis [I] [J] <inf) DP [k | (1 <I) | (1 <j)] = min (DP [k | (1 <I) | (1 <j)], DP [k] + dis [I] [J]);} if (DP [all]> = inf) Return-1; return ans + dp [all];} int main () {ll I, j, U, V, W; while (CIN> N> m) {for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) dis [I] [J] = inf; memset (DU, 0, sizeof du); ans = 0; while (M --) {CIN> U> V> W; u --; V --; dis [u] [v] = dis [v] [u] = min (DIS [u] [v], W); Du [u] ++; du [v] ++; ans + = W;} Floyd (); cout <work () <Endl;} return 0 ;}