拓扑排序关键路径

 输入数据,输出拓扑排序,关键路径(如果有多组解,分情况输出),一直都是尽量不用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

 

  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<list>
  4. using namespace std;
  5. #define OVERFLOW -2
  6. #define OK 1 
  7. #define ERROR 0
  8. #define N 100
  9. typedef int InfoType;
  10. typedef char VertexType;
  11. typedef int Status;
  12. typedef struct ArcNode{
  13.         int adjvex;
  14.         ArcNode *nextarc;
  15.         InfoType info;}ArcNode;
  16. typedef struct{
  17.         VertexType data;
  18.         ArcNode *firstarc;}VNode,*AdjList;
  19. typedef struct{
  20.         AdjList vertices;
  21.         int vexnum,arcnum;}Graph;
  22. int LocateGraph(Graph G,VertexType v) 
  23.     for(int i=0;i<G.vexnum;i++) 
  24.             if(G.vertices[i].data==v) return i; 
  25.     return G.vexnum;
  26. }
  27.     
  28.     
  29. Status CreateGraph(Graph &G)
  30. {
  31.        cin>>G.vexnum>>G.arcnum;
  32.        if(!(G.vertices=new VNode[G.vexnum])) exit(OVERFLOW);
  33.        int i,j,k;
  34.        for(i=0;i<G.vexnum;i++)
  35.        {
  36.               cin>>G.vertices[i].data;
  37.               G.vertices[i].firstarc=NULL;
  38.        }
  39.        VertexType v1,v2;
  40.        int value;
  41.        ArcNode *p,*q;
  42.        for(k=0;k<G.arcnum;k++)
  43.        {
  44.               do{
  45.                  cin>>v1>>v2>>value;
  46.                  i=LocateGraph(G,v1);
  47.                  j=LocateGraph(G,v2);
  48.                  }while(i==G.vexnum||j==G.vexnum);
  49.               if(!(p=new ArcNode)) exit(OVERFLOW);
  50.               p->adjvex=j; p->info=value;p->nextarc=G.vertices[i].firstarc;
  51.               G.vertices[i].firstarc=p;
  52.        } 
  53.        return OK; 
  54. }
  55. void FindInDegree(Graph G,int *indegree)
  56. {
  57.         for(int i=0;i<G.vexnum;i++)
  58.                 for(ArcNode *p=G.vertices[i].firstarc;p;p=p->nextarc)
  59.                             ++indegree[p->adjvex];
  60. }
  61. Status TopologicalOrder(Graph G,int *&T,int *&ve,int *&indegree_0)
  62. {
  63.        int *indegree=new int[G.vexnum],i,k=0,top=-1;
  64.        ve=new int[G.vexnum];
  65.        T=new int[G.vexnum];
  66.        if(!ve||!T||!indegree) exit(OVERFLOW);
  67.        for(i=0;i<G.vexnum;i++)
  68.              indegree[i]=ve[i]=T[i]=0,indegree_0[i]=-1;   
  69.        FindInDegree(G,indegree);
  70.        for(i=0;i<G.vexnum;i++)
  71.        {      if(!indegree[i]) 
  72.               {
  73.                   indegree[i]=top,top=i;
  74.                   indegree_0[k++]=i;
  75.               }
  76.        }          
  77.        for(k=i=0;i<G.vexnum;i++)
  78.        {
  79.              if(top==-1) return ERROR;
  80.              int cur=top;
  81.              T[k++]=cur;
  82.              cout<<G.vertices[cur].data<<" ";
  83.              top=indegree[top];
  84.              for(ArcNode *p=G.vertices[cur].firstarc;p;p=p->nextarc)
  85.              {           if(!--indegree[p->adjvex]) indegree[p->adjvex]=top,top=p->adjvex;
  86.                          if(ve[cur]+p->info>ve[p->adjvex]) ve[p->adjvex]=ve[cur]+p->info;             
  87.              }
  88.       
  89.        }
  90.        cout<<endl;
  91.        return OK;
  92. }
  93. list<int> vlist[N];
  94. void Output(Graph G,int a)
  95. {
  96.      static int res[N],idx=0,n=1;
  97.      if(vlist[a].empty())
  98.      {
  99.             if(idx)
  100.             {
  101.                    cout<<"Case #"<<n<<":"<<endl;
  102.                    for (int i = 0; i < idx; ++i) 
  103.                        cout<<G.vertices[res[i]].data<<" ";
  104.                    cout<<G.vertices[a].data<<endl<<endl;
  105.                    n++;
  106.             }
  107.      }
  108.     res[idx++]=a; 
  109.     for(list<int>::const_iterator iter = vlist[a].begin();iter != vlist[a].end(); ++iter) 
  110.         Output(G,*iter); 
  111.     --idx; 
  112. Status CriticalPath(Graph G){
  113.        int i,j,*T,*ve,*indegree_0,*vl=new int[G.vexnum];
  114.        indegree_0=new int[G.vexnum];
  115.        if(!vl||!indegree_0) exit(OVERFLOW);
  116.        if(!(TopologicalOrder(G,T,ve,indegree_0))) return ERROR;
  117.        for(i=0;i<G.vexnum;i++)    
  118.               vl[i]=INT_MAX;
  119.        i=T[G.vexnum-1];
  120.        vl[i]=ve[i];
  121.        for(j=G.vexnum-1;j>=0;j--)
  122.        {      
  123.              i=T[j];
  124.              for(ArcNode *p=G.vertices[i].firstarc;p;p=p->nextarc)
  125.                          if(vl[p->adjvex]-p->info<vl[i]) vl[i]=vl[p->adjvex]-p->info;
  126.        }
  127.        for(j=0;j<G.vexnum;j++)
  128.               for(ArcNode *p=G.vertices[j].firstarc;p;p=p->nextarc)
  129.                    if(ve[j]==vl[p->adjvex]-p->info)
  130.                           vlist[j].push_back(p->adjvex);         
  131.        for(j=0;indegree_0[j]!=-1;j++)
  132.             Output(G,indegree_0[j]);
  133.        
  134. int main(){
  135.     Graph G;
  136.     CreateGraph(G);
  137.     CriticalPath(G);
  138.     return 0;}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值