HDU 1863 unblocked Project (minimum spanning tree, prim)

Source: Internet
Author: User

Test instructions: Given the edges and points of the graph, the cost of minimum spanning tree is required, note: Some points are unreachable, that is, there may be multiple connected graphs. For example, 4 points, 2 sides: 1-2, 3-4.

Idea: If you can't connect all the dots, then output '? '. Previously thought that each point as long as there is an edge attached to be able to generate a tree, in fact, it can be each point although there is a side can reach, but given is actually two diagram, such as the above example. Others follow regular prim.

1#include <bits/stdc++.h>2 using namespacestd;3 Const intn= the;4 Const intMod=0x7f7f7f7f;5 intV[n][n];//Rights6 intVis[n];7 intLow[n];//to the minimum right of each point8 9 Ten intPrimintN) One { A     intpos=vis[1]=1;//starting from point 1 -      for(intI=2; i<=n; i++) low[i]=v[1][i];//at present to each point of the smallest right -     intans=0; the      for(intI=1; i<n; i++)//Take the other n-1. -     { -         intbig=MoD; -          for(intj=2; j<=n; J + +)//The least right side of the search +         { -             if(!vis[j] && low[j]<big)//not browsed, at present can reach, the right small +             { Apos=J; atbig=Low[j]; -             } -         } -         if(Big==mod)return 0;//cannot be reached.  -ans+=Big; -vis[pos]++;//have viewed in          for(intj=2; j<=n; J + +)//update weights to each point -             if(!vis[j] && v[pos][j]<mod && low[j]>v[pos][j]) low[j]=v[pos][j];//not browsed, there is a way up, shorter to     } +     returnans; - } the  *  $ Panax Notoginseng  - intMain () the { +Freopen ("Input.txt","R", stdin); A     intN, M, a, B, t; the      while(SCANF ("%d%d", &n, &m), N) +     { -  $memset (CNT,0,sizeof(CNT)); $memset (Vis,0,sizeof(Vis)); -memset (V,0x7f,sizeof(v));//set to not reach -  the          for(intI=0; i<n; i++) -         {Wuyiscanf"%d%d%d",&a,&b,&t); thev[a][b]=v[b][a]=T; -         } Wu         intans=Prim (m); -         if(ANS) printf ("%d\n", ans); About         Elseprintf"? \ n"); $     } -     return 0; -}
AC Code

HDU 1863 unblocked Project (minimum spanning tree, prim)

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.