void DisplayScreenTransferDestinationAddAction::_setFromParametersMap(const ParametersMap& map) { try { _screen = DisplayScreenTableSync::GetEditable(map.get<RegistryKeyType>(PARAMETER_DISPLAY_SCREEN_ID), *_env); _transferPlace = StopAreaTableSync::Get(map.get<RegistryKeyType>(PARAMETER_TRANSFER_PLACE_ID), *_env); } catch(ObjectNotFoundException<DisplayScreen>&) { throw ActionException("Display screen not found"); } catch(ObjectNotFoundException<StopArea>&) { throw ActionException("Transfer place not found"); } const string city(map.get<string>(PARAMETER_DESTINATION_PLACE_CITY_NAME)); GeographyModule::CityList cities(GeographyModule::GuessCity(city, 1)); if(cities.empty()) { throw ActionException("City not found"); } const string place(map.get<string>(PARAMETER_DESTINATION_PLACE_NAME)); vector<boost::shared_ptr<StopArea> > stops( cities.front()->search<StopArea>(place, 1) ); if(stops.empty()) { throw ActionException("Place not found"); } _destinationPlace = stops.front().get(); }
void HikingTrailStopAddAction::_setFromParametersMap(const ParametersMap& map) { try { _trail = HikingTrailTableSync::GetEditable(map.get<RegistryKeyType>(PARAMETER_TRAIL_ID), *_env); } catch(ObjectNotFoundException<HikingTrail>&) { throw ActionException("No such trail"); } const string city(map.get<string>(PARAMETER_CITY)); GeographyModule::CityList cities(GeographyModule::GuessCity(city, 1)); if(cities.empty()) { throw ActionException("City not found"); } const string place(map.get<string>(PARAMETER_NAME)); vector<boost::shared_ptr<StopArea> > stops( cities.front()->search<StopArea>(place, 1) ); if(stops.empty()) { throw ActionException("Place not found"); } _stop = StopAreaTableSync::GetEditable(stops.front()->getKey(), *_env); _rank = map.getOptional<size_t>(PARAMETER_RANK); if(_rank && *_rank > _trail->getStops().size()) { throw ActionException("Rank is too high"); } }
static vector<City> edgesToTour(const vector<Edge>& edges) { vector<City> cities(edges.size()); vector<Edge> es(edges); cities[0] = es[0][0]; cities[1] = es[0][1]; swap(es[0], es.back()); es.pop_back(); for (size_t i = 2; i < cities.size(); i++) { for (size_t e_i = 0; e_i < es.size(); e_i++) { bool found = false; const Edge &e = es[e_i]; if (e[0] == cities[i-1]) { cities[i] = e[1]; found = true; } if (e[1] == cities[i-1]) { cities[i] = e[0]; found = true; } if (found) { swap(es[e_i], es.back()); es.pop_back(); break; } } } return cities; }
std::vector<std::vector<_TReal> > GenerateCities(_TRandom &random, const std::vector<std::pair<_TReal, _TReal> > &boundary, const size_t nCities) { typedef std::vector<_TReal> _TPoint; std::vector<_TPoint> cities(nCities); for (size_t i = 0; i < cities.size(); ++i) cities[i] = GenerateCity(random, boundary); return cities; }
void DisplayScreenAddDisplayedPlaceAction::_setFromParametersMap(const ParametersMap& map) { try { _screen = DisplayScreenTableSync::GetEditable( map.get<RegistryKeyType>(PARAMETER_SCREEN_ID), *_env ); RegistryKeyType id(map.getDefault<RegistryKeyType>(PARAMETER_PLACE, 0)); if(id > 0) { _place = StopAreaTableSync::Get(id, *_env); } else { const string city(map.get<string>(PARAMETER_CITY_NAME)); GeographyModule::CityList cities(GeographyModule::GuessCity(city, 1)); if(cities.empty()) { throw ActionException("City not found"); } const string place(map.get<string>(PARAMETER_PLACE_NAME)); vector<boost::shared_ptr<StopArea> > stops( cities.front()->search<StopArea>(place, 1) ); if(stops.empty()) { throw ActionException("Place not found"); } _place = stops.front(); } } catch (ObjectNotFoundException<DisplayScreen>&) { throw ActionException("Display screen not found"); } }
int main() { int n; scanf("%d", &n); std::vector<pos> cities(n); for(int i = 0; i < n; i++) { int x, y; scanf("%d %d", &x, &y); cities[i] = pos(x, y); } double result = 0.f; for(auto it = cities.begin(); it < cities.end(); it++) { std::unordered_map<ratio, segment, ratiohash> h; h.reserve(300); for(auto it2 = cities.begin(); it2 < cities.end(); it2++) { if(it == it2) continue; pos c1 = *it, c2 = *it2; if(c2.x < c1.x) // c2 must always refers to the rightmost (or topmost) city std::swap(c1, c2); int dx = c2.x - c1.x; int dy = c2.y - c1.y; if(dx != 0) { // We map each slope to its (reduced) ratio int g = gcd(dx, dy); ratio slope = ratio(dx/g, dy/g); auto it = h.find(slope); if(it != h.end()) { int& np = it->second.mul; // Number of other cities along this line int& x1 = it->second.v1; int& x2 = it->second.v2; // We update the result, so remove the old one result -= calcLength(slope, x1, x2)/np; if(c2.x > x2) // Possibly expand the interval x2 = c2.x; else if(c1.x < x1) x1 = c1.x; np++; // We have to divide the result by the number of cities along this line since // we will be recalculating it each time the outer loop encounters such a city result += calcLength(slope, x1, x2)/double(np); } else { // This is the first other city found along this slope. If there turns out to // be only two cities along this line, we will be overestimating the result by // 2 since our outer loop will reach the other city, so preemptively divide by 2 h.insert(std::make_pair(slope, segment(c1.x, c2.x, 2))); result += calcLength(slope, c1.x, c2.x)/2.0f; } } else // dx = 0 - in this case the interval is not along the x axis but the y axis { // so we adjust accordingly auto it = h.find(pos(0, 1)); if(c2.y < c1.y) std::swap(c1, c2); if(it != h.end()) { int& np = it->second.mul; int& y1 = it->second.v1; int& y2 = it->second.v2; double dy = y2-y1; result -= dy/np; if(c2.y > y2) y2 = c2.y; else if(c1.y < y1) y1 = c1.y; np++; result += (y2-y1)/double(np); } else { h.insert(std::make_pair(pos(0, 1), segment(c1.y, c2.y, 2))); result += std::abs(dy)/2.f; } } } } std::printf("%.0lf\n", result); return 0; }