输入数据,输出拓扑排序,关键路径(如果有多组解,分情况输出),一直都是尽量不用STL的,不过有写时候不用确实很麻烦。。
输入
9 11
a b c d e f g h i
a b 6
a c 4
a d 5
b e 1
c e 1
d f 2
e g 9
e h 7
f h 4
g i 2
h i 4
输出
a b c e g d f h i
Case #1:
a b e h i
Case #2:
a b e g i
- #include<iostream>
- #include<stdlib.h>
- #include<list>
- using namespace std;
- #define OVERFLOW -2
- #define OK 1
- #define ERROR 0
- #define N 100
- typedef int InfoType;
- typedef char VertexType;
- typedef int Status;
- typedef struct ArcNode{
- int adjvex;
- ArcNode *nextarc;
- InfoType info;}ArcNode;
- typedef struct{
- VertexType data;
- ArcNode *firstarc;}VNode,*AdjList;
- typedef struct{
- AdjList vertices;
- int vexnum,arcnum;}Graph;
- int LocateGraph(Graph G,VertexType v)
- {
- for(int i=0;i<G.vexnum;i++)
- if(G.vertices[i].data==v) return i;
- return G.vexnum;
- }
- Status CreateGraph(Graph &G)
- {
- cin>>G.vexnum>>G.arcnum;
- if(!(G.vertices=new VNode[G.vexnum])) exit(OVERFLOW);
- int i,j,k;
- for(i=0;i<G.vexnum;i++)
- {
- cin>>G.vertices[i].data;
- G.vertices[i].firstarc=NULL;
- }
- VertexType v1,v2;
- int value;
- ArcNode *p,*q;
- for(k=0;k<G.arcnum;k++)
- {
- do{
- cin>>v1>>v2>>value;
- i=LocateGraph(G,v1);
- j=LocateGraph(G,v2);
- }while(i==G.vexnum||j==G.vexnum);
- if(!(p=new ArcNode)) exit(OVERFLOW);
- p->adjvex=j; p->info=value;p->nextarc=G.vertices[i].firstarc;
- G.vertices[i].firstarc=p;
- }
- return OK;
- }
- void FindInDegree(Graph G,int *indegree)
- {
- for(int i=0;i<G.vexnum;i++)
- for(ArcNode *p=G.vertices[i].firstarc;p;p=p->nextarc)
- ++indegree[p->adjvex];
- }
- Status TopologicalOrder(Graph G,int *&T,int *&ve,int *&indegree_0)
- {
- int *indegree=new int[G.vexnum],i,k=0,top=-1;
- ve=new int[G.vexnum];
- T=new int[G.vexnum];
- if(!ve||!T||!indegree) exit(OVERFLOW);
- for(i=0;i<G.vexnum;i++)
- indegree[i]=ve[i]=T[i]=0,indegree_0[i]=-1;
- FindInDegree(G,indegree);
- for(i=0;i<G.vexnum;i++)
- { if(!indegree[i])
- {
- indegree[i]=top,top=i;
- indegree_0[k++]=i;
- }
- }
- for(k=i=0;i<G.vexnum;i++)
- {
- if(top==-1) return ERROR;
- int cur=top;
- T[k++]=cur;
- cout<<G.vertices[cur].data<<" ";
- top=indegree[top];
- for(ArcNode *p=G.vertices[cur].firstarc;p;p=p->nextarc)
- { if(!--indegree[p->adjvex]) indegree[p->adjvex]=top,top=p->adjvex;
- if(ve[cur]+p->info>ve[p->adjvex]) ve[p->adjvex]=ve[cur]+p->info;
- }
- }
- cout<<endl;
- return OK;
- }
- list<int> vlist[N];
- void Output(Graph G,int a)
- {
- static int res[N],idx=0,n=1;
- if(vlist[a].empty())
- {
- if(idx)
- {
- cout<<"Case #"<<n<<":"<<endl;
- for (int i = 0; i < idx; ++i)
- cout<<G.vertices[res[i]].data<<" ";
- cout<<G.vertices[a].data<<endl<<endl;
- n++;
- }
- }
- res[idx++]=a;
- for(list<int>::const_iterator iter = vlist[a].begin();iter != vlist[a].end(); ++iter)
- Output(G,*iter);
- --idx;
- }
- Status CriticalPath(Graph G){
- int i,j,*T,*ve,*indegree_0,*vl=new int[G.vexnum];
- indegree_0=new int[G.vexnum];
- if(!vl||!indegree_0) exit(OVERFLOW);
- if(!(TopologicalOrder(G,T,ve,indegree_0))) return ERROR;
- for(i=0;i<G.vexnum;i++)
- vl[i]=INT_MAX;
- i=T[G.vexnum-1];
- vl[i]=ve[i];
- for(j=G.vexnum-1;j>=0;j--)
- {
- i=T[j];
- for(ArcNode *p=G.vertices[i].firstarc;p;p=p->nextarc)
- if(vl[p->adjvex]-p->info<vl[i]) vl[i]=vl[p->adjvex]-p->info;
- }
- for(j=0;j<G.vexnum;j++)
- for(ArcNode *p=G.vertices[j].firstarc;p;p=p->nextarc)
- if(ve[j]==vl[p->adjvex]-p->info)
- vlist[j].push_back(p->adjvex);
- for(j=0;indegree_0[j]!=-1;j++)
- Output(G,indegree_0[j]);
- }
- int main(){
- Graph G;
- CreateGraph(G);
- CriticalPath(G);
- return 0;}