Prim minimum spanning tree algorithm problem poj2485

Source: Internet
Author: User

Start to write runtime error using Kruskal algorithm


#include <cstdio> #include <queue> #include <set>using namespace Std;int a[2510][25100];struct weight    {int A, B;    int value;    BOOL operator < (const weight & RHS) Const {return value < Rhs.value;    } BOOL operator > (const weight & RHS) Const {return value > rhs.value;    }};int sett[251000];int find2 (int x) {while (x! = Sett[x]) x=sett[x]; return x;}    void Merge2 (int a,int b) {if (a > B) sett[a]=b; else Sett[b]=a;}    int main () {int t;    scanf ("%d", &t);        while (t--) {priority_queue<weight, vector<weight>, greater<weight> > PQ;        int n;        scanf ("%d", &n);        for (int i=0;i<n;i++) sett[i]=i;                for (int i=0;i<n;i++) for (int j=0;j<n;j++) {int x;                scanf ("%d", &x);                if (x = = 0) x = 65537;                Weight route;                Route.a=i;                Route.b=j; Route.value=X            Pq.push (route);        } set<int> drop;//printf ("size=%d n=%d\n", Drop.size (), n);            while (Drop.size ()! = n-1) {//printf ("size=%d n=%d\n", Drop.size (), n);            Weight mi = pq.top ();            Pq.pop ();            int FX =find2 (MI.A);            int fy =find2 (MI.B);                if (fx!=fy) {Drop.insert (mi.value);            Merge2 (FX,FY); }}//printf ("size%d\n", Drop.size ());//For (Set<int>::iterator it=drop.begin (); It!=drop.end (); it++) {//printf ("dsa\n");//printf ("%d", *it);/} Set<int>::iterator it = drop.        End ();        it--;    printf ("%d\n", *it); } return 0;} /*140 1 2 31 0 2 32 2 0 33 3 3 0-1073741819-1073741819*/
Then write the prim algorithm

#include <cstdio> #include <algorithm> #include <cstring>using namespace Std;int cost[510][510];int Mincost[510];bool used[510];int N;int Prim () {    memset (used,false,sizeof (used));    memset (mincost,65537,sizeof (Mincost));    int maxres=0;    mincost[0]=0;    while (true) {        int v=-1;        for (int i=0;i<n;i++)            if (!used[i] && (v==-1 | | mincost[i]<mincost[v])  ) v=i;        if (v = =-1) break;        Used[v]=true;        if (Mincost[v] >maxres) maxres=mincost[v];        for (int i=0;i<n;i++)            mincost[i] = min (Mincost[i], cost[v][i]);    }    return maxres;} int main () {    int cases;    scanf ("%d", &cases);    while (cases--) {            scanf ("%d", &n);    for (int i=0;i<n;i++) for        (int j=0;j<n;j++)    {        int x;        scanf ("%d", &cost[i][j]);        if (cost[i][j]== 0) cost[i][j] = 65537;    }    printf ("%d\n", Prim ());    }    return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Prim minimum spanning tree algorithm problem poj2485

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.