Incorrect/Partial Output - Stuck please Help!

Hello!
Please help. I'm stuck. Thank you so much :)
I was tasked to create a program that would output the shortest paths to all other nodes. So for example in this photo, with source A, ( https://www.includehelp.com/cpp-tutorial/Images/d0.jpg ) the output will be:

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


Current Output:

 
AC ED BG E FD 

*It shows the correct path but I just can't figure out how to somehow connect them.


So far, I have been able to compute the shortest path costs from the source to each node using Dijkstra's Algorithm. However, I can't seem to figure out how to output the "correct path directions" as seen above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#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]);
			}
		} 
	}

}


Input:
1
2
3
4
5
6
7
8
9
10
11
12
13
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
Last edited on
Registered users can post here. Sign in or register to post.