Codeforces traveling graph pressure DP + Euler Loop

Source: Internet
Author: User

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 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.