Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I need to fix something very small in my C++ program. My program did not display the names of the airports from the minimum

Hi, I need to fix something very small in my C++ program. My program did not display the names of the airports from the minimum path it only displays "Total path: 920" and it should display "Min Path: SF LA LONDON PARIS 920." Please provide a screenshot and I will rate your answer thanks.

The program should display the path like this:

Enter Start Vertex: SF

Enter End Vertex : PARIS

Min Path: SF LA LONDON PARIS 920

Code:

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

void addEdge(vector > adj[], int u, int v, int wt)

{

adj[u].push_back(make_pair(v, wt));

adj[v].push_back(make_pair(u, wt));

}

// Print adjacency list representaion ot graph

void printGraph(vector > adj[], int V)

{

int v, w;

for (int u = 0; u < V; u++)

{

cout << u << " - ";

for (auto it = adj[u].begin(); it != adj[u].end(); it++)

{

v = it->first;

w = it->second;

cout << v << " ="

<< w << " ";

}

cout << " ";

}

}

int findMinDist(int dist[], bool visited[], int V)

{

int min = INT_MAX, minInd = 0;

for (int i = 0; i

{

if (visited[i] == false && dist[i] <= min)

{

min = dist[i];

minInd = i;

}

}

return minInd;

}

// This function mainly does BFS and prints the

// shortest path from src to dest. It is assumed

// that weight of every edge is 1

void findShortestPath(vector> adj[], int src, int dest, int V, int &total)

{

int *dist = new int[V];

bool *visited = new bool[V];

int *parent = new int[V];

for (int i = 0; i

{

dist[i] = INT_MAX;

visited[i] = false;

parent[i] = -1;

}

dist[src] = 0;

for (int k = 0; k

{

int ind = findMinDist(dist, visited, V);

visited[ind] = true;

for (int l = 0; l

{

parent[adj[ind][l].first] = ind;

if (!visited[adj[ind][l].first] && dist[ind] != INT_MAX && (dist[ind] + adj[ind][l].second)

dist[adj[ind][l].first] = dist[ind] + adj[ind][l].second;

}

}

total = dist[dest];

return;

}

// Driver code

int main()

{

vector> adj[6];

int V = 6;

string line;

ifstream myfile("file.txt");

vector vertex;

if (myfile.is_open())

{

while (getline(myfile, line))

{

// cout << line << ' ';

while (line != "-1")

{

char *token = strtok(const_cast(line.c_str()), " ");

token = strtok(NULL, " ");

vertex.push_back(token);

getline(myfile, line);

// cout << line << ' ';

}

V = vertex.size();

getline(myfile, line);

//cout << line << ' ';

while (line != "-1")

{

char *token = strtok(const_cast(line.c_str()), " ");

int u = atoi(token);

int v = atoi(strtok(NULL, " "));

int w = atoi(strtok(NULL, " "));

addEdge(adj, u, v, w);

getline(myfile, line);

// cout << line << ' ';

}

}

myfile.close();

}

else {

cout << "Unable to open file";

exit(1);

}

string start, end;

cout << "Enter Start Vertex : ";

cin >> start;

cout << "Enter End Vertex : ";

cin >> end;

int s, e;

int i = 0;

for (auto it = vertex.begin(); it != vertex.end(); it++)

{

if (*it == start)

s = i;

if (*it == end)

e = i;

i++;

}

// printGraph(adj, V);

int total = 0;

findShortestPath(adj, s, e, V, total);

cout << "Total path: " << total << endl;

//printGraph(adj, V);

system("pause");

return 0;

}

Text file required:

0 SF 1 LA 2 CHICAGO 3 NY 4 PARIS 5 LONDON -1 0 1 80 0 2 200 0 3 300 1 2 230 1 5 700 2 3 180 3 4 630 3 5 500 4 5 140 -1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

Define Administration and Management

Answered: 1 week ago

Question

Define organisational structure

Answered: 1 week ago

Question

Define line and staff authority

Answered: 1 week ago

Question

Define the process of communication

Answered: 1 week ago

Question

Explain the importance of effective communication

Answered: 1 week ago