This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphCheapest.cpp
204 lines (165 loc) · 5.05 KB
/
GraphCheapest.cpp
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
// Lab 15c, Write A Function For Cheapest Route
// Programmer: Minos Park
// Editor(s) used: Sublime Text 2
// Compiler(s) used: G++
#include <algorithm>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <utility>
using namespace std;
#include <cstdlib>
struct Container
{
int vertex;
int prev;
double cost;
bool operator<(const Container& v) const {return cost > v.cost;}
};
struct Vertex
{
string name;
bool isVisited;
list<pair<int, double> > adjacentVertices;
int prev;
double cost;
};
pair<stack<int>, double> getCheapestRoute(int iStart, int iEnd, vector<Vertex>& database)
{
list<pair<int, double> >::iterator it;
for (int i = 0; i < database.size(); i++)
{
database[i].cost = 0;
database[i].prev = -1;
database[i].isVisited = false;
}
priority_queue<Container> toDoList;
Container startVertex;
startVertex.cost = 0;
startVertex.prev = -1;
startVertex.vertex = iStart;
toDoList.push(startVertex);
while (!toDoList.empty())
{
Container temp = toDoList.top();
toDoList.pop();
if (database[temp.vertex].isVisited == true) continue;
database[temp.vertex].isVisited = true;
database[temp.vertex].cost = temp.cost;
database[temp.vertex].prev = temp.prev;
if (temp.vertex == iEnd) break;
for (it = database[temp.vertex].adjacentVertices.begin(); it != database[temp.vertex].adjacentVertices.end(); ++it)
{
int thisNeighborsIndex = it->first;
double thisNeighborsCost = it->second;
Container neighborsContainer;
neighborsContainer.vertex = thisNeighborsIndex;
neighborsContainer.cost = (temp.cost + thisNeighborsCost);
neighborsContainer.prev = temp.vertex;
toDoList.push(neighborsContainer);
}
}
pair<stack<int>, double> result;
stack<int> resultInts;
int vertex = iEnd;
result.second = database[iEnd].cost;
while (vertex != -1)
{
resultInts.push(vertex);
vertex = database[vertex].prev;
}
result.first = resultInts;
return result;
}
int main()
{
cout << "Lab 15c, Write A Function For Cheapest Route\n";
cout << "Programmer: Minos Park\n";
cout << "Editor(s) used: Sublime Text 2\n";
cout << "Compiler(s) used: G++\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
ifstream fin;
fin.open("cities.txt");
if (!fin.good()) throw "I/O error";
// process the input file
vector<Vertex> database;
while (fin.good()) // EOF loop
{
string fromCity, toCity, cost;
// read one edge
getline(fin, fromCity);
getline(fin, toCity);
getline(fin, cost);
fin.ignore(1000, 10); // skip the separator
fromCity.erase(remove_if(fromCity.begin(), fromCity.end(), ::isspace), fromCity.end());
toCity.erase(remove_if(toCity.begin(), toCity.end(), ::isspace), toCity.end());
// add vertices for new cities included in the edge
int iToVertex = -1;
int iFromVertex = -1;
int i;
for (i = 0; i < database.size(); i++) // seek "to" city
{if (database[i].name == fromCity) break;} // end for()
if (i == database.size()) // not in database yet
{
// store the vertex if it is new
Vertex fromVertex;
fromVertex.name = fromCity;
database.push_back(fromVertex);
}
iFromVertex = i;
for (i = 0; i < database.size(); i++) // seek "from" city
{if (database[i].name == toCity) break;} // end for()
if (i == database.size()) // not in vector yet
{
// store the vertex if it is new
Vertex toVertex;
toVertex.name = toCity;
database.push_back(toVertex);
}
iToVertex = i;
// store bi-directional edges
double edgeCost = atof(cost.c_str());
database[iFromVertex].adjacentVertices.push_back(pair<int, double>(iToVertex, edgeCost));
database[iToVertex].adjacentVertices.push_back(pair<int, double>(iFromVertex, edgeCost));
}
fin.close();
cout << "Input file processed\n\n";
while (true)
{
string startCity;
string endCity;
int iStartCity = -1;
int iEndCity = -1;
cout << "\nEnter the source city [blank to exit]: ";
getline(cin, startCity);
if (startCity.length() == 0) break;
cout << "Enter the destination city [blank to exit]: ";
getline(cin, endCity);
if (endCity.length() == 0) break;
for (int i = 0; i < database.size(); i++)
{
if (startCity == database[i].name) iStartCity = i;
if (endCity == database[i].name) iEndCity = i;
}
if (iStartCity >= 0 && iEndCity >= 0)
{
pair<stack<int>, double> result = getCheapestRoute(iStartCity, iEndCity, database);
cout << "Total miles: " << result.second;
while (!result.first.empty())
{
cout << "-" << database[result.first.top()].name;
result.first.pop();
}
}
else
{
cout << "ERROR: One or more cities not found in database..." << endl;
}
cout << endl << endl;
}
}