#include #include #include #include #include #ifdef _DEBUG #include #include #endif using namespace std; struct stop { int x, y; }; struct bus { int l, r; }; struct fact { int a, b, c; }; struct input { vector stops; vector buses; vector facts; int n, m, t, f, d; void read_stops(std::istream& is) { stops.resize(n + 1); for (int i = 1; i <= n; i++) { auto& stop = stops[i]; is >> stop.x >> stop.y; } } int calc_dist(int i, int j) { return i == j ? 0 : abs(stops[i].x - stops[j].x) + abs(stops[i].y - stops[j].y); } void read_buses(std::istream& is) { buses.resize(m); for (int i = 0; i < m; i++) { auto& bus = buses[i]; is >> bus.l >> bus.r; } } void read_facts(std::istream& is) { facts.resize(f); for (int i = 0; i < f; i++) { auto& fact = facts[i]; is >> fact.a >> fact.b >> fact.c; } } void read(istream& is) { is >> n; read_stops(is); is >> m; read_buses(is); is >> t >> f; read_facts(is); sort(facts.begin(), facts.end(), [](const fact& l, const fact& r) { return l.a < r.a; }); is >> d; } }; istream& operator >> (istream& is, input& i) { i.read(is); return is; } struct course { vector stops; vector times; void write(ostream& os) const { int ss = stops.size(); os << ss; for (int i = 0; i < ss; i++) os << " " << stops[i]; os << endl; int ts = times.size(); os << ts; for (int i = 0; i < ts; i++) os << " " << times[i]; os << endl; } }; ostream& operator << (ostream& os, const course& c) { c.write(os); return os; } struct solution : vector { void write(ostream& os) const { for (auto& c : *this) os << c; } }; ostream& operator << (ostream& os, const solution& s) { s.write(os); return os; } bool solve(input& in, solution& sol) { sol.resize(in.m); vector stops(in.f); // get stops from facts transform(in.facts.begin(), in.facts.end(), stops.begin(), [](const fact& f) { return f.b; }); auto it = stops.begin(); // remove duplicates while (it != stops.end()) { if (find(stops.begin(), it, *it) != it) it = stops.erase(it); else it++; } // sort stops int cnt = stops.size(); for (int i = 0; i < cnt - 2; i++) { int min = in.calc_dist(stops[i], stops[i + 1]); int i1 = i + 1; for (int j = i + 2; j < cnt; j++) { int d = in.calc_dist(stops[i], stops[j]); if (d < min) { i1 = j; min = d; } } if (i1 != i + 1) { int s = stops[i + 1]; stops[i + 1] = stops[i1]; stops[i1] = s; } } // close the course stops.push_back(stops.front()); cnt++; // calculate delays vector delays(cnt); for (int i = 1; i < cnt; i++) delays[i] = delays[i - 1] + in.calc_dist(stops[i - 1], stops[i]); vector bd; bd.reserve(in.m); for (int i = 0; i < in.m; i++) bd.push_back(i); // order buses by durability sort(bd.begin(), bd.end(), [&in](int l, int r) { return in.buses[l].l > in.buses[r].l; }); int sib = 0, sie = 1; for (int i = 0; i < in.m; i++) { // try to find bus line that can cary all stops int b = bd[i]; int l = in.buses[b].l; int db = delays[sib]; // find while (sie < cnt && l > delays[sie] - db) sie++; { // offset start so that all stops are reached after people are there for (auto& f : in.facts) { int si = find(stops.begin(), stops.end(), f.b) - stops.begin(); if (si >= sib && si < sie) { int delay = f.a - delays[si]; if (delay > 0) for (auto& d : delays) d += delay; } } sol[b].stops.reserve(sie - sib); for (int k = sib; k < sie; k++) sol[b].stops.push_back(stops[k]); db = delays[sib]; sol[b].times.push_back(db); for (auto& d : delays) d -= db; if (sie - sib == 1) { int es = stops[sib]; int ns = stops[sib ? 0 : 1]; int min = in.calc_dist(es, ns); for (int i = 0; i < cnt; i++) { if (i != sib) { int d = in.calc_dist(es, stops[i]); if (d < min) { min = d; ns = stops[i]; } } } sol[b].stops.push_back(ns); sie++; } //sol[0].times.push_back(delays[delays.size() - 1] + in.buses[0].r); sib = sie - 1; if (sie == cnt) break; } } #ifdef _DEBUG ostringstream ss; ss << "Stops:"; for (int& s : stops) ss << s << ", "; ss << endl; ss << "Times:"; for (int& d : delays) ss << d << ", "; ss << endl; ss << "Delays:"; int prev = delays[0]; for (int& d : delays) ss << d - prev << ", "; ss << endl; ss << "Dist:"; for (int& d : delays) { ss << d - prev << ", "; prev = d; } ss << endl; OutputDebugStringA(ss.str().c_str()); #endif // _DEBUG return sie == cnt; } int main() { ifstream ifs("wonderland.in"); input i; ifs >> i; solution s; bool re = solve(i, s); ofstream ofs("wonderland.out"); ofs << s; }