POJ 2485 Highways(最小生成树)

    不知该说好事成双,还是该说有些问题本质是一样的。用Kruskal做完前面那个题目,这个之前没有被解决的Kruskal题也轻易地搞定了。同样是开始的时候用Kruskal出问题,改用Prim过了POJ,现在回过头来用Kruskal算法做,也好,又过了。发出来,或许也会有像我这样曾经被这个问题搞郁闷的朋友。问题抽象一个下就是求Kruskal算法中最后加入的那条边的长度,同样是Prim算法中加入的边的最长的那条。

Kruskal算法C++源代码:

#include <iostream>
#include <algorithm>
using namespace std;
//也就是用Kruskal方法求最短路径时最后加入的那条边
typedef struct Edge{
    int x, y, dis;
};
Edge highway[250001];
bool flag[501];
int nodeSet[501];

bool compare(Edge a, Edge b)
{
    return (a.dis < b.dis);
}
int main()
{
    int tcase, n;
    cin>>tcase;
    while(tcase--){
        scanf("%d", &n);
        int tag = 1;
        for(int i = 1; i <= n; i++){
            nodeSet[i] = i;
            for(int j = 1; j <= n; j++){
                highway[tag].x = i;
                highway[tag].y = j;
                scanf("%d", &highway[tag].dis);
                tag++;
            }
        }
        sort(highway+1, highway+tag, compare);
        memset(flag, 0, sizeof(flag));
        tag = n+1;
        int edgeNum = 0;
        while(edgeNum < n-1){
            if((!flag[highway[tag].x]) && (!flag[highway[tag].y])){
                flag[highway[tag].x] = true;
                flag[highway[tag].y] = true;
                edgeNum++;
                nodeSet[highway[tag].x] = nodeSet[highway[tag].y];
            }else if((!flag[highway[tag].x]) && (flag[highway[tag].y])){
                flag[highway[tag].x] = true;
                edgeNum++;
                nodeSet[highway[tag].x] = nodeSet[highway[tag].y];
            }else if((!flag[highway[tag].y]) && (flag[highway[tag].x])){
                flag[highway[tag].y] = true;
                edgeNum++;
                nodeSet[highway[tag].y] = nodeSet[highway[tag].x];
            }else{
                if(nodeSet[highway[tag].x] != nodeSet[highway[tag].y]){
                    edgeNum++;
                    int tmp = nodeSet[highway[tag].x];
                    for(int i = 1; i <= n; i++)
                        if(nodeSet[i] == tmp)
                            nodeSet[i] = nodeSet[highway[tag].y];
                }
            }
            tag++;
        }
        cout<<highway[tag-1].dis<<endl;
    }
    return 0;
}

 

 


<!---->#include  < iostream >
#include 
< algorithm >
using   namespace  std;
// 也就是用Kruskal方法求最短路径时最后加入的那条边
typedef  struct  Edge{
    
int  x, y, dis;
};
Edge highway[
250001 ];
bool  flag[ 501 ];
int  nodeSet[ 501 ];

bool  compare(Edge a, Edge b)
{
    
return  (a.dis  <  b.dis);
}
int  main()
{
    
int  tcase, n;
    cin
>> tcase;
    
while (tcase -- ){
        scanf(
" %d " & n);
        
int  tag  =   1 ;
        
for ( int  i  =   1 ; i  <=  n; i ++ ){
            nodeSet[i] 
=  i;
            
for ( int  j  =   1 ; j  <=  n; j ++ ){
                highway[tag].x 
=  i;
                highway[tag].y 
=  j;
                scanf(
" %d " & highway[tag].dis);
                tag
++ ;
            }
        }
        sort(highway
+ 1 , highway + tag, compare);
        memset(flag, 
0 sizeof (flag));
        tag 
=  n + 1 ;
        
int  edgeNum  =   0 ;
        
while (edgeNum  <  n - 1 ){
            
if (( ! flag[highway[tag].x])  &&  ( ! flag[highway[tag].y])){
                flag[highway[tag].x] 
=   true ;
                flag[highway[tag].y] 
=   true ;
                edgeNum
++ ;
                nodeSet[highway[tag].x] 
=  nodeSet[highway[tag].y];
            }
else   if (( ! flag[highway[tag].x])  &&  (flag[highway[tag].y])){
                flag[highway[tag].x] 
=   true ;
                edgeNum
++ ;
                nodeSet[highway[tag].x] 
=  nodeSet[highway[tag].y];
            }
else   if (( ! flag[highway[tag].y])  &&  (flag[highway[tag].x])){
                flag[highway[tag].y] 
=   true ;
                edgeNum
++ ;
                nodeSet[highway[tag].y] 
=  nodeSet[highway[tag].x];
            }
else {
                
if (nodeSet[highway[tag].x]  !=  nodeSet[highway[tag].y]){
                    edgeNum
++ ;
                    
int  tmp  =  nodeSet[highway[tag].x];
                    
for ( int  i  =   1 ; i  <=  n; i ++ )
                        
if (nodeSet[i]  ==  tmp)
                            nodeSet[i] 
=  nodeSet[highway[tag].y];
                }
            }
            tag
++ ;
        }
        cout
<< highway[tag - 1 ].dis << endl;
    }
    
return   0 ;
}

Prim算法C++源代码:

#include <iostream>
using namespace std;

int highway[501][501];
int lowcost[501], n;

int prim()
{ 
    for(int i = 1; i <= n; i++)
        lowcost[i] = highway[1][i];
    int ans = 0, nextVex, minEdge;
    for(int i = 1; i < n; i++){
        minEdge = 999999999;
        nextVex = 1;
        for(int j = 1; j <= n; j++){
            if((lowcost[j] < minEdge) && (lowcost[j] > 0)){
                minEdge = lowcost[j];
                nextVex = j;	
            }
        }
        if(minEdge > ans)
            ans = minEdge;
        lowcost[nextVex] = 0;
        for(int j = 1; j <= n; j++){
            if(lowcost[j] > highway[nextVex][j]){
                lowcost[j] = highway[nextVex][j];
            }
        }
    }
    return ans;
}
int main()
{
    int tcase;
    scanf("%d", &tcase);
    while(tcase--)
    {
        scanf("%d", &n);	
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                scanf("%d", &highway[i][j]);
        printf("%d\n", prim());
    }
    return 0;
}

  <!---->#include  < iostream >
using   namespace  std;

int  highway[ 501 ][ 501 ];
int  lowcost[ 501 ], n;

int  prim()

    
for ( int  i  =   1 ; i  <=  n; i ++ )
        lowcost[i] 
=  highway[ 1 ][i];
    
int  ans  =   0 , nextVex, minEdge;
    
for ( int  i  =   1 ; i  <  n; i ++ ){
        minEdge 
=   999999999 ;
        nextVex 
=   1 ;
        
for ( int  j  =   1 ; j  <=  n; j ++ ){
            
if ((lowcost[j]  <  minEdge)  &&  (lowcost[j]  >   0 )){
                minEdge 
=  lowcost[j];
                nextVex 
=  j;    
            }
        }
        
if (minEdge  >  ans)
            ans 
=  minEdge;
        lowcost[nextVex] 
=   0 ;
        
for ( int  j  =   1 ; j  <=  n; j ++ ){
            
if (lowcost[j]  >  highway[nextVex][j]){
                lowcost[j] 
=  highway[nextVex][j];
            }
        }
    }
    
return  ans;
}
int  main()
{
    
int  tcase;
    scanf(
" %d " & tcase);
    
while (tcase -- )
    {
        scanf(
" %d " & n);    
        
for ( int  i  =   1 ; i  <=  n; i ++ )
            
for ( int  j  =   1 ; j  <=  n; j ++ )
                scanf(
" %d " & highway[i][j]);
        printf(
" %d\n " , prim());
    }
    
return   0 ;
}

你可能感兴趣的:(算法,J#)