最小生成树算法之Prim算法

2020-01-06 13:26:24刘景俊
  • visited[pos]=1;  //update the weight 
  • for(j=1;j<=n;j++)  if(visited[j]==0&&dis[j]>g[pos][j]) 
  • dis[j]=g[pos][j];  } 
  • return ans;  } 
  •   int main() 
  • {  int i=1,j=1; 
  • int ans=0;  int w; 
  • printf("Please enter the number of the nodes:n");  scanf("%d",&n); 
  • for(i=1;i<=n;i++)  for(j=1;j<=n;j++) 
  • {  if(i==j) 
  • g[i][j]=0;  else 
  • g[i][j]=Max;  } 
  • printf("Please enter the number of the edges:n");  int edgenum; 
  • scanf("%d",&edgenum);  int v1,v2; 
  • printf("Please enter the number and the corresponding weight:n");  for(i=1;i<=edgenum;i++) 
  • {  scanf("%d%d%d",&v1,&v2,&w); 
  • g[v1][v2]=g[v2][v1]=w;  } 
  • ans=prim();  printf("The sum of the weight of the edges is:%dn",ans); 
  • system("pause");  return 0; 
  •   } 

    5.程序运行后的结果截图

    最小生成树算法之Prim算法

    以上就是本文的全部内容,希望对大家的学习有所帮助。