Kruskal ...
1#include <iostream>2#include <cstdio>3#include <algorithm>4 #defineN 100105 #defineM 1000106 #defineINF 0x7fffffff7 using namespacestd;8 structnode9 {Ten intu,v,w; One }e[m]; A intP[n],fa[n]; - intans,t,n,m; - BOOLCMP (node A,node b) {returna.w<B.W;} the intFind (intx) - { - returnfa[x]==x?x:fa[x]=Find (fa[x]); - } + intMain () - { +scanf"%d%d",&n,&m); Aans=inf; at for(intI=1; i<=n;i++) - { -scanf"%d",&p[i]); -ans=min (ans,p[i]); -fa[i]=i; - } in for(intI=1; i<=m;i++) - { toscanf"%d%d%d",&e[i].u,&e[i].v,&E[I].W); +e[i].w=p[e[i].u]+p[e[i].v]+e[i].w*2; - } theSort (e+1, e+m+1, CMP); * for(intI=1; i<=m;i++) $ {Panax Notoginseng intF1=find (e[i].u), f2=Find (E[I].V); - if(f1!=F2) the { +fa[f1]=F2; At++; theans+=E[I].W; + } - if(t==n-1) Break; $ } $printf"%d\n", ans); - return 0; -}
View Code 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. For your first 10 commits, your program runs on some of the formal test data and returns the results of the run.
Input
* Line 1th: Two integers separated by spaces N and P * 2..n+1: Line i+1 contains an integer: C_i * n+2..n+p+1 line: The n+j+1 line contains three integers separated by spaces: S_j, E_j and L_j
Output
Line 1th: An integer, the total time required (contains two talk times with cows in your ranch).
Sample Input5 7
10
10
20
6
30
1 2 5
2 3 5
2 4 12
3 4 17
2 5 15
3 5 6
4 5 12
Sample Output176
HINT
Source
"BZOJ1232" [Usaco2008nov] comfort Cow cheer minimum spanning tree