template class Network { public: int find(const name& v) dist& getE(int m, int n) const dist& NoEdge() };
template class AdjMatrix { public: dist& getE(int m, int n) };
template class Link { public: dist& getE(int m, int n) { for (list::iterator iter = vertices[m].e->begin(); iter != vertices[m].e->end() && iter->vID < n; iter++); if (iter == vertices[m].e->end()) return NoEdge; if (iter->vID == n) return iter->cost; return NoEdge; } };
#include "Network.h" template class Weight { public: Weight(Network* G) : G(G), all(false), N(G->vNum()) { length = new dist*[N]; path = new int*[N]; shortest = new bool[N]; int i, j; for (i = 0; i < N; i++) { length[i] = new dist[N]; path[i] = new int[N]; }
for (i = 0; i < N; i++) { shortest[i] = false; for (j = 0; j < N; j++) { length[i][j] = G->getE(i, j); if (length[i][j] != G->NoEdge()) path[i][j] = i; else path[i][j] = -1; } } }
~Weight() { for (int i = 0; i < N; i++) delete []length; delete []path; delete []shortest; }
bool TopoSort() { initialize(); int i, top = -1; for (i = 0; i < N; i++) if (!count[i]) for (i = 0; i < N; i++) //TopoSort Start { if (top == -1) return false; result[i] = top; top = count[top]; for (iterator iter = begin(result[i]); iter != end(result[i]); iter++) if (!--count[iter->vID]) } return true; }
bool Cripath() { if (!TopoSort()) return false; int i; iterator iter; CA.clear(); dist* Ve = new dist[N]; dist* Vl = new dist[N];//Ve最早开始时间,Vl最迟开始时间 for (i = 0; i < N; i++) Ve[i] = 0;//Ve初始化
for (i = 0; i < N; i++)//按拓扑顺序计算Ve for (iter = begin(result[i]); iter != end(result[i]); iter++) if (Ve[result[i]]+iter->cost>Ve[iter->vID]) Ve[iter->vID]= Ve[result[i]] + iter->cost;
for (i = 0; i < N; i++) Vl[i] = Ve[N - 1];//Vl初始化
for (i = N - 2; i >= 0; i--)//按逆拓扑顺序计算Vl for (iter = begin(result[i]); iter != end(result[i]); iter++) if (Vl[iter->vID]-iter->cost < Vl[result[i]]) Vl[result[i]] = Vl[iter->vID] - iter->cost;
for (i = 0; i < N; i++)//计算各个活动的最早开始时间和最迟开始时间 for (iter = begin(i); iter != end(i); iter++) if (Ve[i] == Vl[iter->vID] - iter->cost) CA.push_back(CriAct(i, iter->vID));