qq_36323751
qq_36323751
2017-04-12 13:24

C++:非法访问是怎么回事

  • 最小生成树
  • prim算法
  • c++
  • 最小堆

#include
#include
using namespace std;
#define MAX 100
#define inf 10000
int A[MAX][MAX],visited[MAX],g[MAX];
typedef int elem_t;
typedef struct HeapStruct *Minheap;
struct HeapStruct{
elem_t H[MAX];//存储堆元素的数组
elem_t ph[MAX];
int size;//堆当前的元素个数
};

void Bubbleup(HeapStruct Heap,int n)
{
while(n>1)
{
if(Heap.H[n]<Heap.H [n/2])
{
int tmp=Heap.H [n];
Heap.H[n]=Heap.H[n/2];
Heap.H[n/2]=tmp;
n=n/2;
}
else
break;
}

}

int ExtractMin(HeapStruct Heap)
{
int M=Heap.H[1];
int pa,child,temp1;
temp1=Heap.H[Heap.size];
Heap.size--;
//Heap.H[Heap.size]=0;
for(pa=1;pa*2 <=Heap.size ;pa=child)//向下冒泡
{
child=2*pa;
if(Heap.H[child] > Heap.H[child+1]&&Heap.size>=child+1)
child++;
if(temp1 > Heap.H[child])
Heap.H[pa] = Heap.H[child];
else
break;
}
Heap.H[pa]=temp1;
visited[g[M]]=1;//这里出的问题
return M;
}

void Heapinsert(HeapStruct Heap,int n)
{
Heap.H[++Heap.size]= n;
Bubbleup(Heap,Heap.size);
}

void prim(int m,int n,int A[MAX][MAX],int p[],HeapStruct Heap)
{
p[m]=0;
int temp=m;
visited[m]=1;
for(int k=1;k<=n;k++)//生成堆
{
if(A[m][k]!=0)
{
p[k]=m;
g[A[m][k]]=k;
Heapinsert(Heap,A[m][k]);
Heap.ph[k]=Heap.size;
}
}
m=g[ExtractMin(Heap)];
while(Heap.size!=0)
{
visited[m]=1;
for(int i=1;i<=n;i++)
{
if(A[m][i]<Heap.H[Heap.ph[i]]&&A[m][i]!=0&&visited[i]!=1)
{
p[i]=m;
g[A[m][i]]=i;
Heap.H[Heap.ph[i]]=A[m][i];
Bubbleup(Heap,Heap.ph[i]);
}
}
m=g[ExtractMin(Heap)];

}
for(int j=1;j<=n;j++)
if(j!=temp)
cout<

"<<j<<endl;

}

First-chance exception in project2.exe: 0xC0000005: Access Violation.

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答