Blue Bridge Cup algo--6 Comfort Cow (minimum spanning tree)

Source: Internet
Author: User

Problem description

Farmer John became very lazy, and he did not want to continue to maintain the road between the cows for passage. Roads are used to connect n pastures, and pastures are numbered 1 to n consecutively. Every pasture is a cow's home. FJ plans to remove as many roads as possible on p roads, but also to maintain connectivity between pastures. You first have to decide which roads are the N-1 roads that need to be preserved. Section J bidirectional roads are connected to the ranch Sj and Ej (1 <= SJ <= N; 1 <= Ej <= N; Sj! = Ej), and it takes LJ's time to finish it. 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 get to the I ranch (even if you've been there), you have to spend time with CI to 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 sleeping 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.

Input format

The 1th line consists of two integers n and p.

Next n rows, each line contains an integer CI.

Next P line, each line contains three integers SJ, EJ and LJ.

Output format output An integer, the total time required (contains two talk times with cows in your ranch). Sample Input 5 7
10
10
20
6
30
1 2 5
2 3 5
2 4 12
3 4 17
2 5 15
3 5 6 Sample output 176 data size and conventions

5 <= N <= 10000,n-1 <= P <= 100000,0 <= Lj <= 1000,1 <= Ci <= 1,000.

The question is really ... You have a big brain hole.

In fact, in the beginning, I really did not read, and later read, but there seems to be no idea, just think of the minimum spanning tree, but Benquan is how much or not clear, and then on the Internet to learn from the great God code, just understand how.

1#include <iostream>2#include <cstring>3#include <algorithm>4 using namespacestd;5 structnode{6     ints,e,v;7}edge[100010];8 intm=0, n,val[10010],p;9 voidAddintSintEintv) {Tenedge[++m].s=s; OneEdge[m].e=e; Aedge[m].v=v; - } - intfather[10010]; the intFINDF (inti) {//and check set, intends to use Kruskal to calculate the minimum spanning tree; -     if(father[i]==i)returni; -     Else returnfather[i]=findf (Father[i]); - } + BOOLCMP (node A,node b) { -     returna.v<B.V; + } A intsum=0; at voidKruskal () { -Sort (edge+1, edge+m+1, CMP); -     intk=1; -      for(intI=1; i<=n;i++) father[i]=i; -      for(intI=1; i<=m;i++){ -         intA=findf (EDGE[I].S); in         intb=findf (EDGE[I].E); -         if(a==b)Continue; tofather[b]=A; +sum+=edge[i].v; -         if(n==k++) Break; the     } * } $ intMain () {Panax Notoginseng     ints,e,v; -Cin>>n>>P; the     intk=9999999; +      for(intI=1; i<=n;i++){ ACin>>Val[i]; the         if(val[i]<k) k=Val[i]; +     }  -      for(intI=1; i<=p;i++){ $Cin>>s>>e>>v; $v=v*2+val[s]+Val[e]; - Add (s,e,v);  -     } the Kruskal (); -cout<<sum+k<<Endl;Wuyi     return 0; the}

Blue Bridge Cup algo--6 Comfort Cow (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.