HDU-2121 Ice_cream ' s World II (Zhu Liu algorithm + virtual root)

Source: Internet
Author: User

The main idea: to give you n points, M Bar has an edge, ask which point is the root node, you can minimize the total weight of the spanning tree, the total value of output and root.
If the structure is not the smallest spanning tree, the additional output

Problem-solving ideas: The problem is very ingenious, violent enumeration words, definitely tle, so, this problem requires a bit of skill
You can set a virtual root, the virtual root connection each point, the weight of all sides of the total weight of +1. Then, take the virtual root as the root, run Zhu Liu algorithm.
After running the results, to determine if the total weight of the minimum spanning tree is larger than 2 * (total weight of all sides +1), indicating that the virtual root is connected to at least two points, so that the minimum spanning tree is a false minimum spanning tree, because at least two points in the degree of 0

How to find the root when you get the result, you can use the edge to judge. We first construct the M-bar, followed by the construction of the N-Edge (the virtual root to each point), if a point of the minimum weight of the value of a virtual root to that point of the weight, it proves that the point is the minimum root. Because only the subscript is greater than or equal to M Bencai is the edge of the virtual root to the point, so just record the subscript, the final output with subscript-M to get the dot

#include <cstdio>#include <cstring>#define M 20010#Define N 1010structedge{int  from, to, cost;} E[M];intN, m, tot;intSum;voidAddedge (intUintVintc) {E[tot]. from= u;    E[tot].to = v; E[tot++].cost = C;}voidInit () {tot =0; Sum =0;intU, V, c; for(inti =0; I < m; i++) {scanf ("%d%d%d", &u, &v, &c);        Addedge (U, V, c);    Sum + = C; } sum++; for(inti =0; I < n;    i++) {Addedge (n, I, Sum); }}#define INF 0x3f3f3f3fintMinroot;intPre[n], Vis[n], Id[n],inch[N];intDirected_mst (intRoot) {intAns =0, u, V, TMP; while(1) {memset (PRE,-1,sizeof(pre)); for(inti =0; I < n; i++)inch[I] = INF; for(inti =0; i < tot; i++) {u = e[i]. from; v = e[i].to;if(U! = V && e[i].cost <inch[V]) {inch[v] = e[i].cost; PRE[V] = u;if(U = = root) Minroot = i; }        } for(inti =0; I < n; i++) {if(i = = root)Continue;if(inch[i] = = INF)return-1; }intCNT =0; memset (Vis,-1,sizeof(VIS)); memset (ID,-1,sizeof(ID));inch[Root] =0; for(inti =0; I < n; i++) {ans + =inch[i]; TMP = i; while(tmp! = root && id[tmp] = =-1&& vis[tmp]! = i) {vis[tmp] = i;            TMP = pre[tmp]; }if(tmp! = root && id[tmp] = =-1) {u = pre[tmp]; while(U! = tmp)                    {Id[u] = cnt;                U = pre[u];            } id[tmp] = cnt++; }        }if(CNT = =0) Break; for(inti =0; I < n; i++)if(Id[i] = =-1) Id[i] = cnt++; for(inti =0; i < tot;            i++) {tmp = e[i].to; E[i]. from= Id[e[i]. from]; E[i].to = id[e[i].to];if(E[i]. from! = e[i].to) E[i].cost-=inch[TMP];        } n = cnt;    root = Id[root]; }returnAns;}voidSolve () {n++;intAns = directed_mst (n-1);if(ans = =-1|| Ans >=2* Sum) printf ("impossible\n");Elseprintf"%d%d\n", Ans-sum, minroot-m); printf"\ n");}intMain () { while(SCANF ("%d%d", &n, &m)! = EOF) {init ();    Solve (); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU-2121 Ice_cream ' s World II (Zhu Liu algorithm + virtual root)

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.