SPFA

来源:互联网 发布:pop3服务器端口去哪差 编辑:程序博客网 时间:2024/06/10 18:17
int spfa(int s,int t,int n)
{
    memset(vis,0,sizeof(vis));
    memset(o,0,sizeof(o));
    for (int i=0;i<=n;i++) dist[i]=INF;
    dist[s]=0;
    vis[s]=1;
    queue<int>q;
    q.push(s);
    while (!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        o[u]++;
        if (o[u]>n) return -1;
        for (int i=head[u];i;i=e[i].next)
        {
            int temp=dist[u]+e[i].w;
            if (temp< dist[e[i].to])
            {
                dist[e[i].to]=temp;
                if (!vis[e[i].to])
                {
                    vis[e[i].to]=1;
                    q.push(e[i].to);
                }
            }
        }
    }
    return dist[t];
}

0 0
原创粉丝点击