Minimum spanning Tree prim () algorithm;

Source: Internet
Author: User

1. The minimum spanning tree refers to the sum of the paths of all points connected;

The 2.prim algorithm is extended from one point to all points, starting in the spanning tree with only one node V, the first extension, the node closest to the smallest spanning tree is added to the minimum spanning tree, while the minimum spanning tree is updated to the shortest path to the other nodes, and so on until all nodes are joined to the minimum spanning tree.

3. Taking poj2485 as an example,

Test instructions: The maximum edge length in the minimum spanning tree is obtained;

Source:

#include <iostream>
#include <stdio.h>
#define MAX 999999
using namespace Std;
int mapp[510][510];
int check ()
{
int i,j,n;
int maxx=0;
BOOL v[600];
int dist[600];
cin>>n;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++) {
scanf ("%d", &mapp[i][j]);
if (mapp[i][j]==0) Mapp[i][j]=max;
}
for (i=1;i<=600;i++) V[i]=false;
Extend from the first node
V[1]=true;
for (i=1;i<=n;i++) {
Dist[i]=mapp[1][i];
}
Need to add a n-1 point
for (i=1;i<=n-1;i++)
{
int Mini=0,minn=max;
for (j=1;j<=n;j++)
if (!v[j]&&minn>dist[j])
{
MINN=DIST[J];
Mini=j;
}
if (Maxx<minn) Maxx=minn;
V[mini]=true;
for (j=1;j<=n;j++)
if (!v[j]&&dist[j]>mapp[mini][j]) {
DIST[J]=MAPP[MINI][J];
}
}
Return Maxx;
}
int main ()
{
int t,i;
cin>>t;
for (i=1;i<=t;i++)
{
Cout<<check () <<endl;
}
return 0;
}

Minimum spanning Tree prim () algorithm;

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.