📄 railsystem.cpp
字号:
#include "RailSystem.h"
void RailSystem::reset(void) {
// TODO: reset the data objects of the
// City objects' contained in cities
}
RailSystem::RailSystem(string const &filename) {
load_services(filename);
}
void RailSystem::load_services(string const &filename) {
ifstream inf(filename.c_str());
string from, to;
int fee, distance;
while ( inf.good() ) {
// Read in the from city, to city, the fee, and distance.
inf >> from >> to >> fee >> distance;
if ( inf.good() ) {
// TODO: Add entries in the cities container and
// and services in the rail system for the new
// cities we read in.
}
}
inf.close();
}
RailSystem::~RailSystem(void) {
// TODO: release all the City* and Service*
// from cities and outgoing_services
}
void RailSystem::output_cheapest_route(const string& from,
const string& to, ostream& out) {
reset();
pair<int, int> totals = calc_route(from, to);
if (totals.first == INT_MAX) {
out << "There is no route from " << from << " to " << to << "\n";
} else {
out << "The cheapest route from " << from << " to " << to << "\n";
out << "costs " << totals.first << " euros and spans " << totals.second
<< " kilometers\n";
cout << recover_route(to) << "\n\n";
}
}
bool RailSystem::is_valid_city(const string& name) {
return cities.count(name) == 1;
}
pair<int, int> RailSystem::calc_route(string from, string to) {
priority_queue<City*, vector<City*>, Cheapest> candidates;
// TODO: Implement Dijkstra's shortest path algorithm to
// find the cheapest route between the cities
// Return the total fee and total distance.
// Return (INT_MAX, INT_MAX) if not path is found.
if (cities[to]->visited) {
return pair<int,int>(cities[to]->total_fee, cities[to]->total_distance);
} else {
return pair<int,int>(INT_MAX, INT_MAX);
}
}
string RailSystem::recover_route(const string& city) {
// TODO: walk backwards through the cities
// container to recover the route we found
return "";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -