循环松弛
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cassert>
#include <vector>
#include <ctime>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
template<typename Weight>
class Edge {
private:
int a, b;
Weight weight;
public:
Edge(int a, int b, Weight weight) {
this->a = a;
this->b = b;
this->weight = weight;
}
Edge() {}
~Edge() {}
int v() { return a; }
int w() { return b; }
Weight wt() { return weight; }
int other(int x) {
assert(x == a || x == b);
return x == a ? b : a;
}
bool operator<(Edge<Weight>& e) {
return weight < e.wt();
}
bool operator<=(Edge<Weight>& e) {
return weight <= e.wt();
}
bool operator>(Edge<Weight>& e) {
return weight > e.wt();
}
bool operator>=(Edge<Weight>& e) {
return weight >= e.wt();
}
bool operator==(Edge<Weight>& e) {
return weight == e.wt();
}
friend ostream& operator<<(ostream &os, const Edge &e) {
os << e.a << "-" << e.b << ": " << e.weight;
return os;
}
};
template<typename Weight>
class SparseGraph {
private:
int n, m;
bool directed;
vector<vector<Edge<Weight> *>> g;
public:
SparseGraph(int n, bool directed) {
this->n = n;
this->m = 0;
this->directed = directed;
for (int i = 0; i < n; i++)
g.push_back(vector<Edge<Weight> *>());
}
~SparseGraph() {
for (int i = 0; i < n; i++)
for (int j = 0; j < g[i].size(); j++)
delete g[i][j];
}
int V() { return n; }
int E() { return m; }
void addEdge(int v, int w, Weight weight) {
assert(v >= 0 && v < n);
assert(w >= 0 && w < n);
g[v].push_back(new Edge<Weight>(v, w, weight));
if (v != w && !directed)
g[w].push_back(new Edge<Weight>(w, v, weight));
m++;
}
bool hasEdge(int v, int w) {
assert(v >= 0 && v < n);
assert(w >= 0 && w < n);
for (int i = 0; i < g[v].size(); i++)
if (g[v][i]->other(v) == w)
return true;
return false;
}
void show() {
for (int i = 0; i < n; i++) {
cout << "vertex " << i << ":\t";
for (int j = 0; j < g[i].size(); j++)
cout << "( to:" << g[i][j]->w() << ",wt:" << g[i][j]->wt() << ")\t";
cout << endl;
}
}
class adjIterator {
private:
SparseGraph &G;
int v;
int index;
public:
adjIterator(SparseGraph &graph, int v) : G(graph) {
this->v = v;
this->index = 0;
}
Edge<Weight>* begin() {
index = 0;
if (G.g[v].size())
return G.g[v][index];
return NULL;
}
Edge<Weight>* next() {
index += 1;
if (index < G.g[v].size())
return G.g[v][index];
return NULL;
}
bool end() {
return index >= G.g[v].size();
}
};
};
template <typename Graph, typename Weight>
class ReadGraph {
public:
ReadGraph(Graph &graph, const string &filename) {
ifstream file(filename);
string line;
int V, E;
assert(file.is_open());
assert(getline(file, line));
stringstream ss(line);
ss >> V >> E;
assert(graph.V() == V);
for (int i = 0; i < E; i++) {
assert(getline(file, line));
stringstream ss(line);
int a, b;
Weight w;
ss >> a >> b >> w;
assert(a >= 0 && a < V);
assert(b >= 0 && b < V);
graph.addEdge(a, b, w);
}
}
};
template <typename Graph, typename Weight>
class BellmanFord {
private:
Graph &G;
int s;
Weight* distTo;
vector<Edge<Weight>*> from;
bool hasNegativeCycle;
bool detectNegativeCycle() {
for (int i = 0; i < G.V(); i++) {
typename Graph::adjIterator adj(G, i);
for (Edge<Weight>* e = adj.begin(); !adj.end(); e = adj.next())
if (!from[e->w()] || distTo[e->v()] + e->wt() < distTo[e->w()])
return true;
}
return false;
}
public:
BellmanFord(Graph &graph, int s) :G(graph) {
this->s = s;
distTo = new Weight[G.V()];
for (int i = 0; i < G.V(); i++) {
from.push_back(NULL);
}
distTo[s] = Weight();
for (int pass = 1; pass < G.V(); pass++) {
for (int i = 0; i < G.V(); i++) {
typename Graph::adjIterator adj(G, i);
for (Edge<Weight>* e = adj.begin(); !adj.end(); e = adj.next())
if (!from[e->w()] || distTo[e->v()] + e->wt() < distTo[e->w()]) {
distTo[e->w()] = distTo[e->v()] + e->wt();
from[e->w()] = e;
}
}
}
hasNegativeCycle = detectNegativeCycle();
}
~BellmanFord() {
delete[] distTo;
}
bool negativeCycle() {
return hasNegativeCycle;
}
Weight shortestPathTo(int w) {
assert(w >= 0 && w < G.V());
assert(!hasNegativeCycle);
return distTo[w];
}
bool hasPathTo(int w) {
assert(w >= 0 && w < G.V());
return from[w] != NULL;
}
void shortestPath(int w, vector<Edge<Weight>> &vec) {
assert(w >= 0 && w < G.V());
assert(!hasNegativeCycle);
stack<Edge<Weight>*> s;
Edge<Weight> *e = from[w];
while (e->v() != this->s) {
s.push(e);
e = from[e->v()];
}
s.push(e);
while (!s.empty()) {
e = s.top();
vec.push_back(*e);
s.pop();
}
}
void showPath(int w) {
assert(w >= 0 && w < G.V());
assert(!hasNegativeCycle);
vector<Edge<Weight>> vec;
shortestPath(w, vec);
for (int i = 0; i < vec.size(); i++) {
cout << vec[i].v() << " -> ";
if (i == vec.size() - 1)
cout << vec[i].w() << endl;
}
}
};
int main(void)
{
string filename = "testG2.txt";
int V = 5;
SparseGraph<int> g = SparseGraph<int>(V, true);
ReadGraph<SparseGraph<int>, int> readGraph(g, filename);
cout << "Test Bellman-Ford:" << endl << endl;
BellmanFord<SparseGraph<int>, int> bellmanFord(g, 0);
if (bellmanFord.negativeCycle())
cout << "The graph contain negative cycle!" << endl;
else
for (int i = 1; i < V; i++) {
cout << "Shortest Path to " << i << " : " << bellmanFord.shortestPathTo(i) << endl;
bellmanFord.showPath(i);
cout << "----------" << endl;
}
system("pause");
return 0;
}