下面这个代码在PTA上面测试是对的,在其他编译器上显示process returned -1073741571(0xC00000FD),但是如果把MVNum改为100在其他编译器就对了,求指教。谢谢
#include <iostream>
using namespace std;
typedef int Status;
#define OK 1
#define ERROR -1
#define MaxInt 32767
#define MVNum 1000
typedef int VerTexType;
typedef int ArcType;
int cost=0;
typedef struct
{
VerTexType vexs[MVNum];
ArcType arcs[MVNum][MVNum];
int vexnum,arcnum;
}AMGraph;
struct
{
VerTexType adjvex;
ArcType lowcost;
}closedge[MVNum];
int LocateVex(AMGraph &G,VerTexType u)
{
for(int i=0;i<G.vexnum;i++)
{
if(u==G.vexs[i]) return i;
}
return -1;
}
Status CreateUDN(AMGraph& G)
{
cin>>G.vexnum>>G.arcnum;
for(int i=0;i<=G.vexnum;i++)
G.vexs[i]=i;
for(int i=0;i<=G.vexnum;i++)
for(int j=0;j<=G.vexnum;j++)
{
if(i==j)
G.arcs[i][j]=0;
else
G.arcs[i][j]=MaxInt;
}
for(int k=0;k<G.arcnum;k++)
{
int x,y,w;
cin>>x>>y>>w;
G.arcs[x][y]=G.arcs[y][x]=w;
}
return OK;
}
void MiniSpanTree_Prim(AMGraph G,VerTexType u)
{
int k;
k=LocateVex(G,u);
for(int j=0;j<=G.vexnum;j++)
if(j!=k)
closedge[j]={u,G.arcs[k][j]};
for(int i=1;i<G.vexnum;i++)
{
int mincost=MaxInt;
k=-1;
for(int j=1;j<=G.vexnum;j++)
{
if(closedge[j].lowcost!=0&&closedge[j].lowcost<mincost)
{
mincost=closedge[j].lowcost;
k=j;
}
}
if(k!=-1)
{
closedge[k].lowcost=0;
cost+=mincost;
}
else
break;
for(int j=1;j<=G.vexnum;j++)
{
if(G.arcs[k][j]!=0&&G.arcs[k][j]<closedge[j].lowcost)
{
closedge[j]={G.vexs[k],G.arcs[k][j]};
}
}
}
}
int main()
{
AMGraph G;
CreateUDN(G);
MiniSpanTree_Prim(G,1);
int flag=1;
for(int i=1;i<=G.vexnum;i++)
if(closedge[i].lowcost!=0)
{
flag=0;
break;
}
if(flag)
{
cout<<"YES"<<endl;
cout<<cost;
}
else
{
cout<<"NO"<<endl;
cout<<"-1";
}
return 0;
}