bellman_ford

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Masqueradey/article/details/80275338
#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;
}

猜你喜欢

转载自blog.csdn.net/Masqueradey/article/details/80275338