spfa求最短路模板(邻接矩阵)

int n, dis[300], mapp[300][300], used[300];
int SPFA(int u)
{
    for (int i = 1; i <= n; i++)
    {
        dis[i] = inf;
    }
    queueq;
    q.push(u);
    used[u] = 1;
    dis[u] = 0;
    while (!q.empty())
    {
        int now = q.front();
        used[now] = 0;
        q.pop();
        for (int i = 1; i <= n; i++)
            if (dis[i] > dis[now] + mapp[now][i])
            {
                dis[i] = dis[now] + mapp[now][i];
                if (!used[i])
                {
                    used[i] = 1;
                    q.push(i);
                }
            }
    }
}
void inti()
{
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (i == j)
            {
                mapp[i][j] = 0;
            }
            else
            {
                mapp[i][j] = inf;
            }
}

你可能感兴趣的:(最短路问题)