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
/
GraphShortest.cpp.txt
115 lines (99 loc) · 3.06 KB
/
GraphShortest.cpp.txt
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
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <utility>
using namespace std;
#include <cstdlib>
struct Vertex
{
string name;
bool isVisited;
list<pair<int, double> > adjacentVertices;
int prev;
double cost;
};
pair<stack<int>, double> getShortestRoute(int iStart, int iEnd, vector<Vertex>& database)
{
pair<stack<int>, double> result;
list<pair<int, double> >::iterator it; // to iterate over adjacentVertices
// TO DO -- write this function
return result;
}
int main()
{
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
// TO DO -- code to eliminate whitespace
// add vertices for new cities included in the edge
int iToVertex = -1, iFromVertex = -1, i;
for (i = 0; i < database.size(); i++) // seek "to" city
if (database[i].name == fromCity)
break;
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;
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 fromCity, toCity;
cout << "\nEnter the source city [blank to exit]: ";
getline(cin, fromCity);
if (fromCity.length() == 0) break;
// find the from city
int iFrom;
for (iFrom = 0; iFrom < database.size(); iFrom++)
if (database[iFrom].name == fromCity)
break;
cout << "Enter the destination city [blank to exit]: ";
getline(cin, toCity);
if (toCity.length() == 0) break;
// find the destination city
int iTo;
for (iTo = 0; iTo < database.size(); iTo++)
if (database[iTo].name == toCity)
break;
// TO DO -- skip the next code block if no matching cites are found
cout << "Route";
pair<stack<int>, double> result = getShortestRoute(iFrom, iTo, database);
while (!result.first.empty()){cout << '-' << database[result.first.top()].name; result.first.pop();}
cout << "Total edges: " << result.second;
cout << endl;
}
}