Algorithm training to comfort cows 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 pasture sJ and EJ(1 <= sJ <= N; 1 <= EJ <= N; SJ ! = EJ), and it takes the time of LJ 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 first ranch (even if you've been there), you have to spend CI time talking to 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 row 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.

There's a problem with the data look in the code
1#include <bits/stdc++.h>2#include <iostream>3#include <cstring>4#include <cstdio>5#include <algorithm>6#include <vector>7 #definell __int648 #definePI ACOs (-1.0)9 #defineMoD 1000000007Ten using namespacestd; One intn,p; A structnode - { -     intW; the     intL,r; -}n[1000006]; - inteax100005]; - ints[100005]; + intFindintroot) - { +     if(fa[root]!=root) A         returnfa[root]=find (Fa[root]); at     Else -         returnRoot; - } - voidUnio (intAintb) - { -     intAa=find (a); in     intbb=find (b); -     if(aa!=BB) to     { +fa[aa]=BB; -     } the } * BOOLcmpstructNode AA,structnode BB) $ {Panax Notoginseng     returnaa.w<BB.W; - } the intMain () + { Ascanf"%d%d",&n,&p); the     intminx=10000000; +      for(intI=1; i<=n;i++) -     { $scanf"%d",&s[i]); $minx=min (minx,s[i]); -fa[i]=i; -     } the      for(intI=1; i<=p;i++) -     {Wuyiscanf" %d%d%d",&n[i].l,&n[i].r,&N[I].W); then[i].w=2*n[i].w+s[n[i].l]+S[N[I].R]; -     } WuSort (n+1, n+1+p,cmp); -     intans=0; About      for(intI=1; i<=p;i++) $     { -         if(Find (N[I].L)! =find (N[I].R)) -         { -ans=ans+N[I].W; A Unio (N[I].L,N[I].R); +         } the     } -printf"%d\n", ans+minx); $     return 0; the } the /* the 5 6 the Ten - Ten in  - the 6 the  - About 1 2 5 the 2 3 5 the 2 4 the 3 4 + 2 5 - 3 5 6 the */

Algorithm training to comfort cows 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.