求解带有负权边的图的最短路
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef long long ll;
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
int n, m, lc[MAXN];
struct Edge {
int u, v, w;
Edge(int u, int v, int w) :u(u), v(v), w(w) {}
};
vector<Edge> E;
bool bellman_ford(int start) {
for (int i = 0; i < n; i++)
lc[i] = (i == start ? 0 : INF);
//松弛n-1次
for (int i = 0; i < n - 1; i++) {
bool judge = false;
for (int j = 0; j < E.size(); j++) {
if (lc[E[j].v] > lc[E[j].u] + E[j].w) {
lc[E[j].v] = lc[E[j].u] + E[j].w;
judge = true;
}
}
//此次松弛lc并未发生改变,说明松弛完成
if (!judge)
return true;
}
for (int j = 0; j < E.size(); j++)
if (lc[E[j].v] > lc[E[j].u] + E[j].w)
return false;
return true;
}