最短路径(弗洛伊德算法)

试实现弗洛伊德最短路径算法。

一、函数接口定义:

void ShortestPath_Floyed(AMGraph G);

其中 G 是基于邻接矩阵存储表示的有向图。

二、裁判测试程序样例:

#include <iostream>
using namespace std;

#define MaxInt 32767
#define MVNum 100

typedef char VerTexType; 
typedef int ArcType;

int Path[MVNum][MVNum];    
int D[MVNum][MVNum];

typedef struct{ 
    VerTexType vexs[MVNum];
    ArcType arcs[MVNum][MVNum];
    int vexnum,arcnum; 
}AMGraph;

void CreateUDN(AMGraph &G);//实现细节隐藏
void ShortestPath_Floyed(AMGraph G);

void DisplayPath(AMGraph G , int begin ,int temp ){
    if(Path[begin][temp] != -1){
        DisplayPath(G , begin ,Path[begin][temp]);
        cout << G.vexs[Path[begin][temp]] << "->";
    }
}

int main(){
    AMGraph G;
    char start , destination;
    int num_start , num_destination;
    CreateUDN(G);
    ShortestPath_Floyed(G);
    cin >> start >> destination;
    num_start = LocateVex(G , start);
    num_destination = LocateVex(G , destination);
    DisplayPath(G , num_start , num_destination);
    cout << G.vexs[num_destination]<<endl;
    cout << D[num_start][num_destination];
    return 0;
}
/* 请在这里填写答案 */

三、输入样例:

第1行输入结点数vexnum和边数arcnum。第2行输入vexnum个字符表示结点的值,接下来依次输入arcnum行,每行输入3个值,前两个字符表示结点,后一个数表示两个结点之间边的权值。最后一行输入源点及终点。

6 8
012345
0 5 100
0 2 10
0 4 30
1 2 5
2 3 50
3 5 10
4 3 20
4 5 60
0 5

五、输出样例:

第一行输出源点到终点的最短路径,第二行输出源点到终点的最短路径距离。

0->4->3->5
60

#include <iostream>
#include <vector>
#include <limits>
using namespace std;

#define MaxInt numeric_limits<int>::max()
#define MVNum 100

typedef char VertexType;
typedef int ArcType;

int Path[MVNum][MVNum];
int D[MVNum][MVNum];

struct AMGraph {
    VertexType vexs[MVNum];
    ArcType arcs[MVNum][MVNum];
    int vexnum, arcnum;
};

void CreateUDN(AMGraph& G) {
    cout << "Enter the number of vertices (vexnum): ";
    cin >> G.vexnum;
    cout << "Enter the number of edges (arcnum): ";
    cin >> G.arcnum;

    cout << "Enter vertex value for each vertex: ";
    for (int i = 0; i < G.vexnum; i++) {
        cin >> G.vexs[i];
    }

    // Initialize the adjacency matrix
    for (int i = 0; i < G.vexnum; i++) {
        for (int j = 0; j < G.vexnum; j++) {
            G.arcs[i][j] = MaxInt;
        }
    }

    cout << "Enter arc value for each edge (start_vertex end_vertex arc_value): ";
    for (int k = 0; k < G.arcnum; k++) {
        char start, end;
        int weight;
        cin >> start >> end >> weight;

        int i = 0;
        while (G.vexs[i] != start) {
            i++;
        }
        int j = 0;
        while (G.vexs[j] != end) {
            j++;
        }

        G.arcs[i][j] = weight;
        G.arcs[j][i] = weight;
    }
}

void ShortestPath_Floyd(AMGraph& G) {
    int vexnum = G.vexnum;
    // Initialize D and Path
    for (int i = 0; i < vexnum; i++) {
        for (int j = 0; j < vexnum; j++) {
            D[i][j] = G.arcs[i][j];
            if (D[i][j] != MaxInt && i != j) {
                Path[i][j] = j;  // 修改为j,表示直接到达j
            } else {
                Path[i][j] = -1;
            }
        }
    }

    for (int k = 0; k < vexnum; k++) {
        for (int i = 0; i < vexnum; i++) {
            for (int j = 0; j < vexnum; j++) {
                if (D[i][k] != MaxInt && D[k][j] != MaxInt && D[i][k] + D[k][j] < D[i][j]) {
                    D[i][j] = D[i][k] + D[k][j];
                    Path[i][j] = Path[i][k];  // 修改为Path[i][k]
                }
            }
        }
    }
}

void DisplayPath(AMGraph G, int start, int end) {
    if (Path[start][end] == -1) {
        cout << "No path exists from " << G.vexs[start] << " to " << G.vexs[end] << endl;
        return;
    }

    cout << "Shortest path from " << G.vexs[start] << " to " << G.vexs[end] << ": ";
    cout << G.vexs[start];
    int i = start;
    while (i != end) {
        i = Path[i][end];
        cout << " -> " << G.vexs[i];
    }
    cout << endl;
}

int LocateVex(AMGraph G, char target) {
    for (int i = 0; i < G.vexnum; i++) {
        if (G.vexs[i] == target) {
            return i;
        }
    }
    return -1;
}

int main() {
    AMGraph G;
    CreateUDN(G);
    ShortestPath_Floyd(G);

    char source, destination;
    cout << "Enter the source and destination vertices: ";
    cin >> source >> destination;

    int sourceIndex = LocateVex(G, source);
    int destinationIndex = LocateVex(G, destination);

    if (sourceIndex == -1 || destinationIndex == -1) {
        cout << "Invalid source or destination vertex.\n";
    } else {
        DisplayPath(G, sourceIndex, destinationIndex);
        cout << "Shortest distance: " << D[sourceIndex][destinationIndex] << endl;
    }

    return 0;
}
posted @ 2023-06-20 17:05  YE-  阅读(107)  评论(0编辑  收藏  举报