Vijos P1234 Pocket Sky (Kruskal) (minimum spanning tree)

Source: Internet
Author: User

P1234 pocket of the Sky

The little cedar sits in the classroom, looking through the pockets of the window to look at the pockets of the sky.

There are a lot of clouds floating there, looks very beautiful, the little Cedar wants to take off such beautiful several clouds, made cotton candy.

Describe

Give you the number of clouds N, and then give you m relationship, indicating which clouds can be linked together.

Now the small cedar to put some clouds together, make K cotton candy, a cotton candy at least to use a cloud, the small cedar want to know how he even, the cost of the least.

Format input Format

Each set of test data
The first line has three numbers n,m,k (1<=n<=1000,1<=m<=10000,1<=k<=10)
The next m number is three x,y,l per line, indicating that the x cloud and the Y cloud can be linked by the cost of L. (1<=x,y<=n,0<=l<10000)
30% of Data n<=100,m<=1000

Output format

Output a row for each set of data, with only one integer representing the minimum cost.

If you can't even make a k marshmallow, output ' no Answer '.

Example 1 sample input 1[copy]
3 1 21) 2 1
Sample output 1[Copy]
1
Limit

1s per test point

Tips

Example 2:
Input:
3 1 1
1 2 1

Output:
No Answer

"Analysis" This minimal spanning tree problem is very interesting, he asked K minimum spanning tree, because there are n clouds and a cloud can form a marshmallow, so do not use the cloud structure to spend 0, then set aside K-1 cloud alone as a cotton candy, the remaining n (k-1) cloud structure into a minimum spanning tree, That is, when the number of sides joined =n-(k-1)-1, the construction is complete. At this time the smallest spanning tree is constructed, and the tree is constructed with a minimum cost of 0, so the K-Marshmallow is constructed with minimal cost;
The algorithm can be constructed by the kruscal of the optimized set.

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<climits>#include<cstring>#include<string>#include<Set>#include<map>#include<queue>#include<stack>#include<vector>#include<list>#include<functional>#defineMoD 1000000007#defineINF 0x3f3f3f3f#definePi ACOs (-1.0)using namespaceStd;typedefLong Longll;Const intn=1005;structedg{intV,u;intW;} edg[ -* -+ -];BOOLCMP (EDG g,edg h) {returng.w<H.W;}intN,m,k,cnt=1, MAXN;intParent[n];voidinit () { for(intI=0; i<n;i++) parent[i]=i;}voidBuild () {intu,v,w;  for(intI=0; i<m;i++) {scanf ("%d%d%d",&u,&v,&W); EDG[I].U=u;edg[i].v=v;edg[i].w=W; }    if(n<k) {printf ("No answer\n"); exit (0);} Sort (Edg,edg+m,cmp);}intFind (intx) {if(parent[x]! = x) Parent[x] =Find (parent[x]); returnparent[x];}//finds and returns the root node of the collection to which node x belongsvoidUnion (intXinty) {x=Find (x); Y=Find (y); if(x = = y)return; Parent[y]=x;}//merging elements from two different collectionsvoidKruskal () {intsum=0; intnum=0; intu,v;  for(intI=0; i<m;i++) {u=edg[i].u;v=edg[i].v; if(Find (u)! =Find (v)) {Sum+=EDG[I].W; Num++;        Union (U,V); }        if(num>=n-(K-1)-1) {printf ("%d\n", sum);  Break; }    }    if(num<n-(K-1)-1) printf ("No answer\n");}intMain () {scanf ("%d%d%d",&n,&m,&k);        Init ();        Build ();    Kruskal (); return 0;}
View Code

Vijos P1234 Pocket Sky (Kruskal) (minimum spanning tree)

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.