码迷,mamicode.com
首页 > 编程语言 > 详细

C++ ssd5 27 optional exercise 7

时间:2016-04-11 20:36:01      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

#include "RailSystem.h"

void RailSystem::reset(void) {

// TODO: reset the data objects of the
// City objects‘ contained in cities
map<string, City*>::iterator iter;
for (iter = cities.begin(); iter != cities.end(); iter++)
{
iter->second->visited = false;
iter->second->from_city = "";
iter->second->total_fee = 32767;
iter->second->total_distance = 32767;
}

}

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.
City* city_from = new City(from);
City* city_to = new City(to);
Service* edge = new Service(to, fee, distance);

cities.insert(pair<string, City*>(from, city_from));//插入city,insert方法决定了如果读入重复的名字插入会不成功,即不会建立重复的City
cities.insert(pair<string, City*>(to, city_to));

if (outgoing_services.find(from) == outgoing_services.end())//如果这是新的城市
{
list<Service*> adjacent_city;
adjacent_city.push_front(edge);
outgoing_services.insert(pair<string, list<Service*>>(from, adjacent_city));//建立邻接链表
}
else //如果再此之前就已经存在这个城市的邻接链表
{
outgoing_services.find(from)->second.push_front(edge);
}


}
}

inf.close();

}

RailSystem::~RailSystem(void) {

// TODO: release all the City* and Service*
// from cities and outgoing_services
map<string, City*>::iterator iter;
for (iter = cities.begin(); iter != cities.end(); iter++)
{
delete iter->second;
}
map<string, list<Service*>>::iterator it;
for (it = outgoing_services.begin(); it != outgoing_services.end(); it++)
{
delete it->second.front();
it->second.pop_front();
}

cities.clear();
outgoing_services.clear();

}

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;
cities[from]->total_fee = 0;//这里必须将初始点的费用和距离置零
cities[from]->total_distance = 0;
candidates.push(cities[from]);
list<Service*>::iterator ser_iter;

while (!candidates.empty())
{

City* top = candidates.top();
if (top->name == to)//如果找到了目标城市
{
break;
}

else
{
candidates.pop();//出队
//遍历目前城市的邻接表
for (ser_iter = outgoing_services.find(top->name)->second.begin(); ser_iter != outgoing_services.find(top->name)->second.end(); ser_iter++)
{



if ((*ser_iter)->fee + top->total_fee < cities[(*ser_iter)->destination]->total_fee)
{
cities[(*ser_iter)->destination]->total_fee = (*ser_iter)->fee + top->total_fee;
cities[(*ser_iter)->destination]->total_distance = (*ser_iter)->distance + top->total_distance;
cities[(*ser_iter)->destination]->from_city = top->name;
}

if (cities[(*ser_iter)->destination]->visited == false)
{
cities[(*ser_iter)->destination]->visited = true;
candidates.push(cities[(*ser_iter)->destination]);//入队之前必须先修改费用和距离以及是否访问过,如果在之后,则队列中将只会保留改之前的数据

}

}
}
}

 

 


// 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
string route_str = city;
string m_city = city;

while (m_city != "")
{
route_str = cities[m_city]->from_city +" to" + route_str;
m_city = cities[m_city]->from_city;
}

return route_str;
}
#pragma once

C++ ssd5 27 optional exercise 7

标签:

原文地址:http://www.cnblogs.com/bigminnow/p/5379690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!