#include<iostream>#include<vector>#include<set>#include<stack>#include<stdlib.h>using namespace std;const int MaxNumber=1e10;struct EdgeType //邊的類型{ int fromvertex; //邊的起點 int tovertex; //邊的終點 double weight; //邊上的權值};template<class vertexType>class DirectedGraphofMatrix //鄰接矩陣儲存的有向圖{ private: vector<vertexType>vertex; //頂點向量 int vertexnum; //頂點數目 vector<EdgeType>edge; //邊向量 int edgenum; //邊的數目 double *Distance; //Distance表示從起始頂點到各個目標頂點的權值 int *path; //path[i]表示第i個頂點在最短路徑中的父頂點 int *indegree; //頂點的入度 int sourcevertex; //源點 bool *Changed; //Changed[i]==false表示Distance[i]沒有被更新,否則,表示Distance[i]被更新 int getposofvertex(vertexType& v); //求出頂點v在圖中的位置 vertexType getvertex(int i); //求出頂點i代表的元素 double getWeight(int fromvertex,int tovertex); //擷取頂點fromvertex到tovertex之間的權值 void SetChanged(); //將Changed都設定為false bool CheckChanged(); //檢查Changed是不是都是false void UpdateEdge(EdgeType& e); //更新邊,更新Distance,path,Changed void printeachpath(int i); //列印從起始頂點到頂點i的一條最短路徑 public: DirectedGraphofMatrix(int n,int m); void TopSort(); //拓撲排序 void Initializationwork(vertexType& startvertex); //在求最短路徑前的初始化工作 void LoopAdjustEdge(); //迴圈調整邊 void showGraph() const; void printshortestpath(); //列印從起始頂點到其餘頂點的最短路徑 void showresult(); //顯示差限問題的解};bool checkInputFormat(istream& is) //檢查輸入格式是否正確{ if(!is.good()) { cerr<<"輸入格式錯誤!"<<endl; return false; } else return true;}template<class vertexType>DirectedGraphofMatrix<vertexType>::DirectedGraphofMatrix(int n=0,int m=0):vertexnum(n+1),edgenum(m+n){ //n表示有n個未知數,m表示有m個差限不等式 int i; vertexType v; EdgeType e; this->vertex.push_back(0); //添加頂點s,序號為0 for(i=1;i<=n;i++) this->vertex.push_back(i); // 將第1到n個未知數的序號輸入頂點 for(i=1;i<=m;i++) { cout<<"請輸入第"<<i<<"不等式減未知數的序號:"; cin>>e.fromvertex; cout<<" 請輸入第"<<i<<"不等式被減未知數的序號:"; cin>>e.tovertex; cout<<" 請輸入第"<<i<<"不等式被右邊的數值:"; cin>>e.weight; this->edge.push_back(e); } for(i=1;i<=n;i++) //將邊頂點s到頂點i的權值置為0 { e.fromvertex=0; e.tovertex=i; e.weight=0; this->edge.push_back(e); } Distance=new double[vertexnum]; path=new int[vertexnum]; this->indegree=new int[vertexnum]; this->Changed=new bool[vertexnum]; if(!Distance || !path || !indegree || !Changed) exit(0);}template<class vertexType>void DirectedGraphofMatrix<vertexType>::TopSort(){ stack<int>vertex_stack; //儲存入度為0的頂點序號 int n=0; //拓撲排序中得頂點數目 int i,j,k; for(i=0;i<this->vertexnum;i++) { if(this->indegree[i]==0) { vertex_stack.push(i); //將第i個頂點入棧 n++; } } while(vertex_stack.empty()==false) { i=vertex_stack.top(); vertex_stack.pop(); for(k=0;k<this->edgenum;k++) { if(this->edge[k].fromvertex==i) { j=this->edge[k].tovertex; this->indegree[j]--; if(this->indegree[j]==0) //第j個頂點入度為0 { vertex_stack.push(j); n++; } } } } if(n!=vertexnum) { cerr<<" 有向圖中存在環路!"<<endl; exit(0); }}template<class vertexType>void DirectedGraphofMatrix<vertexType>::UpdateEdge(EdgeType& e){ int i,j; i=e.fromvertex; j=e.tovertex; if(this->Distance[i]+this->getWeight(i,j)<this->Distance[j]) { this->Distance[j]=this->Distance[i]+this->getWeight(i,j); //更新Distance[j] this->path[j]=i; //更新path[j],i是j的最短路徑上的父節點 this->Changed[j]=true; //表明第j個頂點的Distance已經改變 }}template<class vertexType>void DirectedGraphofMatrix<vertexType>::LoopAdjustEdge(){ int i; do { for(i=0;i<edgenum;i++) //對所有邊進行調整 { this->UpdateEdge(this->edge[i]); } if(this->CheckChanged()==true) return; //一輪調整後Changed未發生改變,邊更新完畢 this->SetChanged(); //否則將Changed都置為false,進行下一輪調整 }while(true);}template<class vertexType>void DirectedGraphofMatrix<vertexType>::showGraph() const{ int i; for(i=0;i<this->vertexnum;i++) { cout<<" 第"<<i<<"個頂點是:"<<this->vertex[i]<<endl; } for(i=0;i<this->edgenum;i++) { cout<<" 第"<<i<<"條邊的起點是:"<<this->edge[i].fromvertex<<endl; cout<<" 第"<<i<<"條邊的終點是:"<<this->edge[i].tovertex<<endl; cout<<" 第"<<i<<"條邊的權值是:"<<this->edge[i].weight<<endl; }}template<class vertexType>int DirectedGraphofMatrix<vertexType>::getposofvertex(vertexType& v){ int i; for(i=0;i<this->vertexnum;i++) { if(v==this->vertex[i]) return i; } return -1;}template<class vertexType>void DirectedGraphofMatrix<vertexType>::SetChanged(){ int i; for(i=0;i<vertexnum;i++) this->Changed[i]=false;}template<class vertexType>bool DirectedGraphofMatrix<vertexType>::CheckChanged() //Changed元素都是false,函數返回true{ int i; for(i=0;i<vertexnum;i++) { if(this->Changed[i]==true) return false; } return true;} template<class vertexType>vertexType DirectedGraphofMatrix<vertexType>::getvertex(int i){ int j; for(j=0;j<vertexnum;j++) { if(i==j) return this->vertex[i]; }}template<class vertexType>double DirectedGraphofMatrix<vertexType>::getWeight(int fromvertex,int tovertex){ int i; for(i=0;i<edgenum;i++) { if(this->edge[i].fromvertex==fromvertex && this->edge[i].tovertex==tovertex) { return this->edge[i].weight; } } return MaxNumber;}template<class vertexType>void DirectedGraphofMatrix<vertexType>::Initializationwork(vertexType& startvertex) //在求最短路徑前的初始化工作{ int i; sourcevertex=this->getposofvertex(startvertex); //擷取源點序號 if(sourcevertex==-1) { cerr<<"輸入的源點不存在,程式終止!"<<endl; exit(0); } for(i=0;i<this->vertexnum;i++) { this->indegree[i]=0; //將所有頂點入度清零 this->Changed[i]=false; //初始時所有Changed[i]都為false; } for(i=0;i<this->edgenum;i++) { this->indegree[this->edge[i].tovertex]++; //統計所有頂點入度 } this->Distance[sourcevertex]=0; for(i=0;i<vertexnum;i++) { if(i!=sourcevertex) this->Distance[i]=MaxNumber; this->path[i]=-1; }}template<class vertexType>void DirectedGraphofMatrix<vertexType>::printeachpath(int i){ int parent; //parent是當前頂點的父頂點 double d=this->Distance[i]; //d是起始頂點到頂點i的最短路徑長度 stack<int>s; //棧s用於儲存最短路徑上的頂點 cout<<" 起始頂點到頂點"<<this->getvertex(i)<<"的最短路徑是:"; while((parent=this->path[i])!=-1) //頂點i有父頂點 { s.push(parent); i=parent; } while(!s.empty()) { cout<<this->getvertex(s.top())<<" "; s.pop(); } cout<<d<<endl;}template<class vertexType>void DirectedGraphofMatrix<vertexType>::printshortestpath(){ int i; for(i=0;i<vertexnum;i++) { this->printeachpath(i); }}template<class vertexType>void DirectedGraphofMatrix<vertexType>::showresult() //顯示差限問題的解{ int i; for(i=1;i<this->vertexnum;i++) { cout<<" 第"<<i<<"個未知數的值是:"<<this->Distance[i]<<endl; }}void main(){ int n,m; cout<<"請輸入未知數的個數:"; cin>>n; cout<<" 請輸入不等式的個數:"; cin>>m; DirectedGraphofMatrix<int>g(n,m); int s=0; g.Initializationwork(s); g.showGraph(); g.TopSort(); g.LoopAdjustEdge(); g.showresult();}