"Minimal Spanning tree" Bzoj1232 [Usaco2008nov] Comfort Cow cheer

Source: Internet
Author: User
Tags in degrees

Description

Farmer John became very lazy, and he did not want to continue to maintain the road between the cows for passage. The road is used to connect N (5 <= n <= 10,000) pastures, and the pasture is consecutively numbered 1. N. Every pasture is a cow's home. FJ plans to remove as many roads as possible from P (N-1 <= p <= 100,000), but also to maintain connectivity between pastures. You first have to decide which roads are the N-1 roads that need to be preserved. Article J bidirectional roads are connected to pasture S_j and E_j (1 <= s_j <= N; 1 <= e_j <= N; S_j! = E_j), and it takes L_j (0 <= l_j <= 1,000) to finish the time. No two pastures are connected by more than one route. The cows were very sad because their transportation system had been cut. You need to go to every cow's place to comfort them. Every time you reach the first ranch (even if you've been there), you have to spend time c_i (1 <= c_i <= 1,000) and talk to the cows. You spend the night at the same ranch (this is for your choice) until the cows are slow to get over God from grief. When you get up in the morning and go back to bed at night, you need to talk to the cows in your ranch. So that you can complete your conversation task. Assuming farmer John took your advice, calculate the minimum time for all cows to be comforted.

Solution

The nature of the tree Dfs is that each edge in the traversal is accessed two times (which makes each access to the Suo connection once), the number of points visited is in degrees, and the starting point is one more time.

So we consider each side of the traversing process separately.

The contribution of each side to the answer is Benquan *2+ connected to the point right.

According to the new right, a minimum spanning tree plus the minimum point weight (accessed as a starting point) can be obtained.

The nature of deep excavation operations and the contribution of our choices to answers can have unintended effects.

Code
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 Const intmaxn=1e5+5;5 6 structedge{7     intu,v,w;8     BOOL operator< (Constedge&x)9         Const{returnw<X.W;}Ten }E[MAXN]; One inta[maxn],p[maxn],n,m,ans=1e9; A  - intFindintx) {returnp[x]==x?x:p[x]=find (P[x]);} -  the intMain () { -scanf"%d%d",&n,&m); -      for(intI=1; i<=n;i++) -scanf"%d", &a[i]), ans=min (ans,a[i]); +      for(intI=1; i<=n;i++) p[i]=i; -          +      for(intI=1; i<=m;i++){ Ascanf"%d%d%d",&e[i].u,&e[i].v,&E[I].W); ate[i].w=2*e[i].w+a[e[i].u]+A[E[I].V]; -     } -      -Sort (e+1, e+m+1); -      for(intI=1; i<=m;i++){ -         intX=find (e[i].u), y=find (E[I].V); in         if(x!=y) { -ans+=E[I].W; top[x]=y; +         } -     } the      *printf"%d\n", ans); $     return 0;Panax Notoginseng}

"Minimal Spanning tree" Bzoj1232 [Usaco2008nov] Comfort Cow cheer

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.