bellman_ford

#include<bits/stdc++.h>

const int maxint=10000000;

using namespace std;

struct edgenode{

int st;

int ed;

int weight;

};

class graph{

private:

int nodenum;//µãÊýÄ¿ 

int edgenum;//±ßÊýÄ¿

edgenode *edge;

int *dist;

public:

graph(){

cin>>nodenum;

cin>>edgenum;

edge=new edgenode[edgenum];

dist=new int[nodenum];

}

void inputedge(){

int i;

for(i=0;i<edgenum;i++)cin>>edge[i].st>>edge[i].ed>>edge[i].weight;

bool bellman_ford(){

int i,j,start;

cin>>start;

for(i=0;i<nodenum;i++)dist[i]=maxint;

dist[start]=0;

cout<<nodenum<<edgenum<<endl;

for(i=1;i<nodenum;i++){

for(j=0;i<edgenum;j++){

if(dist[edge[j].st]+edge[j].weight<dist[edge[j].ed])

dist[edge[j].ed]=dist[edge[j].st]+edge[j].weight;

}

}

for(i=0;i<edgenum;i++){

if(dist[edge[i].st]+edge[i].weight<dist[edge[i].ed])

return false;

}

return true;

}

};

int main(){

graph g1=graph();

g1.inputedge();

cout<<g1.bellman_ford()<<endl;

return 0;

}

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