HDU 5723 abandoned country (minimum spanning tree, DFS)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5723

Abandoned country

Time limit:8000/4000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 2969 Accepted Submission (s): 725


Problem Descriptionan abandoned country hasn(n≤100000) Villages which is numbered from 1 toN. Since abandoned for a long time, the roads need to be re-built. There ism(m≤1000000) Roads to being re-built, the length of each road isWi(wi≤1000000) . Guaranteed.< Span id= "mathjax-span-37" class= "Mrow" > wi  are Different. The roads made all the villages connected directly or indirectly before destroyed. Every road would cost the same value of it length to rebuild. The king wants to use the minimum cost to make all of the villages connected with each other directly or indirectly. After the roads is re-built, the king asks a men as messenger. The king would select any of the different points as starting point or the destination with the same probability. Now the king asks-him the minimum cost and the minimum expectations length the messenger would walk.

Inputthe first line contains an integerT(t≤ten) Which indicates the number of test cases.

For each test case, the first line contains the integersn,m Indicate the number of villages and the number of roads to be re-built. NextmLines, each line has three numberi,J,wi The length of a road connecting the village I and the village J is wI.

Outputoutput the minimum cost and minimum expectations with the decimal places. They separated by a space.

Sample Input14 61 2 12 3 23 4 34 1 41 3 52 4 6

Sample OUTPUT6 3.33

Authorhit

Source2016 multi-university Training Contest 1 Test instructions: gives n points and m edges, each edge of the weights are different, to make n points connected and the edge of the sum of the minimum weight, and randomly selected 2 points as the start and end of the path length of the expectation Solving the puzzle: the minimum weight directly for the minimum generation tree is good M-bar edge weights are different, so each weight corresponds to an expectation, when the minimum spanning tree is found, the expectation must be that the smallest expectation is equal to the sum of the contribution of each edge divided by the total path. The point of each side of the connection to pass through the edge, so the contribution of this side is S (one side of the number of points) * (n/a) *w (weight); For each side of the point, you can use the DFS source code:
#include <cstdio>#include<cstring>#include<vector>#include<algorithm>using namespacestd;Const intmaxn=100005;intn,m;structnode{intx,y,w; Node (intXxintYyintww): X (xx), Y (yy), W (WW) {}};vector<node>A;structnode{inty,w; Node (intYyintww): Y (yy), W (WW) {}};vector<Node>B[MAXN];intF[MAXN];BOOLcmpConstNode &i,ConstNode &j) {    returni.w<J.W;}intFindsintx) {returnF[X] = = X?x:f[x] =finds (F[x]);}Long LongKruskal () {Long Longans=0;  for(intI=1; i<=n; i++) f[i]=i;    Sort (A.begin (), A.end (), CMP);  for(intI=0; I<a.size (); i++)    {        intx=finds (a[i].x); inty=finds (A[I].Y); if(x==y)Continue; F[X]=X; Ans+=A[I].W;        B[a[i].x].push_back (Node (A[I].Y,A[I].W));    B[a[i].y].push_back (Node (A[I].X,A[I].W)); }    returnans;}intS[MAXN];DoubleD[MAXN];voidDfsintRootintfather) {S[root]=1;  for(intI=0; I<b[root].size (); i++)    {        intson=b[root][i].y; intL=B[ROOT][I].W; if(Son==father)Continue;        DFS (Son,root); S[root]+=S[son]; D[root]+=d[son]+ ((Double) s[son]* (N-s[son])) *l; }}intMain () {intT; scanf ("%d",&t);  while(t--) {scanf ("%d%d",&n,&m);        A.clear ();  for(intI=0; i<m; i++)        {            intx,y,w; scanf ("%d%d%d",&x,&y,&W);        A.push_back (Node (x,y,w)); } memset (S,0,sizeof(s)); memset (d,0,sizeof(d));  for(intI=1; i<=n; i++) b[i].clear (); Long Longans=Kruskal (); DFS (1,0); Long LongG= (Long Long) N (n1)/2; printf ("%i64d%.2lf\n", ans,d[1]/(Double) (g); }    return 0;}

HDU 5723 abandoned country (minimum spanning tree, DFS)

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.