import java.util.Scanner; //普利姆 public class Main { static final int MAXVEX =65535; static final int MINVEX =65535; public static void main(String[] args) { MyGrop p = new MyGrop(); MinSpanTree_frim(p); } static void MinSpanTree_frim (MyGrop G){ int min , i , j , k; int[] adjvex = new int[MAXVEX]; //保存相关定位点得下标 int[] lowcost = new int[MAXVEX];//保存相关定点间边的权值 adjvex [0]=0; //V0作为最小数的跟的开始 lowcost [0]=0; //V0diyige加入 //初始化从操作 for(i =1 ;i<G.a ;i++ ){ lowcost[i] = G.src[0][i];//将零阶矩阵第0行数据填入 adjvex [i]=0; } //构造最小数 for (i=1 ;i<G.a; i++){ min = MINVEX; j=1; k=0; //遍历节点 while(j<G.a){ // 自己和自己连线 if(lowcost[j]!=0 && lowcost[j] <min){ min = lowcost[j]; k=j;//发现较小的下标 } j++; } System.out.println("("+adjvex[k]+")--"+lowcost[k]+"--("+k+")"); lowcost[k]=0;//将权值设置为零 for(j=1;j<G.a;j++){ if(lowcost[j]!=0 && G.src[k][j]<lowcost[j]){ lowcost[j] = G.src[k][j]; adjvex[j] =k; } } } } } class MyGrop{ final int MINVEX =65535; int a = 9; int src[][] = { {0,10,MINVEX ,MINVEX ,MINVEX,11 ,MINVEX ,MINVEX,MINVEX }, {10,0,18,MINVEX ,MINVEX ,MINVEX,16,MINVEX,12}, {MINVEX,18,0,22,MINVEX,MINVEX,MINVEX,MINVEX,8}, {MINVEX,MINVEX,22,0,20,MINVEX,MINVEX,16,21}, {MINVEX,MINVEX,MINVEX,20,0,26,MINVEX,7,MINVEX}, {11,MINVEX,MINVEX,MINVEX,26,0,17,MINVEX,MINVEX}, {MINVEX,16,MINVEX,MINVEX,MINVEX,17,0,19,MINVEX}, {MINVEX,MINVEX,MINVEX,16,7,MINVEX,19,0,MINVEX}, {MINVEX,12,8,21,MINVEX,MINVEX,MINVEX,MINVEX,0}}; void setFun(){ Scanner Scan = new Scanner(System.in); int i=0;int j=0; while(i++<a){ while(j++<a){ src[i][j]=Scan.nextInt(); } } } }
原文地址:http://blog.csdn.net/u012651389/article/details/44922547