cin>>t;

while (t--)
{

    cin>>n;
    cin>>m;

    if (n==m)
        cout<<"0\n";
    else
    {
        //Breadth First Search Tree:
        queue <gnode*> gqueue;

        bft_data bft=bft_data();


        list<gnode>* gp;
        list<gnode>::iterator it;

        gp=_gfind(graph,n);// finds list containing gnode with n value
        gnode* st=gfind(gp,n),*tmp,*_st;//finds gnode with n value
        _st=st;

        bft.w[st->v]=0;
        bft.pn[st->v]=NULL;
        bft.c[st->v]=1;

        gqueue.push(st);

        while (!gqueue.empty())
        {
            st=gqueue.front();
            gqueue.pop();

            gp=_gfind(graph,st->v);

            it=(*gp).begin();
            for (++it;it!=(*gp).end();it++)//initialized with ++it to skip fist element of list
            {
                tmp=&(*it);
                // cout<<tmp->v<<"\n";
                //  getchar();
                if (bft.c[tmp->v]==0)
                {
                    bft.pn[tmp->v]=st;
                    bft.c[tmp->v]=1;
                    bft.w[tmp->v]=bft.w[st->v]+1;

                    gqueue.push(tmp);
                }
            }

        }


       if(bft.w[m]!=SIZE)
        cout<<bft.w[m]<<"\n";
        else
        cout<<"Impossible\n";
bft.~bft_data();
    }
}

此代码片段通过构造bfs树来计算节点与n和m值节点之间的距离b/w,但不知何故,在第一次外部while循环迭代中构造的bft树保留了它的值,进一步的while循环迭代对bft没有影响。
图的类型是vector<list<gnode>>
bfs是bft_类数据的对象:
class bft_data
{
public:
int w[SIZE],c[SIZE];
gnode* pn[SIZE];

bft_data()
{
    memset(w,SIZE,SIZE);
    memset(c,0,SIZE);
    memset(pn,NULL,SIZE);
}

 };

最佳答案

我还没有看过您的第一个代码提取,但是第二个提取可能解释了这个问题:memset的第三个参数是字节计数,因此您必须将它乘以数组元素的大小:

memset(w,SIZE,SIZE * sizeof(int));
memset(c,0,SIZE * sizeof(int));
memset(pn,NULL,SIZE * sizeof (gnode*));

07-24 13:33