• 【图】有向图中两点间的路径


    Graph_Path.h

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    
    #define MAXINT 32767
    #define MVNUM 100
    
    typedefstruct{
             char Vexs[MVNUM];
             int Arcs[MVNUM][MVNUM];
             int vexnum;
             int arcnum;
    }AMGraph;
    
    staticint visited[MVNUM];
    staticchar path[MVNUM];
    staticint k = 1;
    
    voidCreateGraph(AMGraph &G);
    intLocateVex(AMGraph G, char v1);
    voidPrint(AMGraph G);
    voidDFS(AMGraph G, char v);
    voidPath(AMGraph G, char v1, char v2);

     Graph_Path.cpp

    #include "Graph_Path.h"
    
    voidCreateGraph(AMGraph &G){
        int i, j, k;
        char v1, v2;
        int w;
        cout << "请输入顶点个数和弧的个数: ";
        cin >> G.vexnum >> G.arcnum;
        cout << "请输入顶点信息:" << endl;
        for (i = 0; i < G.vexnum; i++)   
        {
            cin >> G.Vexs[i];   
        }
        for (i = 0; i < G.vexnum; i++)    
        {
            for (j = 0; j < G.vexnum; j++)           
            {
                G.Arcs[i][j] = MAXINT;
            }            
        }
        cout << "请输入每条弧的端点和权值" << endl;
        for (k = 0; k < G.arcnum; k++)    
        {                    
            cin >> v1 >> v2 >> w;
            i = LocateVex(G, v1);
            j = LocateVex(G, v2);
            G.Arcs[i][j] = w;
        }
    }
    
    intLocateVex(AMGraph G, char v1)
    {
        int i;
        for (i = 0; i < G.vexnum; i++)    
        {
            if (v1 == G.Vexs[i]) return i;            
        }
        return 0;
    }
    
    voidPrint(AMGraph G)
    {
        int i, j, k = 0;
        for (i = 0; i < G.vexnum; i++)
            for (j = 0; j < G.vexnum; j++)        
                if (G.Arcs[i][j] == MAXINT)
                    cout << "∞"<< " ";
                else
                    cout<< G.Arcs[i][j] << " ";
                k++;
            if (k % G.vexnum == 0)
            cout << endl;               
            
    }
    
    voidPath(AMGraph G, char v1, char v2)
    { 
        int m, n;
        int i, j;
        int flag = 0;
        m = LocateVex(G, v1);
        n = LocateVex(G, v2);
        for (i = 0; i < G.vexnum; i++)    
        {
            if (G.Arcs[m][i] != MAXINT)
                flag = 1;
        }
        if (flag == 0)
            cout << "无路径可以到达!" << endl;
        if (m == n){
            cout << "起点和终点都是自身!" << endl;
        }else{
            for (i = 0; i < G.vexnum; i++){
                if (G.Arcs[m][i] != MAXINT){
                    if (i == n){
                        for (j = 1; j < k; j++){
                             cout << path[j] << "";
                        }
                        cout << v2 << endl;】
                    }else{
                        path[k] = G.Vexs[i];
                        k++;
                        Path(G, G.Vexs[i], v2);
                        k--;                      
                    }                             
                }                   
            }
        }
            
    }
    

    Graph_PathAppTest.cpp

    #include "Graph_Path.h"
    
    void main()
    {
        int flag;
        char v1, v2;
        AMGraph G;
        cout << "    寻找路径" << endl;
        cout << "1.创建一个图" << endl;
        cout << "2.寻找图中两点间的路径" << endl;
        cout << endl;
        while (flag)    
        {
            cout << "输入菜单选项: ";
            cin >> flag;
            switch (flag)
            {
                case 1 :
                    CreateGraph(G);
                    cout << endl;
                    cout << "该有向图是:" << endl;
                    Print(G);
                    cout << endl;
                    break;
                case 2 :
                    cout << "输入起点和终点:";
                    cin >> v1 >> v2;
                    path[0] = v1;
                    cout << "由" << v1 << "开始:";
                    Path(G, v1, v2);
                    cout << endl;
                    break;
                default :
                    cout << "输入有误!" << endl;
                    break; 
            }
            if (flag == 0)  break;            
        }
    }
  • 相关阅读:
    解决Linux下Qt程序报『QString::arg: Argument missing: 无法解析SSLv2_client_method中的符号』错误
    中介者模式之我们结婚吧
    POJ 2141 Message Decowding(map)
    Cocos2dx之Box2D具体解释 设置物体回复力
    Android之Window
    opencv源代码分析:cvCreateMTStumpClassifier最优弱分类器的代码框架
    LeetCode总结,二分法一般性总结
    Change Number to English By Reading rule of money
    在多台PC上进行ROS通讯-学习笔记
    编程之美初赛第二场 奇妙的数列
  • 原文地址:https://www.cnblogs.com/tanghm/p/12863267.html
Copyright © 2020-2023  润新知