这是我在cpp中编写的示例代码。在这个类定义中,我声明了一个变量,我试图使用在类本身内部定义的函数来改变它的值。一旦此功能结束,它就会发生变化,但一旦退出该功能,更改就不会生效。我真的需要一些帮助。试着破解这个很长一段时间。
class graph
{
public:
int v[100][3],v1[100][3],e[100][2],q[100][2],s[100];
public:
int sizeq,sizes,vlength,elength;
void sets()
{
for(int i=0;i<100;i++)
{
s[i]=0;
}
sizes = 0;
}
void setq()
{
for(int i=0;i<vlength;i++)
{
q[i][0]=v[i][0];
q[i][1]=v[i][1];
}
sizeq = vlength;
}
void setcred(int vert[100][3],int edg[100][2],int nv,int ne)
{
for(int i=0;i<nv;i++)
{
v1[i][0]=vert[i][0];
v[i][0]=vert[i][0];
v[i][1]=1000;
v[i][2]=NULL;
v1[i][1]=vert[i][1];
v1[i][2]=vert[i][2];
}
vlength = nv;
for(int i=0;i<ne;i++)
{
e[i][0]=edg[i][0];
e[i][1]=edg[i][1];
}
elength = ne;
}
int extract()
{
int ret;
int i,min,index;
min = q[0][1];
ret = q[0][0];
index = 0;
//cout<<"min="<<min<<endl;
for(i=0;i<sizeq;i++)
{
//cout<<"in the for loop"<<endl;
//cout<<q[i][1]<<"\t"<<q[i][0]<<endl;
if(q[i][1] < min)
{
min = q[i][1];
ret = q[i][0];
index = i;
}
}
q[index][0]=q[sizeq -1][0];
q[index][1]=q[sizeq -1][1];
sizeq = sizeq - 1;
return(ret);
}
void unions(int u)
{
s[sizes]=u;
sizes = sizes + 1;
}
int adj(int u, int i)
{
int ret;
ret=0;
for(int j=0;j<elength;j++)
{
if(e[j][0] == u && e[j][1] == i)
{
ret=1;
break;
}
}
return(ret);
}
void changev(int i,int j,int k)
{
v[i][1]=k;
}
void relaset(int ver,int c,int u)
{
int index,ve,co,us;
ve=ver;
co=c;
us=u;
v[ve][1]=co;
v[ve][2]=us;
for(int i=0;i<sizeq;i++)
{
if(q[i][0]==ve)
{
index=i;
break;
}
}
q[index][1]=co;
//for(int i = 0; i < sizeq ; i++)
//{
// cout<<q[i][0]<<"\t"<<q[i][1]<<"\n";
// }
}
};
void initialize(graph g1,int i);
void relax(int u,int ver,graph g1);
void pathing(int t,graph g1);
int path[100];
int main()
{
graph g1;
int vert[100][3],edg[100][2];
int u,nv,ne;
vert[0][0] = 0;
vert[0][1] = 88;
vert[0][2] = 92;
vert[1][0] = 1;
vert[1][1] = 88;
vert[1][2] = 237;
vert[2][0] = 2;
vert[2][1] = 88;
vert[2][2] = 382;
vert[3][0] = 3;
vert[3][1] = 259;
vert[3][2] = 237;
vert[4][0] = 4;
vert[4][1] = 430;
vert[4][2] = 382;
vert[5][0] = 5;
vert[5][1] = 430;
vert[5][2] = 237;
vert[6][0] = 6;
vert[6][1] = 259;
vert[6][2] = 382;
vert[7][0] = 7;
vert[7][1] = 430;
vert[7][2] = 92;
edg[0][0] = 0;
edg[0][1] = 1;
edg[1][0] = 1;
edg[1][1] = 2;
edg[2][0] = 1;
edg[2][1] = 3;
edg[3][0] = 3;
edg[3][1] = 5;
edg[4][0] = 2;
edg[4][1] = 6;
edg[5][0] = 6;
edg[5][1] = 4;
edg[6][0] = 4;
edg[6][1] = 5;
edg[7][0] = 5;
edg[7][1] = 7;
nv = 8;
ne = 8;
g1.setcred(vert,edg,nv,ne);
//dijksta's algorithm starts from here
g1.changev(0,1,0);
g1.sets();
g1.setq();
while(g1.sizeq!=0)
{
u=g1.extract();
g1.unions(u);
for(int i=0;i<g1.vlength;i++)
{
if(g1.adj(u,i)==1)
{
relax(u,i,g1);
}
}
}
//now make the path
pathing(7,g1);
return 0;
}
void relax(int u,int ver,graph g1)
{
int du,dv,setter,combo;
int index;
du = g1.v[u][1];
dv = g1.v[ver][1];
combo=du+1;
setter=u;
if(dv>combo)
{
g1.relaset(ver,combo,u);
/*g1.v[ver][1]=combo;
g1.v[ver][2]=u;
for(int i=0;i<g1.sizeq;i++)
{
if(g1.q[i][0]==ver)
{
index=i;
break;
}
}
g1.q[index][1]=combo;*/
}
}
void pathing(int t,graph g1)
{
/*for(int i = 0; i < 8 ; i++)
{
cout<<"here";
cout<<g1.v1[i][0]<<"\t"<<g1.v1[i][1]<<"\t"<<g1.v1[i][2]<<"\n";
}*/
int p;
p=t;
while(p!=0)
{
cout<<p<<endl;
p = g1.v[p][2];
}
cout<<"0"<<"\n";
}