The shortest path algorithm for solving Dijsktra

The shortest path algorithm for solving Dijsktra
 

description

 

Map includes an n cities, assuming m paths between the cities (directed graph), the length of each path is known. Given a map of the city starting point and end point of the city, use Dijsktra algorithm to find the shortest path between the start of the end.

 

Entry

A plurality of sets of data, each data line has m + 3. The first two acts of integers n and m, n represent the number of cities and the Number of path m. The second line has n characters, on behalf of the name of each city. The third row to the m + 2 lines each have two characters a and b and an integer d, representative of a city from the city b has a distance d of the path. The last two characters acts on behalf of the city start and end points to be seeking the shortest path. When n and m are both equal to zero, the input end.

Export

Each two data output lines. A first line integer between from start to finish for the shortest length. The second series of acts string representing the path. Separated by a space between each character.

Sample input 1 

3 3
A B C
A B 1
B C 1
A C 3
A C
6 8
A B C D E F
A F 100
A E 30
A C 10
B C 5
C D 50
E D 20
E F 60
D F 10
A F
0 0

Sample Output 1

2
A B C
60
A E D F
#include <the iostream> 
#include < String > 
#include <CString>
 #define MaxInt 32767
 #define MVNum 100 // maximum number of vertices 
/ * Note that there is a directed graph with adjacency matrix
 * / 
int D [MVNum]; // start point distances to the respective start point 
int S [MVNum]; // visited 
int the Path [MVNum];
 the using  namespace STD; 
typedef struct 
{ 
    char vexs [MVNum];
     int ARCS [MVNum] [MVNum];
     int vexnum, arcnum; 
AMGraph}; 
int the Locate (AMGraph & G,char s)//返回编号
{
    for (int i = 1; i <=G.vexnum; i++)
    {
        if (G.vexs[i] == s)
            return i;
    }
}
void Creat(AMGraph &G)
{
    for (int i = 1; i <= G.vexnum; i++)
    {
        cin >> G.vexs[i];
    }
    for(int i=0;i<=G.arcnum;i++)
        for (int j = 0; j <= G.arcnum; j++)
        {
            G.arcs[i][j] = MaxInt;
        }
    for (int i = 0; i < G.arcnum; i++)
    {
        char a, b;
        int c;
        cin >> a >> b >> c;
        int h, t;
        h = Locate(G, a);
        t = Locate(G, b);
        G.arcs[h][t] = c;//有向图
    }
}
void Dijkstra(AMGraph &G,int v0) 
{
    int n = G.vexnum;
    int v;
    for (v = 1; v <= n; v++)
    {
        S[v] = 0;
        D[v] = G.arcs[v0][v];
        if (D[v] <MaxInt)
            Path[v] = v0;
        else
            Path[v] = -1;
    }
    S[v0] = 1;
    D[v0] = 0;
    for (int i = 0; i < n - 1; i++)
    {
        int min =MaxInt;
        int v=1;
        for (int w = 1; w <= n; w++)
        {
            if (S[w] != 1 && D[w] <=min)
            {
                v = w;
                min = D[w];
            }
        }
        S[v] = 1;
        for (int w = 1; w <= n; w++)
        {
            if (S[w] != 1 && (D[v] + G.arcs[v][w] < D[w]))
            {
                D[w] = D[v] + G.arcs[v][w];
                Path[w] = v;
            }
        }
    }
}
void Print(AMGraph &G,int s,int e)
{
    int r[MVNum];//记录路径的编号
    r[0] = e;
    int i = 1;
    while (Path[r[i-1]]!=-1)
    { 
       r[i] = Path[r[i - 1]];
       i++;
    }
    for (int j = i - 1; j > 0; j--)
        cout << G.vexs[r[j]] << " ";
    cout << G.vexs[r[0]] << endl;
}
int main()
{
    int n, m;
    while (cin >> n >> m)
    {
        if (n == 0 && m == 0)
            break;
        AMGraph G;
        G.vexnum = n;
        G.arcnum = m;
        Creat(G);
        char start, end;
        cin >> start >> end;
        int s, e;
        s = Locate(G, start);
        e = Locate(G, end);
        Dijkstra(G, s);
        cout <<D[e]<<endl;
        Print(G,s,e);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/h694879357/p/11893977.html
Recommended