图的最短路径(C++实现)

  1 #include <iostream>
  2 using namespace std ;
  3 
  4 #define MAXVERTEXNUM 100
  5 struct Graph
  6 {
  7   int VertexNum ;
  8   char Vertex[MAXVERTEXNUM] ;
  9   int AdjMatrix[MAXVERTEXNUM][MAXVERTEXNUM] ;
 10 } ;
 11 
 12 Graph MGraph ;
 13 
 14 
 15 #define INFINITY 100
 16 void CreateGraph (Graph *G)
 17 {
 18   int i , j ;
 19 
 20   cin >> G-> VertexNum ;
 21   for (i = 1 ; i <= G->VertexNum ; i++)
 22   {
 23     cin >> G->Vertex[i] ;
 24   }
 25   for (i = 1 ; i <= G->VertexNum ; i++)
 26   {
 27     for (j = 1 ; j <= G->VertexNum ; j++)
 28     {
 29       cin >> G->AdjMatrix[i][j] ;
 30       if (G->AdjMatrix[i][j] == -1)
 31       {
 32         G->AdjMatrix[i][j] = INFINITY ;
 33       }
 34     }
 35   }
 36 }
 37 
 38 
 39 void ShowGraph (Graph *G)
 40 {
 41   int i , j ;
 42 
 43   for (i = 1 ; i <= G->VertexNum ; i++)
 44   {
 45     cout << G->Vertex[i] << " " ;
 46   }
 47   cout << endl ;
 48 
 49   for (i = 1 ; i <= G->VertexNum ; i++)
 50   {
 51     for (j = 1 ; j <= G->VertexNum ; j++)
 52     {
 53       cout << G->AdjMatrix[i][j] << " " ;
 54     }
 55     cout << endl ;
 56   }
 57 }
 58 
 59 char Path[MAXVERTEXNUM][MAXVERTEXNUM] ;
 60 int Dest[MAXVERTEXNUM] ;
 61 void ShortestPath (Graph *G , char StartVexChar)
 62 {
 63   int i , j , m , StartVex , CurrentVex , MinDest ;
 64   int Final[MAXVERTEXNUM] ;
 65 
 66 
 67   for (i = 1 ; j <= G->VertexNum ; i++)
 68   {
 69     if (G->Vertex[i] == StartVexChar)
 70     {
 71       StartVex = i ;
 72       break ;
 73     }
 74   }
 75 
 76   for (i = 1 ; i <= G->VertexNum ; i++)
 77   {
 78     Path[i][0] = 0 ;
 79     Dest[i] = INFINITY ;
 80     if (G->AdjMatrix[StartVex][i] < INFINITY)
 81     {
 82       Dest[i] = G->AdjMatrix[StartVex][i] ;
 83       Path[i][1] = G->Vertex[StartVex] ;
 84       Path[i][2] = G->Vertex[i] ;
 85       Path[i][0] = 2 ;
 86     }
 87     Final[i] = 'F' ;
 88   }
 89   Dest[StartVex] = 0 ;
 90   Final[StartVex] = 'T' ;
 91   for (i = 1 ; i <= G->VertexNum ; i++)
 92   {
 93     MinDest = INFINITY ;
 94     for (j = 1 ; j <= G->VertexNum ; j++)
 95     {
 96       if (Final[j] == 'F')
 97       {
 98         if (Dest[j] < MinDest)
 99         {
100           CurrentVex = j ;
101           MinDest = Dest[j] ;
102         }
103       }
104     }
105     Final[CurrentVex] = 'T' ;
106     for (j = 1 ; j <= G->VertexNum ; j++)
107     {
108       if ((Final[j] == 'F') && (MinDest + G->AdjMatrix[CurrentVex][j] < Dest[j]))
109       {
110         Dest[j] = MinDest + G->AdjMatrix[CurrentVex][j] ;
111 
112         for (m = 0 ; m <= Path[CurrentVex][0] ; m++)
113         {
114           Path[j][m] = Path[CurrentVex][m] ;
115         }
116         Path[j][0]++ ;
117         Path[j][Path[j][0]] = G->Vertex[j] ;
118       }
119     }
120   }
121 }
122 
123 void ShowPath (Graph *G)
124 {
125   int i , j ;
126 
127   for (i = 1 ; i <= G->VertexNum ; i++)
128   {
129     cout << G->Vertex[i] << "(" << Dest[i] << ") : " ;
130     if (Path[i][0] > 0)
131     {
132       for (j = 1 ; j < G->VertexNum ; j++)
133       {
134         cout << " " << Path[i][j] ;
135       }
136     }
137     cout << endl ;
138   }
139 }
140 
141 int main ()
142 {
143   char StartVex ;
144 
145   CreateGraph (&MGraph) ;
146   ShowGraph (&MGraph) ;
147 
148   cin >> StartVex ;
149   ShortestPath (&MGraph , StartVex) ;
150   ShowPath (&MGraph) ;
151 
152   return 0 ;
153 
154 }

 

转载于:https://www.cnblogs.com/maoguy/p/6049299.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值