图论-最短路-BF算法

来源:互联网 发布:淘宝宝贝品牌怎么填 编辑:程序博客网 时间:2024/06/05 22:45

原来一直把好多算法当黑箱子用,但是做题中发现肯定不会考原算法的,我们必须要掌握算法的精髓思路,然后改造它,其实并不难,不要方就行。
今天先看BF算法,思路非常简单,代码也好理解。

#include <iostream>using namespace std;const int INF = 1e9 + 7;const int MAX_V = 100;const int MAX_E = 100;struct edge { int from, to, cost; };edge es[MAX_E];int d[MAX_V];int V, E;void shortest_path(int s) {    for (int i = 0; i < V; i++) d[i] = INF;    d[s] = 0;    while (1) {        bool update = false;        for (int i = 0; i < E; i++) {            edge e = es[i];            if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {                d[e.to] = d[e.from] + e.cost;                update = true;            }        }        if (!update) break;    }}int main() {    return 0;}
0 0
原创粉丝点击