No root minimum tree chart template

Source: Internet
Author: User

1#include <iostream>2 using namespacestd;3#include <cstdio>4#include <cstring>5 #defineMAXN 10056 #defineINF 0x7f7f7f7f7 typedef __int64 type;8 structNode//the right and the vertex of the edge9 {Ten     intu, v; One type W; A}EDGE[MAXN *MAXN]; - intPRE[MAXN], ID[MAXN], VIS[MAXN], N, M, POS; -Typeinch[MAXN];//min. pre[v] is the starting point of the edge theType Directed_mst (intRootintVintE) - { -Type RET =0;//total weight of minimum tree diagram -      while(true) +     { -         inti; +         //1. Find the minimum incoming edge for each node A          for(i =0; i < V; i++) at             inch[I] = INF;//Initialize to Infinity -          for(i =0; i < E; i++)//Traverse each edge -         { -             intU =edge[i].u; -             intv =edge[i].v; -             if(EDGE[I].W <inch[v] && u! = V)//description of the edge record with a lower weight in vertex v in             { -PRE[V] = u;//node U points to v to                 inch[v] = EDGE[I].W;//Minimum incoming Edge +                 if(U = = root)//this point is the actual starting point. -pos =i; the             } *         } $          for(i =0; i < V; i++)//determine if there is a minimum tree chartPanax Notoginseng         { -             if(i = =root) the                 Continue; +             if(inch[I] = =INF) A                 return-1;//In addition to the root is a little into the edge, then the root can not reach it indicates that it is independent of the point must not constitute a tree chart the         } +         //2. Find a ring -         intCNT =0;//number of record rings $memset (ID,-1,sizeof(ID)); $memset (Vis,-1,sizeof(Vis)); -         inch[Root] =0; -          for(i =0; i < V; i++)//Mark each ring the         { -RET + =inch[i];//Record weight valueWuyi             intv =i; the              while(Vis[v]! = i && id[v] = =-1&& V! =root) -             { WuVIS[V] =i; -v =Pre[v]; About             } $             if(v! = root && id[v] = =-1) -             { -                  for(intU = pre[v]; U! = V; U =Pre[u]) -Id[u] = cnt;//Mark Node U is the first ring AID[V] = cnt++; +             } the         } -         if(CNT = =0) $              Break;//No ring break the          for(i =0; i < V; i++) the             if(Id[i] = =-1) theId[i] = cnt++; the             //3. Create a new zoom point, re-mark -              for(i =0; i < E; i++) in             { the                 intU =edge[i].u; the                 intv =edge[i].v; AboutEDGE[I].U =Id[u]; theEDGE[I].V =Id[v]; the                 if(Id[u]! =Id[v]) theEDGE[I].W-=inch[v]; +             } -V =CNT; theRoot =Id[root];Bayi     } the     returnret; the } - intMain () - { the     inti; the      while(SCANF ("%d%d", &n, &m)! =EOF) the     { theType sum =0; -          for(i =0; I < m; i++) the         { thescanf"%d%d%i64d", &edge[i].u, &AMP;EDGE[I].V, &EDGE[I].W); theedge[i].u++; edge[i].v++;94Sum + =EDGE[I].W; the         } theSum + +; the          for(i = m; i < M + N; i++)//Add Super Node 0, node 0 to the rest of the Benquan the same (the edge of the title is greater than the original image of the total edge weight)98         { AboutEDGE[I].U =0; -EDGE[I].V = I-m +1;101EDGE[I].W =sum;102         }103Type ans = directed_mst (0, n +1, M +n);104         //n+1 is the sum of points, M+n is the total number of edges the         //ans represents the total weight of the smallest tree diagram rooted in Super node 0,106         //The ans minus sum, if the difference is less than sum, indicating that the out of node 0 is only 1, indicating that the original is connected graph107         //If the difference is >=sum, then the exit of node 0 is more than 1, indicating that the original is not a connected graph108         if(ans = =-1|| Ans-sum >=sum)109Puts"Impossible"); the         Else111printf"%i64d%d\n", Ans-sum, pos-m); thePuts"");113     } the     return 0; the}
View Code

No root minimum tree chart template

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.