Optimal irrigation for CCF simulation problems

Source: Internet
Author: User

Optimal irrigation

Time limit: 1.0s
Memory Limit: 256.0MB
Problem DescriptionRay Ray contracted a lot of wheat fields, in order to irrigate the wheat fields, Ray Ray dug a very deep well in the first wheat field, all the wheat fields from this well to water diversion irrigation.
In order to irrigate, Ray Ray need to establish some canals to connect wells and wheat fields, and Ray Ray can also use some of the wheat fields as "transit stations", using canals to connect different fields of wheat, so as long as a wheat field can be irrigated, the wheat fields connected to it can also be irrigated.
Now Ray Ray know which fields of wheat can be built between canals and the cost of building each channel (note that not all wheat fields can be established between canals). How much is it necessary to irrigate all the wheat fields to build canals? Input FormatThe first line of input contains two positive integers n, m, respectively, representing the number of slices in the wheat field and the amount of canals that Ray Ray can establish. Wheat fields use 1, 2, 3, ... Label in turn.
Next m line, each line contains three integers ai, bi, CI, which indicates that a channel can be established between the AI wheat field and the second Bi wheat field, the cost of which is CI. output FormatThe output line contains an integer that represents the minimum cost required to irrigate all wheat fields. Sample Input4 4
1 2 1
2 3 4
2 4 2
3 4 3 Sample Output6 Sample DescriptionThe following three canals were established: wheat field 1 with wheat field 2, wheat field 2 with wheat field 4, wheat field 4 with wheat Field 3. Evaluation use case size and the first 20% evaluation use cases meet: n≤5.
The first 40% evaluation cases meet: N≤20.
The first 60% evaluation cases meet: n≤100.
All evaluation cases meet: 1≤n≤1000,1≤m≤100,000,1≤ci≤10,000. Problem solving: minimum spanning tree
1#include <bits/stdc++.h>2 #defineLL Long Long3 #defineINF 0x3f3f3f3f3f3f3f4 #definePII pair<int,int>5 using namespacestd;6 Const intMAXN = -;7 structarc{8     intTo,cost,next;9Arcintx =0,inty =0,intz =-1){Tento =x; OneCost =y; ANext =Z; -     } -}e[500000]; the intTOT,N,M,HEAD[MAXN]; - LL D[MAXN]; - BOOLDONE[MAXN]; - voidAddintUintVintCost ) { +E[tot] =arc (V,cost,head[u]); -Head[u] = tot++; +E[tot] =arc (U,cost,head[v]); AHEAD[V] = tot++; at } -priority_queue< pii,vector< PII >,greater< PII > >Q; - LL Prim () { -      while(!q.empty ()) Q.pop (); -      for(inti =1; I <= N; ++i) { -Done[i] =false; inD[i] =INF; -     } toQ.push (Make_pair (d[1] =0,1)); +LL ans =0; -      while(!Q.empty ()) { the         intU =Q.top (). Second; * Q.pop (); $         if(Done[u])Continue;Panax NotoginsengDone[u] =true; -Ans + =D[u]; the          for(inti = Head[u]; ~i; i =E[i].next) { +             if(!done[e[i].to] && E[i].cost <D[e[i].to]) { AD[e[i].to] =E[i].cost; the Q.push (Make_pair (d[e[i].to],e[i].to)); +             } -         } $     } $     returnans; - } - intMain () { the     intu,v,w; -      while(~SCANF ("%d%d",&n,&m)) {Wuyimemset (head,-1,sizeof(head)); the          for(inti = tot =0; I < m; ++i) { -scanf" %d%d%d",&u,&v,&W); Wu Add (u,v,w); -         } Aboutprintf"%i64d\n", Prim ()); $     } -     return 0; -}
View Code

Optimal irrigation for CCF simulation problems

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.