Bzoj 3590 [Dual connected component] [dp]__ double connected component

Source: Internet
Author: User
Description Description

Ask for an edge double Unicom component that contains all the nodes, and is the edge right and the smallest. n≤12 n \le Solution Solution

Notice that any one edge double connected component is composed of a chain and a sub-connected component, and the number of nodes in the problem is n≤12 n\le 12. Consider the pressure DP.

An FS f_s represents the minimum value of the weight of a dual-unicom component that has a set of points as s S.

The HS,U,0/1 h_{s, U, 0/1} represents the minimum and minor values of the weights of the Edge (U,V) (V∈s) (u,v) (V\in S).

The Gs,u,v g_{s, U, v} represents the minimum value of the weight of a chain of s s that is u,v u,v to the endpoint.

You can update F F through the H,g h,g array. Time complexity O (2nn2) O (2^nn^2)

#include <cstring> #include <iostream> using namespace std;
  inline char get (void) {static char buf[100000], *s = buf, *t = buf;
    if (s = = t) {T = (s = buf) + fread (buf, 1, 100000, stdin);
  if (S = = T) return EOF;
return *s++;
  } inline void read (int &x) {static char c; x = 0;
  for (c = Get (), C < ' 0 ' | | | c > ' 9 '; c = Get ());
for (; c >= ' 0 ' && C <= ' 9 '; c = Get ()) x = x * + C-' 0 ';
const int INF = 0X1F1F1F1F;
const int M = 15;
const int N = (1 << 12) + 10;
int test, N, m, x, Y, Z, Sn, MN, SE, gcnt;
int id[m], head[m];
int f[n], h[n][m][2], g[n][m][m];
  struct Edge {int to, next, key;
Edge (int t = 0, int n = 0, int k = 0): to (T), Next (n), key (k) {}};

Edge G[m * M * 2];  inline int Min (int a, int b) {return a < b a:b;} inline void Addedge (int from, int to, int key) {G[++GCNT] = Edge (To, Head[from], key);
  Head[from] = gcnt; G[++GCNT] = Edge (from, Head[to], key);
Head[to] = gcnt; int main (void) {
  Freopen ("1.in", "R", stdin);
  Freopen ("1.out", "w", stdout);
  Read (test);
  for (int i = 0; i < i++) Id[i + 1] = 1 << i;
    while (test--) {memset (f, 0x1f, sizeof F);
    Memset (g, 0x1f, sizeof g); gcnt = 0;
    memset (head, 0, sizeof head); Read (n); Read (m);
    Sn = 1 << n;
      for (int i = 1; I <= m; i++) {read (x); Read (y); read (z);
    Addedge (x, y, z);
    for (int i = 1; I <= n; i++) f[id[i]] = g[id[i]][i][i] = 0; for (int S = 0; S < Sn;
        s++) {for (int i = 1; I <= n; i++) {mn = inf; se = INF;
              for (int j = head[i]; j; j = g[j].next) {if (Id[g[j].to] & S) {if (G[j].key <= mn) { Se = MN;
            MN = G[j].key;
            else if (G[j].key < se) {se = G[j].key; }} H[s][i][0] = MN;
      H[s][i][1] = se; for (int S = 1; S < Sn; s++) for (int i = 1; I <= n; i++) if (id[I] & s) for (int j = 1; J <= N; j +) if (Id[j] & S) {for (int p = head[i]; P p = g[p].next) if (!) ( Id[g[p].to] & S) g[s | id[g[p].to]][g[p].to][j] = Min (G[s | id[g[p].to]][g[p].to][j), G[S][I][J] +
              G[p].key); for (int p = head[j]; p; p = g[p].next) if (!) ( Id[g[p].to] & S) g[s | id[g[p].to]][i][g[p].to] = Min (G[s | id[g[p].to]][i][g[p].to), G[S][I][J] +
            G[p].key); for (int S = 1; S < Sn;
      s++) {int R = (Sn-1) ^ S; for (int T = R; T T = (T-1) & R) {for (int i = 1; I <= N. i++) if (Id[i] & T) for (int j = 1; j <= N; J + +) if (Id[j] & T) if (i = = j) F[s | T] = Min (F[s |
                T], F[s] + h[s][i][0] + h[s][j][1] + g[t][i][j]); else F[s | T] = Min (F[s | T], F[s] + h[s][i][0] + h[s][j][0] + g[t][i][j]);
    } if (F[sn-1] >= INF) puts ("impossible");
  else printf ("%d\n", f[sn-1]);
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.