最短路径

Dijkstra与Floyd算法

Dijkstra 单源最短路

 

  1. #include<iostream> 
  2. #include<stdlib.h> 
  3. #include<string>
  4. using namespace std;
  5. #define OVERFLOW -1
  6. #define INFINITY 10000
  7. typedef char VertexType;
  8. typedef int* PIter; 
  9. typedef string* PathMatrix;
  10. typedef int* ShortPathTable;
  11. typedef struct{
  12.         VertexType *vexs;
  13.         int **arcs;
  14.         int vexnum,arcnum;}Graph;
  15.         
  16. int LocateVex(Graph G,VertexType v)
  17. {
  18.     for(int i=0;i<G.vexnum;i++)
  19.         if(G.vexs[i]==v) return i;
  20.     return G.vexnum;
  21. }
  22. void CreateGraph(Graph &G)
  23. {
  24.      cin>>G.vexnum>>G.arcnum;
  25.      G.vexs=new VertexType[G.vexnum]; 
  26.      G.arcs=new PIter[G.vexnum]; 
  27.      if(!G.vexs||!G.arcs)    exit(OVERFLOW); 
  28.      int i,j,k; 
  29.      for(i=0;i<G.vexnum;i++)
  30.      { 
  31.         if(!(G.arcs[i]=new int[G.vexnum]))  exit(OVERFLOW); 
  32.         for(j=0;j<G.vexnum;j++) 
  33.             if(i!=j) G.arcs[i][j]=INFINITY; 
  34.             else     G.arcs[i][j]=0;
  35.      } 
  36.      VertexType v1,v2; 
  37.         for(i=0;i<G.vexnum;i++) 
  38.                 cin>>G.vexs[i]; 
  39.         for(k=0;k<G.arcnum;k++)
  40.         { 
  41.                 do
  42.                       cin>>v1>>v2; 
  43.                       i=LocateVex(G,v1); 
  44.                       j=LocateVex(G,v2); 
  45.                       cin>>G.arcs[i][j]; 
  46.                       }while(i==G.vexnum||j==G.vexnum); 
  47.         } 
  48. void OutputPath(Graph G,int v0,PathMatrix P,int *cost)
  49. {
  50.     for(int i=0;i<G.vexnum;i++)
  51.     {
  52.         if(v0!=i)
  53.         {
  54.             cout<<G.vexs[v0]<<"->"<<G.vexs[i]<<":";
  55.             if(!P[i].empty())
  56.                 cout<<G.vexs[v0]<<P[i]<<" "<<cost[i]<<endl;
  57.             else cout<<"NO PATH!!"<<endl;
  58.         }
  59.     }
  60. }
  61. void ShortestPath_DIJ(Graph G,int v0,PathMatrix &P,ShortPathTable &D,int *cost)
  62. {
  63.      bool *final=new bool[G.vexnum];
  64.      D=new int[G.vexnum];
  65.      P=new string[G.vexnum];
  66.      cost=new int[G.vexnum];
  67.      if(!final||!D||!P||!cost) exit(OVERFLOW);
  68.      int v,w,i,min;
  69.      for(v=0;v<G.vexnum;v++)
  70.      {
  71.           final[v]=false,D[v]=G.arcs[v0][v];
  72.           if(D[v]<INFINITY) P[v]=G.vexs[v],cost[v]=D[v];
  73.           else cost[v]=INFINITY;
  74.      }
  75.      D[v0]=0,final[v0]=true;
  76.      for(i=1;i<G.vexnum;i++)
  77.      {
  78.            min=INFINITY;
  79.            for(w=0;w<G.vexnum;w++)
  80.               if(!final[w])
  81.                  if(D[w]<min) v=w,min=D[w];
  82.            final[v]=true;
  83.            for(w=0;w<G.vexnum;w++)
  84.               if(!final[w]&&(min+G.arcs[v][w]<D[w]))
  85.                 {
  86.                     D[w]=min+G.arcs[v][w];
  87.                     P[w]=P[v]+G.vexs[w];
  88.                     cost[w]=D[w];
  89.                 }
  90.       }
  91.          OutputPath(G,v0,P,cost); delete[] P;delete[] D;delete cost;
  92. }
  93.              
  94. int main()
  95. {
  96.     Graph G;
  97.     PathMatrix P;
  98.     ShortPathTable D;
  99.     int *cost;
  100.     CreateGraph(G);
  101.     ShortestPath_DIJ(G,0,P,D,cost);
  102.     system("pause"); 
  103.     return 0; 
  104. 输入
  105. 6 8
  106. a b c d e f
  107. a c 10
  108. a e 30
  109. a f 100
  110. b c 5
  111. c d 50
  112. d f 10
  113. e d 20
  114. e f 60
  115. 输出
  116. a->b:NO PATH!!
  117. a->c:ac 10
  118. a->d:aed 50
  119. a->e:ae 30
  120. a->f:aedf 60
  121. 请按任意键继续. . .

 

Floyd

 

  1. #include<iostream> 
  2. #include<stdlib.h> 
  3. #include<string>
  4. using namespace std;
  5. #define OVERFLOW -1
  6. #define INFINITY 10000
  7. typedef char VertexType;
  8. typedef int* PIter; 
  9. typedef string* PStr;
  10. typedef string** PathMatrix;
  11. typedef int** DistancMatrix;
  12. typedef struct{
  13.         VertexType *vexs;
  14.         int **arcs;
  15.         int vexnum,arcnum;}Graph;
  16.         
  17. int LocateVex(Graph G,VertexType v)
  18. {
  19.     for(int i=0;i<G.vexnum;i++)
  20.         if(G.vexs[i]==v) return i;
  21.     return G.vexnum;
  22. }
  23. void CreateGraph(Graph &G)
  24. {
  25.      cin>>G.vexnum>>G.arcnum;
  26.      G.vexs=new VertexType[G.vexnum]; 
  27.      G.arcs=new PIter[G.vexnum]; 
  28.      if(!G.vexs||!G.arcs)    exit(OVERFLOW); 
  29.      int i,j,k; 
  30.      for(i=0;i<G.vexnum;i++)
  31.      { 
  32.         if(!(G.arcs[i]=new int[G.vexnum]))  exit(OVERFLOW); 
  33.         for(j=0;j<G.vexnum;j++) 
  34.             if(i!=j) G.arcs[i][j]=INFINITY; 
  35.             else     G.arcs[i][j]=0;
  36.      } 
  37.      VertexType v1,v2; 
  38.         for(i=0;i<G.vexnum;i++) 
  39.                 cin>>G.vexs[i]; 
  40.         for(k=0;k<G.arcnum;k++)
  41.         { 
  42.                 do
  43.                       cin>>v1>>v2; 
  44.                       i=LocateVex(G,v1); 
  45.                       j=LocateVex(G,v2); 
  46.                       cin>>G.arcs[i][j]; 
  47.                       }while(i==G.vexnum||j==G.vexnum); 
  48.         } 
  49. void OutputPath(Graph G,PathMatrix P,DistancMatrix D)
  50. {
  51.     for(int i=0;i<G.vexnum;i++)
  52.             for(int j=0;j<G.vexnum;j++)
  53.             {
  54.                  if(j!=i)
  55.                  { 
  56.                       cout<<G.vexs[i]<<"->"<<G.vexs[j]<<":";
  57.                       if(!P[i][j].empty())
  58.                          cout<<G.vexs[i]<<P[i][j]<<" "<<D[i][j]<<endl;
  59.                       else cout<<"NO PATH!!"<<endl;
  60.                  }      
  61.             }
  62. }
  63. void ShortestPath_FLOYD(Graph G,PathMatrix &P,DistancMatrix &D)
  64. {
  65.          P=new PStr[G.vexnum];
  66.          D=new PIter[G.vexnum];
  67.          int v,w,u;
  68.          for(v=0;v<G.vexnum;v++)
  69.          {
  70.               P[v]=new string[G.vexnum];
  71.                 D[v]=new int[G.vexnum];
  72.                 for(w=0;w<G.vexnum;w++)
  73.                 {
  74.                      D[v][w]=G.arcs[v][w];
  75.                      if(D[v][w]<INFINITY) P[v][w]=G.vexs[w];
  76.                 }
  77.          }
  78.          for(u=0;u<G.vexnum;u++)
  79.             for(v=0;v<G.vexnum;v++)
  80.                  for(w=0;w<G.vexnum;w++)
  81.                         if(D[v][u]+D[u][w]<D[v][w])
  82.                         {
  83.                             D[v][w]=D[v][u]+D[u][w];
  84.                             P[v][w]=P[v][u]+P[u][w];
  85.                         }   
  86.             OutputPath(G,P,D);    delete[] P;delete[] D;
  87. }
  88. int main()
  89. {
  90.     Graph G;
  91.     CreateGraph(G);
  92.     PathMatrix P;
  93.     DistancMatrix D;
  94.     ShortestPath_FLOYD(G,P,D);
  95.     return 0;}
  96. 输入 
  97. 3 5
  98. a b c
  99. a b 4
  100. a c 11
  101. b a 6
  102. b c 2
  103. c a 3
  104. 输出 
  105. a->b:ab 4
  106. a->c:abc 6
  107. b->a:bca 5
  108. b->c:bc 2
  109. c->a:ca 3
  110. c->b:cab 7
  111. 请按任意键继续. . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值