#include #include #include #include bool LOG=false; using namespace std; class edge; class vertex { friend ostream &operator<<(ostream&,const vertex *); friend ostream &operator<<(ostream&,const edge *); friend class graph; friend class edge; public: vertex(int n,edge* arc = NULL):val(n),firstEdge(arc){isVisited=false;} ~vertex(){if(LOG)cout<<"V"< vs; vector es; bool weighted; bool directed; public: graph():vNum(0),eNum(0),weighted(false),directed(false){} graph(int ,int,bool,bool); ~graph(); void getData(); void display(); int minPath(int , int ); void reVisitVertex(){for (int i=0;iisVisited=false ) ;} void reVisitEdge(){for (int i=0;iisVisited=false ) ;} }; graph::graph(int n,int m,bool weighted,bool directed)\ :vNum(n),eNum(m),weighted(weighted),directed(directed) { cin.ignore(1); for (int i=0;i>a >>b; --a,--b; if(weighted)cin>>w; edge *arc=new edge (vs[a],vs[b],w,vs[a]->firstEdge); vs[a]->firstEdge = arc; es.push_back(arc); } } ostream& operator<<(ostream& os,const vertex* v) { os<<"V"<val+1<<" -->"; edge *arc= v->firstEdge; while(arc){ os<<" V"<in->val+1; arc=arc->nextEdge; } return os; } ostream& operator<<(ostream& os,const edge* e) { os<<"V"<out->val+1<<"--"<weight<<"-->"<in->val+1; return os; } graph::~graph() { for (int i=0;ifirstEdge; while(arc){ edge *p=arc; arc=arc->nextEdge; delete p; } delete vs[i]; } } void graph::display() { cout<<"-----VERTEXS-----"< last; // can't initialize with n NULL ptr for (int i=0;i distnace(vNum,-1); distnace[p->val] = 0; list que; que.push_back(p); while(!que.empty()){ vertex * cur = que.front(); que.pop_front(); cur->isVisited = true; edge *arc = cur->firstEdge; while(arc){ vertex * tmp=arc->in; if(! tmp->isVisited){ que.push_back(tmp); int sum = arc->weight+distnace[arc->out->val]; if(distnace[tmp->val]==-1){ distnace[tmp->val]= sum; last[tmp->val] = arc->out; } else if(distnace[tmp->val]>sum){ distnace[tmp->val] = sum; last[tmp->val] = arc->out; } } arc = arc->nextEdge; } } cout<<"path V"<val]!=p ){ cout<<"V"<val+1<<"<--"; cur = last[cur->val]; } reVisitVertex(); if(! cur) { cout.clear(); cout<<"there isn't path from V"<>n>>m; graph g=graph(n,m,weighted,directed); g.display(); return 0; }