图的最短路径(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 }

 

posted @ 2016-11-09 23:28  maoguy  阅读(5288)  评论(0编辑  收藏  举报