zoj 1586(Segmentation Fault )求指点
#include <stdio.h>#include <malloc.h>
#define MaxVertices 250
#define MaxWeight 1000000
typedef struct
{
int graph[MaxVertices];
int edge[MaxVertices][MaxVertices];
int numEdge;
int numVer;
}AdjMGraph;
void Initiate(AdjMGraph * G,int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(i==j)
G->edge[i][j]=0;
else
G->edge[i][j]=MaxWeight;
G->numEdge=0;
G->numVer=n;
}
void InsertEdge(AdjMGraph * G,int v1,int v2,int weight)
{
G->edge[v1][v2]=weight;
G->edge[v2][v1]=weight;
G->numEdge+=2;
}
int Prim(AdjMGraph G)
{
int sum=0,minWeight,i,j,k,flag[MaxVertices];
for(i=1;i<G.numVer;i++)
flag[i]=G.edge[0][i];
for(i=1;i<G.numVer;i++)
{
minWeight=MaxWeight;
for(j=1;j<G.numVer;j++)
if(flag[j]<minWeight&&flag[j]>0)
{
minWeight=flag[j];
k=j;
}
sum+=minWeight;
flag[k]=-1;
for(j=1;j<G.numVer;j++)
if(G.edge[k][j]<flag[j])
flag[j]=G.edge[k][j];
}
return sum;
}
int main()
{
int n,i,m,j,x;
scanf("%d",&n);
for(i=0;i<n;i++)
{
AdjMGraph G;
scanf("%d",&m);
Initiate(&G,m);
for(j=0;j<m;j++)
scanf("%d",&G.graph[j]);
for(i=0;i<m;i++)
for(j=0;j<m;j++)
{
scanf("%d",&x);
if(x==0)
continue;
G.edge[i][j]=x+G.graph[i]+G.graph[j];
}
printf("%d\n",Prim(G));
}
return 0;
}