"Usaco" 2.Cheering up the cows minimum spanning tree,

Source: Internet
Author: User

Tagged with: Usaco gold cheering up the cows minimum spanning tree

Test instructions: It can be understood to give you a diagram, let you build a tree, and then start from a root, each one side will consume the right side of the Edge + edge. Finally add a root to the point, and ask at least how much it cost.

The puzzle: Change the edge and make the minimum spanning tree.

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm> #define N 101000#define inf 0x3f3f3f3fusing namespace std;struct ksd{int U,v,len;bool operator < (const KS D &a) Const{return Len<a.len;}} Road[n];int p[n];int n,m,ans=inf;int f[n];int Find (int x) {return x==f[x]?x:f[x]=find (f[x]);} int main () {int i,j,k;int a,b,c;scanf ("%d%d", &n,&m), for (i=1;i<=n;i++) {scanf ("%d", &p[i]); Ans=min (ans , P[i]); f[i]=i;} for (i=1;i<=m;i++) {scanf ("%d%d%d", &a,&b,&c); road[i].u=a,road[i].v=b,road[i].len=p[a]+p[b]+c*2;} Sort (road+1,road+m+1), for (i=1;i<=m;i++) {int Fa=find (ROAD[I].U), Fb=find (ROAD[I].V), if (FA!=FB) {ans+=road[i]. LEN;F[FB]=FA;}} printf ("%d\n", ans); return 0;}


Copy to Google TranslateTranslation Results

"Usaco" 2.Cheering up the 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.