bellman_ford 模板

const int INF = 0x3f3f3f3f;
const int MAXN = 550;

int dis[MAXN];

struct Edge
{
    int u, v, cost;
    Edge(int _u = 0, int _v = 0, int _cost = 0) :u (_u),v(_v), cost(_cost){};
};

vector<Edge> E;

bool bellman_ford(int start,int n)
{
    for (int i = 0;i <= n;i++) dis[i] = INF;
    dis[start] = 0;
    for (int i = 1;i < n;i++)
    {
        bool flag = false;
        for (int j = 0;j < E.size();j++)
        {
            int u = E[j].u;
            int v = E[j].v;
            int cost = E[j].cost;
            if (dis[v] > dis[u] + cost)
            {
                dis[v] = dis[u] + cost;
                flag = true;
            }
        }
        if (!flag) return true;
    }

    for (int j = 0;j < E.size();j++)
    {
        if (dis[E[j].v] > dis[E[j].u] + E[j].cost)
            return false;
    }
    return true;
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/u014427196/article/details/49945673
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞