bool Config::loadSetting(const std::string &line, const std::string &name, PPMap &var) const { size_t i = isSetting(line, name, "="); if(i != std::string::npos) { size_t j = line.find(':', i); if(j == std::string::npos) { return false; } var.clear(); var.insert(PPMap::value_type( trim(line.substr(i, j - i)), trim(line.substr(j + 1)) )); return true; } i = isSetting(line, name, "+="); if(i != std::string::npos) { size_t j = line.find(':', i); if(j == std::string::npos) { return false; } var.insert(PPMap::value_type( trim(line.substr(i, j - i)), trim(line.substr(j + 1)) )); return true; } i = isSetting(line, name, "-="); if(i != std::string::npos) { size_t j = line.find(':', i); if(j == std::string::npos) { return false; } PPMap::iterator a = var.find(PPMap::value_type( trim(line.substr(i, j - i)), trim(line.substr(j + 1)) )); if(a != var.end()) { var.erase(a); } return true; } return false; }
int shortestDistance(int xMe, int yMe, int xHome, int yHome, vector <string> teleports) { #if 1 PVec points; points.push_back(P(xMe, yMe)); points.push_back(P(xHome, yHome)); PPMap warp; int i; for (i = 0; i < (int)teleports.size(); ++i) { istringstream s(teleports[i]); P p1, p2; s >> p1.first >> p1.second >> p2.first >> p2.second; points.push_back(p1); points.push_back(p2); warp[p1] = p2; warp[p2] = p1; } LLV Done(points.size()); PLLMap Costs; LL total = 0; int min_index = 0; while (min_index != 1) { Done[min_index] = 1; P Last = points[min_index]; LL min_cost = INF; for (i = 1; i < (int)points.size(); ++i) { if (Done[i]) { continue; } LL prev = Costs[points[i]]; LL d = Distance(Last, points[i]); PPMap::const_iterator it = warp.find(points[i]); if (it != warp.end()) { LL w = 10 + Distance(Last, it->second); d = min(d, w); } LL cost = total + d; if (!prev || cost < prev) { Costs[points[i]] = cost; } else { cost = prev; } if (cost < min_cost) { min_index = i; min_cost = cost; } } total = min_cost; } return (int)total; #else P Me(xMe, yMe); P Home(xHome, yHome); PVec pv1, pv2; LLV GoalCost; int i, j; for (i = 0; i < (int)teleports.size(); ++i) { int x1, y1, x2, y2; if (sscanf(teleports[i].c_str(), "%d %d %d %d", &x1, &y1, &x2, &y2) != 4) { return -1; } pv1.push_back(P(x1, y1)); pv2.push_back(P(x2, y2)); // teleport + walk to goal GoalCost.push_back(10 + Distance(*pv2.rbegin(), Home)); pv1.push_back(P(x2, y2)); pv2.push_back(P(x1, y1)); GoalCost.push_back(10 + Distance(*pv2.rbegin(), Home)); } bool f = true; while (f) { f = false; for (i = 0; i < (int)pv1.size(); ++i) { for (j = 0; j < (int)pv1.size(); ++j) { if (i == j) { continue; } // teleport + walk to anther spot + teleport LL cost = 10 + Distance(pv2[i], pv1[j]) + GoalCost[j]; if (cost < GoalCost[i]) { f = true; GoalCost[i] = cost; } } } } LL Min = Distance(Me, Home); for (i = 0; i < (int)pv1.size(); ++i) { Min = min(Min, Distance(Me, pv1[i]) + GoalCost[i]); } return (int)Min; #endif }