C++ ssd5 27 optional exercise 7

#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

时间: 2024-12-29 07:24:58

C++ ssd5 27 optional exercise 7的相关文章

C++ ssd5 25 optional exercise 6

// parts.h #ifndef _PARTS_H_#define _PARTS_H_ #include <vector>#include <map>#include <string>#include <iostream> using namespace std; //**************** Part ****************class Part { private: static int count; public: string n

C++ ssd5 16 optional exercise 3

#ifndef GRID_H#define GRID_H #include <string>#include <vector> using namespace std; /** IMPORTANT NOTE:** For this assignment, you might need to add state to the* class and/or argment existing methods, and/or create private helper* methods, b

C++ ssd5 12 optional exercise2

#pragma once#ifndef FIFO_H#define FIFO_H#include"simulator.h" class fifo : public simulator{public: fifo(int second_per_page); virtual void simulate(string file); //virtual void addevent(event e); private: //queue<event> wait;}; #endif #in

Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 2)及总结

Exercise 1:Linear Regression---实现一个线性回归 关于如何实现一个线性回归,请参考:http://www.cnblogs.com/hapjin/p/6079012.html Exercise 2:Logistic Regression---实现一个逻辑回归 问题描述:用逻辑回归根据学生的考试成绩来判断该学生是否可以入学. 这里的训练数据(training instance)是学生的两次考试成绩,以及TA是否能够入学的决定(y=0表示成绩不合格,不予录取:y=1表示录

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression

斯坦福大学机器学习公开课:Programming Exercise 2: Logistic Regression---Matlab实现 1 Logistic Regression In this part of the exercise, I will build a logistic regression model to predict whether a student gets admitted into a university. You want to determine each

coursera 机器学习 logistic regression 逻辑回归的项目

github : https://github.com/twomeng/logistic-regression- ex1. m 1 %% Machine Learning Online Class - Exercise 2: Logistic Regression 2 % 3 % Instructions 4 % ------------ 5 % 6 % This file contains code that helps you get started on the logistic 7 %

matlab(8) Regularized logistic regression : 不同的λ(0,1,10,100)值对regularization的影响,对应不同的decision boundary\ 预测新的值和计算模型的精度predict.m

不同的λ(0,1,10,100)值对regularization的影响\ 预测新的值和计算模型的精度 %% ============= Part 2: Regularization and Accuracies =============% Optional Exercise:% In this part, you will get to try different values of lambda and % see how regularization affects the decisio

【机器学习】Octave 实现逻辑回归 Logistic Regression

34.62365962451697,78.0246928153624,0 30.28671076822607,43.89499752400101,0 35.84740876993872,72.90219802708364,0 60.18259938620976,86.30855209546826,1 79.0327360507101,75.3443764369103,1 45.08327747668339,56.3163717815305,0 61.10666453684766,96.51142

caffe源码阅读4-layer.hpp

An interface for the units of computation which can be composed into a Net. Layer&s must implement a Forward function, in which they take their input (bottom) Blob&s (if any) and compute their output Blob&s (if any). They may also implement a