Re-discussion on Bellman-ford algorithm

Source: Internet
Author: User

Prerequisites: Understanding the Bellman-ford algorithm

Basic model:

#include <iostream>using namespace std; #define INF 0x7fffffff#define Maxx 100int G[maxx][maxx],dis[maxx];int n,m; int min (int a,int b) {return (A&GT;B?B:A);}        void init (int n) {for (int i=1;i<=n;i++) {dis[i]=0;    for (int j=1;j<=n;j++) g[i][j]= (I==j?0:inf);    }}void Bellman_fory () {for (int i=1;i<=n;i++) dis[i]=g[1][i];    dis[1]=0;        for (int k=2;k<=n;k++)//From Dist (1) [i] Hand eject dist (2) [I],..., Dist (n-1) [i] {for (int i=1;i<=n;i++)//modify Dist[i per vertex] {if (i!=1) for (int j=1;j<=n;j++) {//Vertex J to I have a direct path and pass vertices J can make Dist[i] shorten if (G[j][i]<inf&&dis[i]>dis[j]+g[j][i]) dis[i]=dis[j                ]+g[j][i]; }}}/* To determine if there is a negative weight loop for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {if (G I        [J]<inf&&dis[j]>dis[i]+g[i][j]) return false; } RETurn true;    } */}int Main () {cin>>n>>m;    int a,b,c;    Init (n);        for (int i=1;i<=m;i++) {cin>>a>>b>>c;    G[a][b]=c;    } bellman_fory ();    for (int i=1;i<=n;i++) cout<<dis[i]<< ""; Cout<<endl;}

Further discussion:

Implemented with Adjacency table (complexity is O (n^2))

#include <iostream>using namespace std; #define INF 0x7fffffff#define maxx 1010typedef struct edge{int u,v; int weight;} Edge;    Edge Edge[maxx];int dist[maxx];int n,e,s;//node, edge number, source point void init () {cin>>n>>e>>s;    for (int i=1;i<n;i++) Dist[i]=inf;    dist[s]=0;        for (int i=1;i<=e;i++) {cin>>edge[i].u>>edge[i].v>>edge[i].weight;    if (edge[i].u==s) dist[edge[i].v]=edge[i].weight; }}bool Bellman_ford () {for (Int. i=1;i<=n-1;i++) for (int j=1;j<=n;j++) if (EDGE[J].WEIGHT&LT;INF&A    Mp;&dist[edge[j].v]>dist[edge[j].u]+edge[j].weight) Dist[edge[j].v]=dist[edge[j].u]+edge[j].weight;    BOOL flag = 1; for (int i=1;i<e;i++) if (Edge[j].weight<inf&&&&dist[edge[i].v]>dist[edge[i].u]+edge[i].            Weight) {flag = 0;        Break } return flag;    int main () {init (); if (Bellman_ford ()) for (int i=1;i<=n;i++) cout<<dist[i]<<endl;    else cout<< "No" <<endl; return 0;}

Test examples:

7 10
1 2 6
1 3 5
1 4 5
2 5-1
3 2-2
3 5 1
4 3-2
4 6-1
5 7 3
6 7 3

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Re-discussion on Bellman-ford algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.