7-20 Minimum Spanning Tree

输入格式:

1 line. A sequence of numbers divided by comma, Every number represent the weight of edge of the Minimum Spanning Tree. The sequence is in ascending orders.

输出格式:

1 line.
A sequence of numbers divided by comma. Every number represent the weight of edge of the Minimum Spanning Tree. The sequence is in ascending orders.

输入样例:

在这里给出一组输入。例如:

A,B,1

输出样例:

在这里给出相应的输出。例如:

1,1,1,2,2,2,3,4,7,

C语言代码:

#include<stdio.h>
#include<stdlib.h>
#define infinity 999


void selectionsort (int * A,int N)
{
	int i,j,k;
     for ( i = 0 ; i < N-1 ; i++ )
     {
        for  (k=i , j=i+1 ; j < N ; j++) 
            if  (A[j]<A[k])  k=j;
            if  ( k != i ){
            	int temp=A[i];
				A[i]=A[k];
				A[k]=temp; 
			}
    }
}


main(){
    int Graph[10][10];
    int i,j;
    for(i=0;i<10;i++){
        for(j=0;j<10;j++){
            if(i==j) Graph[i][j]=0;
            else Graph[i][j]=-1;
        }
    }
    Graph[0][1]=Graph[1][0]=3;
    Graph[0][4]=Graph[4][0]=4;
    Graph[0][3]=Graph[3][0]=4;
    Graph[1][2]=Graph[2][1]=10;
    Graph[1][5]=Graph[5][1]=3;
    Graph[1][4]=Graph[4][1]=2;
    Graph[2][5]=Graph[5][2]=6;
    Graph[2][6]=Graph[6][2]=1;
    Graph[3][4]=Graph[4][3]=5;
    Graph[3][7]=Graph[7][3]=6;
    Graph[4][7]=Graph[7][4]=2;
    Graph[4][5]=Graph[5][4]=11;
    Graph[4][8]=Graph[8][4]=1;
    Graph[5][6]=Graph[6][5]=2;
    Graph[5][8]=Graph[8][5]=3;
    Graph[5][9]=Graph[9][5]=11;
    Graph[6][9]=Graph[9][6]=8;
    Graph[7][8]=Graph[8][7]=4;
    Graph[8][9]=Graph[9][8]=7;
    //input: modify 
    char a,b;
    int len;
    scanf("%c,%c,%d",&a,&b,&len);
    Graph[a-'A'][b-'A']=Graph[b-'A'][a-'A']=len;
    //initialize
    int superNode[10];//node in the MST list or not. 1 for in, -1 for not judged, 0 for to be judged
    int disk[10];
    int Edge[9];
    int start=0,sizeE=0;//random start Vertex is A
    superNode[0]=1;disk[0]=infinity;
    for(i=1;i<10;i++){
        superNode[i]=-1;
        disk[i]=infinity;
    }
    //MST
    for(j=1;j<10;j++){
        if(Graph[0][j]!=-1){
            superNode[j]=0;
            disk[j]=Graph[0][j];
        }
    }
    for(;sizeE<9;){
        //从相邻顶点中找到最小的边
        int min=disk[0],minid=0;
        for(i=1;i<10;i++){
            if(superNode[i]==0&&disk[i]<min){
                min=disk[i];
                minid=i;
            }
        }
        //将最小的边加入Edge
        Edge[sizeE++]=min;
        //将最小的顶点归入supernode
        disk[minid]=infinity;
        superNode[minid]=1;
        for(i=0;i<10;i++){
            if(superNode[i]!=1&&Graph[minid][i]!=-1){
                superNode[i]=0;
                if(Graph[minid][i]<disk[i]){
                    disk[i]=Graph[minid][i];
                }
            }
        }
    }
    selectionsort(Edge,9);
    for(i=0;i<9;i++){
        printf("%d,",Edge[i]);
    }
    return 0;
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值