HDU 1233 or unblocked project (minimum spanning tree, prim)

Source: Internet
Author: User

Test instructions: Chinese title

Idea: Prim realization, because there are N (n-1)/2 edge, is already saturated edge, prim more appropriate.

(1) Set point 1 to browse, point 1 can reach each other point, so use the Low[i] array to record the current reach I point minimum length.

(2) in the low array to find the point that has not been browsed, and the nearest, set to browse, remember that the store is POS.

(3) Update the low array with all points reachable from the Pos point, so that the distance from the browsed point to point I is shortest to low[i].

(4) Return to 2 to continue execution until all the points have been browsed.

In the 2nd step, you can record the minimum path length by the way.

1#include <bits/stdc++.h>2 using namespacestd;3 Const intn= the;4 intV[n][n];//Rights5 intVis[n];6 intLow[n];//to the minimum right of each point7 8 9 intPrimintN//Primm AlgorithmTen { Onememset (Vis,0,sizeof(Vis)); A     intpos=vis[1]=1;//starting from point 1 -      for(intI=1; i<=n; i++)if(v[1][i]>0) 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=Long_max; -          for(intj=1; j<=n; J + +)//The least right edge, and the corresponding point +         { -             if(!vis[j] && low[j]<big) +             { Apos=J; atbig=Low[j]; -             } -         } -Ans+=big;//Long Statistical path -vis[pos]=1; -          for(intj=1; j<=n; J + +)//update weights to each point in             if(!vis[j]) low[j]=min (low[j],v[pos][j]); -     } to     returnans; + } -  the  *  $ Panax Notoginseng intMain () - { theFreopen ("Input.txt","R", stdin); +     intN, a, B, t; A      while(SCANF ("%d", &n),n>0) the     { +         intup=n* (n1)/2;//Super Dense map -          for(intI=0; i<up; i++) $         { $scanf"%d%d%d",&a,&b,&t); -v[a][b]=v[b][a]=T; -         } theprintf"%d\n", Prim (n)); -     }Wuyi     return 0; the}
AC Code

HDU 1233 or 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.