Algorithm Training Comfort Cow minimum spanning tree + construction

Source: Internet
Author: User

Http://lx.lanqiao.cn/problem.page?gpid=T16

Test instructions: N points, P-bar, n,p<=1e5. Each point has a right value of c[i], to choose a point to go through all points and return the minimum cost required?

It is easy to find that each side has experienced two times, when the Edge (U,v) reaches V at the first time ans+=c[v],v returns u Ans+=c[u] the construction edge is: Comfort two points (u,v) the cost of 2*w+c[u]+c[v], to find the MST can

#include <bits/stdc++.h>using namespace std;const int inf=1e9;const int n=2e5+20;struct node{int u,v,w;} E[n];int n,p,c[n],d[n],vis[n],ans;int Fa[n];bool CMP (node A,node b) {return A.W<B.W;} int find (int x) {if (fa[x]!=x) Fa[x]=find (fa[x]); return fa[x];} void Union (int x,int y) {int fx=find (x), Fy=find (y); fa[fx]=fy;} A void Kruskal () {for (int i=1;i<=n;i++) Fa[i]=i;sort (E,E+P,CMP) is synthesized for each of the two connected components at a minimum cost; for (int i=0;i<p;i++) {int u=e [I].u,v=e[i].v,w=e[i].w;if (Find (U)!=find (v)) {Union (u,v); ans+=w;}}} int main () {while (cin>>n>>p) {int mn=inf;ans=0;int u,v,w;for (int i=1;i<=n;i++) {cin>>c[i];mn=min (c[i],mn);//select Slepp Place, beginning}for (int i=0;i<p;i++) {Cin>>u>>v>>w;e[i].u=u,e[i].v=v;e[i].w=2*w+c [u]+c[v];//Comfort U,v Two cows required time}kruskal (); memset (vis,0,sizeof (VIS));cout<<ans+mn<<endl; }return 0;}

  

Algorithm Training Comfort Cow minimum spanning tree + construction

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.