使用Dijkstra算法输出问题的最短路径

时间:2019-05-14 21:33:51

标签: c++ graph tree shortest-path dijkstra


我的任务是创建一个程序,该程序将向所有其他节点输出最短路径。因此,例如在这张照片中,使用源A(https://www.includehelp.com/cpp-tutorial/Images/d0.jpg),输出将是(无序或有序的)就可以了:

A-B
A-C
A-B-G
A-B-G-E-D
A-B-G-E
A-B-G-E-F
..........

当前输出:

AC ED BG E FD 

*它显示了正确的部分最短路径,但我不知道如何以某种方式连接

到目前为止,我已经能够使用Dijkstra的算法正确计算从源到每个节点的最短路径成本。但是,我似乎无法弄清楚如何输出如上所示的“连接路径方向”。

代码:

#include<iostream>
#include<climits>    
#include<vector> 
#include<string> 

using namespace std;
int src = 0;
int vertex;

struct node {
    string source;
    vector<string> dest;
    vector<int> cost;
};

int minimumcost(vector<int> cost, vector<bool> Cset, vector<node> Node);
void dijkstra(vector<vector<int>> graph, vector<node> Node);
vector<vector<int>> graph(vector<node> Node);
string delimiter(string temp);
void Print(vector<vector<string>> listed);

int main()
{
    vector<node> Node;
    vector<string> _dest,_dest1;
    string root;
    string temp1;
    string temp2;
    int cost;
    int i = 0;
    int check = 0;

        for(int i=0;i<i+1;){
            cin>>temp1;
            if(temp1 == "root"){
                cin>>root;
                break;
            }
            temp1 = delimiter(temp1);
            for(int j=0; j<i; j++){
                if(temp1 == Node[j].source){
                        cin>>temp2;
                        temp2 = delimiter(temp2);
                        Node[j].dest.push_back(temp2);
                        _dest.push_back(temp2);
                        cin>>cost;
                        Node[j].cost.push_back(cost);
                        check = 1;
                        break;
                    }
                }
            if(check == 0){
                Node.push_back(node());
                Node[i].source = temp1;
                cin>>temp2;
                temp2 = delimiter(temp2);
                Node[i].dest.push_back(temp2);
                _dest.push_back(temp2);
                cin>>cost;
                Node[i].cost.push_back(cost);   
                i++;
                }
            check = 0;

        }


    sort(_dest.begin(), _dest.end() );
    _dest.erase( unique( _dest.begin(), _dest.end() ), _dest.end() );

    for(int i=0; i<_dest.size(); i++){
        for(int j=0; j<Node.size(); j++){
            if(_dest[i] == Node[j].source){
                _dest.erase(_dest.begin()+i);
                i--;
            }
        }
    }
    for(int i=0; i<_dest.size(); i++){
        Node.push_back(node());
        Node[Node.size()-1].source = _dest[i];
    }

    vertex = Node.size();
    cout<<'\n';

    dijkstra(graph(Node),Node);
    cout<<'\n';
    return 0;                           
}


vector<vector<int>> graph(vector<node> Node){
int V = Node.size();
vector<int> temp(V, 0);
vector<string> elements;
vector<vector<int>> graph(V, temp);
     cout<<"  ";
    for(int i=0; i<V;i++){
        elements.push_back(Node[i].source);
    }

    cout<<'\n';
    for(int i=0; i<V;i++){
        for(int j=0; j<V;j++){
            for(int k=0; k<Node[i].dest.size();k++){
                if(Node[i].dest[k] == elements[j])
                   graph[i][j] = Node[i].cost[k];
            }
        }

    }

return graph;
}

string delimiter(string temp){
    if(temp != "root")
            temp[temp.size()-1] = '\0';
    return temp;
}


int minimumcost(vector<int> cost, vector<bool> Cset, vector<node> Node)  {

    int min = INT_MAX;                 
    int index;

    for(int v=0;v<vertex;v++)
    {
        if(Cset[v]==false && cost[v]<=min)      
        {
            min=cost[v];
            index=v;

        }
    }

    return index;
}

void dijkstra(vector<vector<int>> graph, vector<node> Node){
    vector<int> Cost(vertex,0);                  
    vector<bool> Cset(vertex,0);  
    vector<string> elements;
    vector<string> temp(1," ");
    vector<vector<string>> listed(vertex, temp);

    for(int i=0; i<vertex;i++){
        elements.push_back(Node[i].source);
    }

    for(int i=0;i<vertex;i++)                    
    {
        Cost[i]=INT_MAX;
        Cset[i]=false;  
    }

    Cost[src]=0;   

    for(int i=0;i<vertex;i++){
        int u=minimumcost(Cost,Cset,Node);
        Cset[u]=true;                
        for(int j=0;j<vertex;j++){
            if(!Cset[j] && graph[u][j] && Cost[u]!=INT_MAX && Cost[u]+graph[u][j]<Cost[j]){
                Cost[j]=Cost[u]+graph[u][j];
                if(graph[u][j] < graph[u][j-1]){
                    listed[i].push_back(elements[i]);
                    listed[i][listed[i].size()-2] = elements[j];
                }
                else
                    listed[i].push_back(elements[j]);
            }
        }

    }


    cout<<"Vertex\t\tCost from source"<<endl;
    for(int i=0;i<vertex;i++)                       
    {
        cout<<elements[i]<<"\t\t"<<Cost[i]<<endl;
    }
    cout<<"\n";     
    Print(listed);
    cout<<"\n"; 
}

void Print(vector<vector<string>> listed){
vector<vector<string>> temp;
for(int i = 0; i<vertex; i++)
    reverse(listed[i].begin(),listed[i].end());


    for(int i=0;i<listed.size();i++)                       
    {
        for(int j=0;j<listed[i].size();j++)     
            cout<<listed[i][j];
    }

    for(int i=0;i<listed.size();i++){
        temp.push_back({{0}});
        for(int j=0;j<i;j++){
            for(int k=0;k<listed[j].size();k++){
                temp[i].push_back(listed[j][k]);
            }
        } 
    }

}


输入:

A, B, 5
A, C, 3
D, A, 2
B, C, 2
C, D, 7
B, G, 1
B, E, 3
C, E, 7
E, D, 2
D, F, 6
G, E, 1
E, F, 1
root A

请帮助。我被卡住了。谢谢。

0 个答案:

没有答案