https://blog.csdn.net/INGNIGHT/article/details/130035536
算法学习(10):LeetCode刷题之Dijkstra最短路径算法_leetcode最短路径_name_s_Jimmy的博客-CSDN博客
//
// Created by liuyubobobo on 9/28/16.
//
#ifndef INC_03_IMPLEMENTATION_OF_DIJKSTRA_DIJKSTRA_H
#define INC_03_IMPLEMENTATION_OF_DIJKSTRA_DIJKSTRA_H
#include <iostream>
#include <vector>
#include <stack>
#include "Edge.h"
#include "IndexMinHeap.h"
using namespace std;
template<typename Graph, typename Weight>
class Dijkstra{
private:
Graph &G;
int s;
Weight *distTo;
bool *marked;
vector<Edge<Weight>*> from;
public:
Dijkstra(Graph &graph, int s):G(graph){
this->s = s;
distTo = new Weight[G.V()];
marked = new bool[G.V()];
for( int i = 0 ; i < G.V() ; i ++ ){
distTo[i] = Weight();
marked[i] = false;
from.push_back(NULL);
}
IndexMinHeap<Weight> ipq(G.V());
// start dijkstra
distTo[s] = Weight();
ipq.insert(s, distTo[s] );
marked[s] = true;
while( !ipq.isEmpty() ){
int v = ipq.extractMinIndex();
// distTo[v]就是s到v的最短距离
marked[v] = true;
//cout<<v<<endl;
typename Graph::adjIterator adj(G, v);
for( Edge<Weight>* e = adj.begin() ; !adj.end() ; e = adj.next() ){
int w = e->other(v);
if( !marked[w] ){
if( from[w] == NULL || distTo[v] + e->wt() < distTo[w] ){
distTo[w] = distTo[v] + e->wt();
from[w] = e;
if( ipq.contain(w) )
ipq.change(w, distTo[w] );
else
ipq.insert(w, distTo[w] );
}
}
}
}
}
~Dijkstra(){
delete[] distTo;
delete[] marked;
}
Weight shortestPathTo( int w ){
assert( w >= 0 && w < G.V() );
return distTo[w];
}
bool hasPathTo( int w ){
assert( w >= 0 && w < G.V() );
return marked[w];
}
void shortestPath( int w, vector<Edge<Weight>> &vec ){
assert( w >= 0 && w < G.V() );
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() );
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;
}
}
};
#endif //INC_03_IMPLEMENTATION_OF_DIJKSTRA_DIJKSTRA_H
//
// Created by liuyubobobo on 9/28/16.
//
#ifndef INC_05_IMPLEMENTATION_OF_BELLMAN_FORD_BELLMANFORD_H
#define INC_05_IMPLEMENTATION_OF_BELLMAN_FORD_BELLMANFORD_H
#include <stack>
#include <vector>
#include "Edge.h"
using namespace std;
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);
}
// Bellman-Ford
distTo[s] = Weight();
for( int pass = 1 ; pass < G.V() ; pass ++ ){
// Relaxation
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;
}
}
};
#endif //INC_05_IMPLEMENTATION_OF_BELLMAN_FORD_BELLMANFORD_H