HDU 4126 Genghis Khan the Conqueror mst+ tree DP

Source: Internet
Author: User

Test instructions

A graph of the N-point M-Edge is given.

The following m lines give the edges and Benquan

The following q queries.

Q line Each row gives an edge (must be one of the M-bar edges)

Represents the change of edge rights.

(The data guarantees that the modified Benquan is larger than the original edge)

Q: What is the weighted value of the smallest spanning tree after the change?

Each inquiry is independent of each other (that is, each inquiry is a change to the original)

Guaranteed no heavy edges.


The average value of the minimum spanning tree weight after all changes.

Ideas:

First run a minimum spanning tree.

The weight of this MST is obtained by the INT MST;

For each of the inquiries (U.v,dis);

if (U,V) is not an edge on the MST, then the weight at this point is the MST

Otherwise we break the edge of the tree (U,V), and then look for the edge between the U-point set and the V-point set for the least-weighted edge cost[u][v];

So the current weight is mst-g[u][v] + min (cost[u][v], dis);


The rest is how the cost is calculated;

MST will find a tree without roots.

When we turn a tree without a root into a u root, it is virtually constant for the V subtree.

The rest is simple DP.


#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <cstring> #include < string> #include <iostream> #include <queue> #include <algorithm> #include <cmath>template <class t>inline BOOL Rd (T &ret) {char c; int sgn;if (C=getchar (), c==eof) return 0;while (c!= '-' && (c< ') 0 ' | | C> ' 9 ')) C=getchar (), sgn= (c== '-'), -1:1;ret= (c== '-')? 0: (c ' 0 '); while (C=getchar (), c>= ' 0 ' &&c<= ' 9 ') ret=ret*10+ (c ' 0 '); Ret*=sgn;return 1;}        Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-');    x =-X;    } if (X>9) pt (X/10); Putchar (x%10+ ' 0 ');} typedef long Long ll;using namespace Std;const ll inf = 100000000;const int N = 3005;ll G[n][n], D[n], MST, Cost[n][n];boo    L Vis[n], Choose[n][n];int N, m;vector<int> g[n];ll dfs (int u, int fa, int src) {ll siz = inf;        for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];        if (v = = FA) continue; ll tmp = DFS (V, u, SRC);        siz = min (siz, TMP);    COST[U][V] = cost[v][u] = min (cost[u][v], TMP);    } if (fa! = src) siz = min (siz, g[u][src]); return siz;} int pre[n];void MST () {for (int i = 0, i < n; i++) for (int j = 0; J < N; J + +) Cost[i][j] = g[i][    J] = inf, choose[i][j] = 0;        while (m--) {int u, v; ll dis; rd (u); Rd (v); Rd (DIS);    G[U][V] = g[v][u] = min (g[u][v], dis);        } for (int i = 0; i < n; i++) {d[i] = inf;        G[i].clear ();        Vis[i] = 0;    Pre[i] =-1;    } D[0] = 0;    MST = 0;        for (int i = 0; i < n; i++) {int pos =-1;        for (int j = 0; J < N; j + +) if (!vis[j] && (pos = = 1 | | d[pos] > D[J]) pos = j;            if (pre[pos]!=-1) {g[pos].push_back (Pre[pos]);            G[pre[pos]].push_back (POS);        Choose[pos][pre[pos]] = choose[pre[pos]][pos] = 1; } for (int j = 0; J < N; j + +) if (D[j] &Gt                G[j][pos]) {d[j] = G[j][pos];            PRE[J] = pos;        } Vis[pos] = 1;    MST + = D[pos];        }}int Main () {int q, u, v; ll Dis;while (Cin>>n>>m, n+m) {MST ();        for (int i = 0; i < n; i++) Dfs (I,-1, i);        RD (Q);        ll ans = 0;            for (int i = 1; I <= Q; i++) {rd (U); Rd (v); Rd (DIS);            if (choose[u][v] = = false) ans + = MST;        else ans + = mst-g[u][v] + min (cost[u][v], dis); } printf ("%.4f\n", (double) ans/(double) q);} return 0;}


HDU 4126 Genghis Khan the Conqueror mst+ tree DP

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.