Bellman-Ford算法的时间复杂度为O(VE);
#include<iostream>
#include<vector>
#include<cmath>
using std::cout;
using std::endl;
using std::vector;
struct edge
{
int from;
int to;
double weight;
};
struct graph
{
private:
int V;
vector<edge> E;
public:
graph(int v)
: V(v)
{
}
void insert_edge(int from, int to, double weight)
{
edge e;
e.from = from;
e.to = to;
e.weight = weight;
E.push_back(e);
}
void bellman_ford(int s)
{
vector<double> d(V);
for(int i=0;i<V;i++){
d[i]=INFINITY;
}
d[s]=0;
bool updated;
for(int i=0;i<V;i++){
updated=false;
for(auto e:E){
if(d[e.from]+e.weight<d[e.to]){
d[e.to]=d[e.from]+e.weight;