www.pudn.com > Dijkstra_Huffman_Prim.rar > Prim.cpp


// Prim.cpp : Defines the entry point for the console application. 
// 
 
#include "stdafx.h" 
#include  
#include 
#define MAX 999 /*设MAX为权值的上限*/ 
#define M 40   /*M为大于图顶点数的常量*/ 
 
int ad[M][M];   /*网的相邻矩阵*/ 
 
/*用普里姆算法从顶点v出发构造有n个顶点的图的最小生成树T,输出T的各条边*/ 
void prim(int n,int v)  /*n为图的顶点数,v为出发顶点*/ 
{ 
	int lowcost[M],closest[M]; 
	int i,j,k,min,s=0; 
	FILE *fp; 
	fp=fopen("outPrim.txt","w"); 
	for(i=1; i<=n; i++) 
	{ 
		lowcost[i]=ad[v][i]; 
		closest[i]=v; 
	} 
	printf("该图的最小生树为:\n"); 
	fprintf(fp,"该图的最小生树为:\n"); 
	for(i=1; i>t; 
  n=t; 
  printf("已知图的顶点数为%d\n",n); 
  while(!tp.eof()){ 
	  tp>>t; 
	  printf("<%d,%d>=%-3d  ",i,j,t); 
	  ad[i][j]=t; 
	  j++; 
	  if(j==n+1){ 
		  printf("\n"); 
		  i++; 
		  j=1; 
	  } 
	  if(i==n+1)break; 
  } 
  tp.close(); 
  } 
	v=1; 
	prim(n,v); 
}