码迷,mamicode.com
首页 > 编程语言 > 详细

佛洛依德 c++ 最短路径算法

时间:2016-07-06 18:05:00      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

  1 //20142880 唐炳辉 石家庄铁道大学
  2 #include<iostream>
  3 #include<string>
  4 using namespace std;
  5 #define Maxnum 32767
  6 #define N 100
  7 
  8 
  9 
 10 typedef struct
 11 {
 12     string Dianarr[N];                    
 13     string info[N];                    
 14     int arcs[N][N];                
 15     int DianNum,BianNum;
 16 }FloydStruct;
 17 
 18 int LocateVex(FloydStruct G, string a)            
 19 {
 20     int i;
 21     bool flag = 0;
 22     for(i = 0;i < G.DianNum;i++)
 23         if(a == G.Dianarr[i])
 24         {    
 25             flag = 1;
 26             return i;
 27         }
 28     if(flag == 0)
 29     {
 30         return -1;
 31     }
 32 }
 33 void CreateUDN(FloydStruct &G)
 34 {
 35     cout << "请输入顶点数和边数:" << endl;
 36     cin >> G.DianNum >> G.BianNum;
 37     cout << "请输入顶点信息:"  << endl;
 38     for(int i = 0;i<G.DianNum;i++)            
 39         cin >> G.Dianarr[i];
 40     for(int i = 0;i<G.DianNum;i++)
 41         for(int j = 0;j<G.DianNum;j++)
 42             G.arcs[i][j] = Maxnum;
 43     string v1,v2;                                
 44     int w,i,j;
 45     cout << "请输入存在路径的两个顶点和路径长度:" << endl;
 46     for(int k = 0;k<G.BianNum;k++)
 47     {
 48         cin >> v1 >> v2 >> w;
 49         i = LocateVex(G,v1);//获取点的数组下标
 50         j = LocateVex(G,v2);
 51         G.arcs[i][j] = w;//权值赋值给两点
 52     }
 53 }
 54 
 55 
 56 int Path[N][N];
 57 int D[N][N];
 58 void Floyd(FloydStruct G)    //佛洛依德核心算法                    
 59 {
 60     for(int i = 0;i < G.DianNum;i++)
 61         for(int j  = 0; j < G.DianNum;j++)
 62         {
 63             D[i][j] = G.arcs[i][j];
 64             if(D[i][j]<Maxnum)
 65                 Path[i][j] = i;
 66             else 
 67                 Path[i][j] = -1;
 68         }
 69     for(int k = 0;k < G.DianNum;k++)/**核心**/
 70         for(int i  = 0; i < G.DianNum;i++)
 71             for(int j  = 0; j < G.DianNum;j++)
 72                 if(D[i][k] + D[k][j] < D[i][j])
 73                 {
 74                     D[i][j] = D[i][k] + D[k][j];
 75                     Path[i][j] = Path[k][j];
 76                 }
 77 }
 78 
 79 void printFl(FloydStruct G)            //输出                            
 80 {
 81     /*string a,b;
 82     int i,j,font = 1;
 83     cout << "请输入当前位置和要去位置:" << endl;
 84     cin >> a >> b;
 85     i = LocateVex(G,a);
 86     j = LocateVex(G,b);
 87     if(D[i][j]==Maxnum)
 88          cout << "最短距离为:" << "不可达!"<< endl;
 89     else
 90         cout << "最短距离为:" << D[i][j]<< endl;*/
 91     for(int i=0;i<G.DianNum;i++)
 92     {
 93         for(int j=i+1;j<G.DianNum;j++)
 94         {
 95             if(D[i][j]!=Maxnum)
 96             {
 97                 cout<<G.Dianarr[i]<<"------>"<<G.Dianarr[j]<<"的最短路径为"<< D[i][j]<< endl;
 98             }
 99         }
100     
101     }
102     
103 }
104 
105 void main()
106 {
107     FloydStruct G;
108     CreateUDN(G);//生成邻接矩阵
109     Floyd(G);
110     printFl(G);
111 }

 

佛洛依德 c++ 最短路径算法

标签:

原文地址:http://www.cnblogs.com/sanzangtdashi/p/5647426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!