prim 演算法(prim algorithm)

來源:互聯網
上載者:User

標籤: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 program6 101 2 6 1 3 11 4 52 3 52 5 33 5 63 6 44 6 23 4 55 6 6*/

 

prim 演算法(prim algorithm)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.