码迷,mamicode.com
首页 > 编程语言 > 详细

prim 算法(prim algorithm)

时间:2017-09-23 13:34:30      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:orm   ase   min   ogr   ber   span   names   sed   logs   

#include <iostream>
#include <math.h>
#include <list>


using namespace std;


void prim(int n, int u0, double **c)
{
    bool s[n];
    int closest[n];
    double lowcost[n];
    for(int i=0; i<n ;i++)
    {
        lowcost[i] = c[u0][i];
        closest[i] = u0;
        s[i] = false;
    }
    s[u0] = true;   //u0 is the origin, so the closet[u0] = -1, show that no point can reach it
    closest[u0] = -1;

    for(int i=0; i<n; i++)   //this can be n-1 times, because there are all n points, we choose the origin u0, leave n-1 points, get one point one times, so we need n-1 times cycle;
    {
        double temp = 0x7fffffff;
        int t = u0;
        for(int j=0; j<n; j++)
        {
            if(!s[j]&&lowcost[j]<temp)
            {
                temp = lowcost[j];
                t = j;
            }
        }
        if(t==u0)
            break;    //this means we have got the minimum spanning tree or the graph is not connected
        s[t] = true;
        for(int j=0; j<n ;j++)
        {
            if(!s[j]&&lowcost[j]>c[t][j])
            {
                lowcost[j] = c[t][j];
                closest[j] = t;
            }
        }

    }
    for(int i=0; i<n; i++)
    {
        if(s[i]==false)
        {
            cout << "the graph is not connected!!!";
            return;
        }
    }
   list<int> stack1;
   stack1.push_back(-1);

   while(stack1.size()!=0)  //output the result, the format is : firstcity secondcity, this shows there are relation between the two cities,if it is -s before one city, this city is the start city
   {
       int k = stack1.front();
       stack1.pop_front();
       for(int i=0; i<n; i++)
       {
           if(closest[i]==k)
           {
             cout << k << " " << i << endl;
             stack1.push_back(i);
           }

       }
   }


}

int main()
{
    int n;
    cout << "please input the number of cities and the number of relations\n" ;
    cin >> n;
    double **map = new double*[n];
    for(int i=0; i<n; i++)
    {
        map[i] = new double[n];
    }

    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
    {
        map[i][j] = 0x7fffffff;
    }


    int m, temp1, temp2;
    double temp3;

    cin >> m;
    for(int k=0; k<m; k++)
    {
        cin >> temp1 >> temp2 >> temp3;   //the input file I writed it previous is wrong, just need to modyfy the index of city, the right index is equal to wrong index - 1
        map[temp1-1][temp2-1] = map[temp2-1][temp1-1] = temp3;
    }


    prim(n, 1, map);


    return 0;

}


/*
data used to test
the number of city is wrong, so i minus 1 from them in program 6 10 1 2 6 1 3 1 1 4 5 2 3 5 2 5 3 3 5 6 3 6 4 4 6 2 3 4 5 5 6 6 */

 

prim 算法(prim algorithm)

标签:orm   ase   min   ogr   ber   span   names   sed   logs   

原文地址:http://www.cnblogs.com/1915884031A-qqcom/p/7580763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!