int spfa_bfs(int s)  
{  
    queue <int> q;  
    memset(d,0x3f,sizeof(d));  
    d[s]=0;  
    memset(c,0,sizeof(c));  
    memset(vis,0,sizeof(vis));  
  
    q.push(s);  vis[s]=1; c[s]=1;  
    //??¶?????\???vis???????????°????????????????????¶????????\????¬???°  
    int OK=1;  
    while(!q.empty())  
    {  
        int x;  
        x=q.front(); q.pop();  vis[x]=0;  
        //?????´????´????????????¶????¶?????????°  
        for(int k=f[x]; k!=0; k=nnext[k]) //????????¶???x????????\??¨  
        {  
            int y=v[k];  
            if( d[x]+w[k] < d[y])  
            {  
                d[y]=d[x]+w[k];  //??????  
                if(!vis[y])  //??¶???y?????¨??????  
                {  
                    vis[y]=1;    //?????°  
                    c[y]++;      //???????¬???°  
                    q.push(y);   //??\???  
                    if(c[y]>NN)  //?¶??????\????¬???°???????????´???????´????  
                        return OK=0;  
                }  
            }  
        }  
    }  
  
    return OK;  
  
} 