Bellman_Ford模板

Bellman Ford模板

// Bellman_Ford.cpp : Defines the entry point for the console application. // #define MAX_VERTEX_NUM 101 #define INFINITY 0x11111111 #define TRUE 1 #define FALSE 0 typedef struct{ //int info; }VertexType; typedef struct{ int val; //int info; }ArcType,ArcMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; typedef struct{ int vexnum; VertexType vexs[MAX_VERTEX_NUM]; ArcMatrix arcs; }MGraph; typedef int ShortPathTable[MAX_VERTEX_NUM]; bool ShortestPath_BF(MGraph &G, int v0, ShortPathTable &D) { int u,v,w; for (u=0;u<G.vexnum;u++) D[u] = INFINITY; D[v0] = 0; for (u=1;u<G.vexnum;++u){ for (v=0; v<G.vexnum; ++v) for (w=0; w<G.vexnum; ++w) if (D[v]!=INFINITY && D[w] > D[v]+G.arcs[v][w].val) D[w] = D[v]+G.arcs[v][w].val; } for (v=0; v<G.vexnum; ++v) for (w=0; w<G.vexnum; ++w) if (D[v]!=INFINITY && D[w] > D[v]+G.arcs[v][w].val) return true; return false; } #include <string.h> #include <iostream> using namespace std; MGraph g; ShortPathTable d; int main(int argc, char* argv[]) { return 0; } /* int v0; int m,n; int price,power,num; int v,p;int i,j; cin>>m>>n; g.vexnum = n+1; memset(g.arcs,INFINITY,sizeof(g.arcs)); for (i=1;i<=n;i++) { cin>>price>>power>>num; //g.vexs[i].power = power; g.arcs[0][i].val = price; for (j=1;j<=num;j++) { cin>>v>>p; g.arcs[v][i].val = p; } } v0=0; memset(d,INFINITY,sizeof(d)); ShortestPath_BF(g,v0,d); cout<<d[1]<<endl;*/

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/xyzhk01/article/details/5636389
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞