3390: [Usaco2004 Dec]bad cowtractors Cow's Revenge

Source: Internet
Author: User

3390: [Usaco2004 dec]bad cowtractors cattle Revenge time limit:1 Sec Memory limit:128 MB
submit:69 solved:49
[Submit] [Status] [Discuss] Description, a cow, Bessie, was hired to build the internet between the N-2≤n≤1000. She has explored the 1≤m≤20000 of the M (* *) line, connecting two cattle sheds per line, and will have a bud charge of C (1≤c≤100000). The farmer, John, was so stingy that he hoped that the cost of construction would be minimal even he did not want to pay Bessie. Bessie was told that her wages would fall through and decided to retaliate. She intends to build some lines to connect all the barns together, so that John spends the most. But she couldn't make a ring, so John would find out.  Input line 1th: N,m. Line 2nd to m+1: Three integers representing two endpoints and charges for a possible line. The maximum cost of Output. If a reasonable line cannot be built, the output -1sample Input5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5Sample Output42

Connect 4 and 5,2 and 5,2 and 3,1 and 3, spend 17+8+10+7=42HINT Source

Silver

Puzzle: Minimum Spanning tree template problem (hansbug: Although the maximum spanning tree is accurate), feel the water
1/**************************************************************2Problem:33903 User:hansbug4 language:pascal5 result:accepted6Time: AboutMs7Memory:1788KB8****************************************************************/9  Ten var One I,j,k,l,m,n,x,ans:longint; AA:Array[0..100005,1..3] ofLongint; -C:Array[0..100005] ofLongint; - functionGetfat (x:longint): Longint; the          begin -               ifC[x]<>x Thenc[x]:=Getfat (c[x]); - exit (C[x]); -          End; + procedureSwapvarx,y:longint); -           varZ:longint; +           begin Az:=x;x:=y;y:=Z; at           End; - proceduresort (l,r:longint); -           varI,j,x,y:longint; -           begin -i:=l;j:=r;x:=a[(L+r)Div 2,3]; -                Repeat in                       whileA[i,3]>x DoInc (i); -                       whileA[j,3]<x DoDec (j); to                      ifI<=j Then +                         begin -Swap (A[i,1],a[j,1]); theSwap (A[i,2],a[j,2]); *Swap (A[i,3],a[j,3]); $ Inc (I);d EC (j);Panax Notoginseng                         End; -                untilI>J; the                ifI<r Thensort (i,r); +                ifL<j Thensort (l,j); A           End; the   + {$IFDEF WINDOWS}{$R bzoj3390.rc}{$ENDIF} -   $ begin $ readln (n,m); -       fori:=1  toN Doc[i]:=i; -       fori:=1  toM DoREADLN (A[i,1],a[i,2],a[i,3]); theSort1, m); -j:=1; x:=0;Wuyi       fori:=1  toN-1  Do the          begin -                whileJ<=m Do Wu                     begin -K:=getfat (A[j,1]); AboutL:=getfat (A[j,2]); $                          ifK<>l ThenBreak ; - Inc (J); -                     End; -               ifK=l ThenBreak ; AAns:=ans+a[j,3]; +c[k]:=L;inc (j); the Inc (X); -          End; $      ifx< (n1) ThenWriteln (-1)Elsewriteln (ans); the Readln; the End.

3390: [Usaco2004 Dec]bad cowtractors Cow's Revenge

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.