最短路径 Bellman-Ford算法(模板)
计算最短路径的基础算法,Bellman-Ford算法基础模板
const int inf = 0x3f3f3f3f;
const int maxn = 1050;
struct Edge {
int from, to, dist;
Edge(int f, int t, int d) :from(f), to(t), dist(d) {}
};
struct BellmanFord {
int n, m;
vector<Edge> edges;
vector<int> g[maxn];
bool inq[maxn]; //是否在队列中
int d[maxn]; //源点s到每个点的距离
int p[maxn]; //最短路中的上一条弧
int cnt[maxn]; //进队次数
void init(int n) {
this->n = n;
for (int i = 0; i < n; ++i) g[i].clear();
edges.clear();
}
void add(int from, int to, int dist) {
edges.push_back(Edge(from, to, dist));
m = edges.size();
g[from].push_back(m - 1);
}
bool bellman_ford(int s) { //s为源点
queue<int> que;
memset(inq, 0, sizeof(inq));
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; ++i) d[i] = inf;
d[s] = 0;
inq[s] = true;
que.push(s);
while (!que.empty()) {
int u = que.front();
que.pop();
inq[u] = false;
for (int i = 0; i < g[u].size(); ++i) {
Edge &e = edges[g[u][i]];
if (d[u] < inf && d[e.to] > d[u] + e.dist) {
d[e.to] = d[u] + e.dist;
p[e.to] = g[u][i];
if (!inq[e.to]) {
que.push(e.to);
inq[e.to] = true;
if (++cnt[e.to] > n) return false;
}
}
}
}
return true;
}
bool negativeCycle() { //判断图中有无负圈存在,有返回真
queue<int> que;
memset(inq, 0, sizeof(inq));
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; ++i) { d[i] = 0; inq[0] = true; que.push(i); }
while (!que.empty()) {
int u = que.front();
que.pop();
inq[u] = false;
for (int i = 0; i < g[u].size(); ++i) {
Edge &e = edges[g[u][i]];
if (d[e.to] > d[u] + e.dist) {
d[e.to] = d[u] + e.dist;
p[e.to] = g[u][i];
if (!inq[e.to]) {
que.push(e.to);
inq[e.to] = true;
if (++cnt[e.to] > n) return true;
}
}
}
}
return false;
}
};
测试代码
#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1050;
struct Edge {
int from, to, dist;
Edge(int f, int t, int d) :from(f), to(t), dist(d) {}
};
struct BellmanFord {
int n, m;
vector<Edge> edges;
vector<int> g[maxn];
bool inq[maxn]; //是否在队列中
int d[maxn]; //s到每个点的距离
int p[maxn]; //最短路中的上一条弧
int cnt[maxn]; //进队次数
void init(int n) {
this->n = n;
for (int i = 0; i < n; ++i) g[i].clear();
edges.clear();
}
void add(int from, int to, int dist) {
edges.push_back(Edge(from, to, dist));
m = edges.size();
g[from].push_back(m - 1);
}
bool bellman_ford(int s) { //s为源点
queue<int> que;
memset(inq, 0, sizeof(inq));
memset(cnt, 0, sizeof(cnt));
for (int i = 0; i < n; ++i) d[i] = inf;
d[s] = 0;
inq[s] = true;
que.push(s);
while (!que.empty()) {
int u = que.front();
que.pop();
inq[u] = false;
for (int i = 0; i < g[u].size(); ++i) {
Edge &e = edges[g[u][i]];
if (d[u] < inf && d[e.to] > d[u] + e.dist) {
d[e.to] = d[u] + e.dist;
p[e.to] = g[u][i];
if (!inq[e.to]) {
que.push(e.to);
inq[e.to] = true;
if (++cnt[e.to] > n) return false;
}
}
}
}
return true;
}
};
int n, m, s;
BellmanFord bf;
void print() {
for (int i = 0; i < n; ++i) {
if (i == s) continue;
stack<int> st;
Edge &e = bf.edges[bf.p[i]];
while (e.from != s) {
st.push(e.to);
e = bf.edges[bf.p[e.from]];
}
st.push(e.to);
cout << "到达顶点" << i << "的最短路径: " << s << " ";
while (!st.empty()) {
cout << "-->" << st.top();
st.pop();
}
cout << " 最短路径长度是:" << bf.d[i] << endl;
}
}
int main() {
while (scanf("%d%d%d", &n, &m, &s) == 3) {
bf.init(n);
for (int i = 0; i < m; ++i) {
int from, to, dist;
scanf("%d%d%d", &from, &to, &dist);
bf.add(from, to, dist);
}
if(bf.bellman_ford(s)) print();
else cout << "有负圈存在!" << endl;
}
return 0;
}
/*
5 7 0
0 1 100
0 2 30
0 4 10
2 1 60
2 3 60
3 1 10
4 3 50
4 6 0
0 1 20
0 2 5
3 0 -200
1 3 4
3 1 4
2 3 2
*/

浙公网安备 33010602011771号