中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

貪心Kruskal算法生成樹C實(shí)現(xiàn)代碼

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
    #include <iostream.h>  
    #define Max 100  
      
    typedef struct{  
             int u;  
             int v;  
             int weight;  
    }edge;  
    edge edges[Max];  
    int nodes[Max];  
    void interchange(edge* m,edge* n)  
    {  
        edge temp=*m;  
        *m=*n;  
        *n=temp;  
      
    }  
    int partition(edge array[],int p,int q)  
    {  
        int i,j;  
        i=p;  
        j=q+1;  
        while(1)  
        {  
            do i++;  
            while((array[i].weight<array[p].weight)&&(i!=q));  
            do j--;  
            while((array[j].weight>array[p].weight)&&(j!=p));  
            if(i<j)  
                interchange(&array[i],&array[j]);  
            else  
                break;  
        }  
        interchange(&array[p],&array[j]);  
        return j;  
      
    }  
    void quicksort(edge array[],int p,int q)  
    {  
        int j;  
        if (p<q)  
        {  
            j=partition(array,p,q);  
            quicksort(array,p,j-1);  
            quicksort(array,j+1,q);  
        }  
    }  
    void main()  
    {  
            int i,j,m, n, nodenum, edgenum,safe,cost=0,flag=1 ;  
            int presult = 0;  
      
            cout<<"Input the number of nodes and edges:";  
            cin>>nodenum>>edgenum;  
            cout<<"請(qǐng)輸入每條邊的起點(diǎn)、終點(diǎn)及權(quán)值:"<<endl;  
            for(i = 0; i < edgenum; i++)  
            {  
                 cin>>edges[i].u>>edges[i].v>>edges[i].weight;  
                   
            }  
            for(i=1;i<=nodenum;i++)  
                nodes[i]=0;  
            quicksort(edges,0,edgenum-1);  
            for(i = 0; i < edgenum ; i++)  
            {                 
                    m = edges[i].u;  
                    n = edges[i].v;  
                    safe = 0;  
                    if(nodes[m] == 0 &&nodes[n] == 0){  
                            nodes[m] = flag;  
                             nodes[n] =flag;  
                             flag++;  
                            safe = 1;//a safe edge  
      
                    }  
                    else  
                    {  
                        if(nodes[m] == 0 ||nodes[n] == 0 )  
                        {  
                            if(nodes[m] == 0 )                          
                                nodes[m] = nodes[n];  
                            else nodes[n]=nodes[m];  
                              
                            safe = 1;//a safe edge  
                        }  
                        else   
                            if(nodes[m] != nodes[n])  
                            {                     
                                  for(j = 1; j <= nodenum; j++)  
                                  {  
                                    if((nodes[j] == nodes[m] || nodes[j] == nodes[n])&&(j!=m&&j!=n))  
                                    {  
                                            nodes[j] = flag;  
                                    }  
                                  }    
                                  nodes[m]=flag;  
                                  nodes[n]=flag;  
                                  flag++;                            
                                  safe = 1;//a safe edge  
      
                            }  
      
                    }  
                       
                    if(safe == 1){//reserve a safe edge  
      
                            edges[presult].u = m;  
                            edges[presult].v = n;  
                            edges[presult].weight = edges[i].weight;  
                            presult++;  
                    }  
             
                  if(presult == nodenum-1 ){//found mst  
      
                            break;  
                    }   
            }  
            cout<<"Print the result:"<<endl;  
            cout<<"起點(diǎn)   終點(diǎn)   權(quán)值"<<endl;  
                    for(i = 0; i < presult; i++)  
                    {  
                        cost=cost+edges[i].weight;  
                        cout<<edges[i].u<<"       "<< edges[i].v<<"         "<< edges[i].weight<<endl;  
                    }  
                    cout<<"最小生成樹的權(quán)值為:"<<cost<<endl;  
                     
    }  

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:android將sqlite數(shù)據(jù)庫(kù)與程序一起發(fā)布

下一篇:經(jīng)典算法2:遞歸求解整數(shù)劃分