EdgeWeightedDigraph.h
#pragma once
#include <memory>
#include <fstream>
#include <stdexcept>
template <typename T>
class QSP;
template<typename T>
class EdgeWeightedDigraph
{
private:
class AdjacentcyList
{
public:
class DiEdge
{
public:
std::shared_ptr<DiEdge> next;
int v;
int w;
T weight;
public:
DiEdge(const int& s,const int& e,const T& t):v(s),w(e),weight(t),next(nullptr)
{
}
DiEdge() = default;
int from()
{
return v;
}
int to()
{
return w;
}
T Weight()
{
return weight;
}
};
std::shared_ptr<DiEdge> head;
class Iterator
{
private:
std::shared_ptr<DiEdge> it;
public:
Iterator(std::shared_ptr<DiEdge> i) :it(i)
{
}
bool operator == (const Iterator& rhs)const
{
return it == rhs.it;
}
bool operator != (const Iterator& rhs)const
{
return it != rhs.it;
}
Iterator operator ++()
{
it = it->next;
return *this;
}
DiEdge operator *()const
{
if (it == nullptr)
throw std::out_of_range("* nullptr error");
return *it;
}
};
Iterator begin()const
{
return Iterator(head);
}
Iterator end()const
{
return Iterator(nullptr);
}
AdjacentcyList():head(nullptr)
{
}
/*****************************************
函数名称: addDiEdge
******************************************/
void addDiEdge(const int& s,const int& e,const T& t)
{
if (head == nullptr)
{
head = std::make_shared<DiEdge>(DiEdge(s, e, t));
return;
}
std::shared_ptr<DiEdge> curr = head;
while (curr->next != nullptr)
curr = curr->next;
curr->next = std::make_shared<DiEdge>(DiEdge(s, e, t));
return;
}
};
private:
std::unique_ptr<AdjacentcyList[]> adj;
int nV;
int nE;
public:
EdgeWeightedDigraph(const std::string& file):nE(0)
{
std::ifstream in(file);
in >> nV;
adj = std::move(std::unique_ptr<AdjacentcyList[]>(new AdjacentcyList[nV]));
while (!in.eof())
{
int pre, curr;
T weight;
in >> pre >> curr >> weight;
adj[pre].addDiEdge(pre, curr, weight);
++nE;
}
}
friend class QSP<T>;
};
QSP.h
#pragma once
#include "priority_queue.h"
#include "EdgeWeightedDigraph.h"
#include <stack>
template <typename T>
class QSP
{
using Ed = typename EdgeWeightedDigraph<T>::AdjacentcyList::DiEdge;
private:
std::stack<int> resufdfs;
bool cycle;
std::unique_ptr<bool[]> marked;
std::unique_ptr<bool[]> stackOn;
std::unique_ptr<Ed*[]> edge;
std::unique_ptr<T[]> disTo;
int s;
int e;
private:
void iscycle(EdgeWeightedDigraph<T>* ewd,const int& i)
{
stackOn[i] = true;
marked[i] = true;
for (auto& ed : ewd->adj[i])
{
int w = ed.to();
if (stackOn[w])
cycle = true;
if (cycle)
return;
if (!marked[w])
iscycle(ewd, w);
}
stackOn[i] = false;
}
void iscycle(EdgeWeightedDigraph<T>* ewd)
{
for (int i = 0; i < ewd->nV; ++i)
if (!marked[i]) iscycle(ewd, i);
}
void getTop(EdgeWeightedDigraph<T>* ewd,const int& i)
{
marked[i] = true;
for (auto &ed : ewd->adj[i])
{
int w = ed.to();
if (!marked[w])
getTop(ewd, w);
}
resufdfs.push(i);
}
void getTop(EdgeWeightedDigraph<T>* ewd)
{
for (int i = 0; i < ewd->nV; ++i)
marked[i] = false;
for (int i = 0; i < ewd->nV; ++i)
if (!marked[i]) getTop(ewd, i);
}
void relax(EdgeWeightedDigraph<T>* ewd,const int& i)
{
for (auto& ed : ewd->adj[i])
{
int w = ed.to();
//求最长路径时将 > 改为 <
if (disTo[w] > disTo[i] + ed.weight)
{
disTo[w] = disTo[i] + ed.weight;
edge[w] = new Ed(i, w, ed.weight);
}
}
}
public:
QSP(EdgeWeightedDigraph<T>* ewd,const int& s,const int& e)
:marked(new bool[ewd->nV]),stackOn(new bool[ewd->nV]),cycle(false),s(s),e(e),disTo(new T[ewd->nV]),edge(new Ed*[ewd->nV])
{
for (int i = 0; i < ewd->nV; ++i)
{
marked[i] = false;
stackOn[i] = false;
}
iscycle(ewd);
if (cycle)
throw std::logic_error("can't handle cycle graph");
getTop(ewd);
for (int i = 0; i < ewd->nV; ++i)
{
edge[i] = nullptr;
//求最长路径时将 max() 改为 min()
disTo[i] = std::numeric_limits<T>::max();
}
disTo[s] = T{};
while (!resufdfs.empty())
{
int v = resufdfs.top();
resufdfs.pop();
relax(ewd, v);
}
}
T SPW()
{
return disTo[e];
}
void path()
{
stack<Ed> sk;
for (auto i = edge[e]; i != nullptr; i = edge[i->from()])
{
sk.push(*i);
if (i->from() == s)
break;
}
while (!sk.empty())
{
cout << sk.top().from() << ends << sk.top().to() << ends << sk.top().weight << endl;
sk.pop();
}
}
};
测试文件: Text.txt
8
5 4 0.35
4 7 0.37
5 7 0.28
5 1 0.32
4 0 0.38
0 2 0.26
3 7 0.39
1 3 0.29
7 2 0.34
6 2 0.40
3 6 0.52
6 0 0.58
6 4 0.93
main.cpp
#include <iostream>
#include "EdgeWeightedDigraph.h"
#include "QSP.h"
using namespace std;
int main()
{
EdgeWeightedDigraph<double> ewd("Text.txt");
try
{
QSP<double> qsp(&ewd, 5,6);
qsp.path();
cout << qsp.SPW();
}
catch (const exception& e)
{
cout << e.what();
}
system("pause");
return 0;
}
运行:
求最长路径,只需改动注释指出部分。。。。