#include #include #include #include #include #include #include #include using namespace std; const int TIME_MIN = 6 * 60; const int TIME_MAX = 21 * 60; const int TARGET_START = 11 * 60; const int TARGET_END = 21 * 60; const int TARGET_STEP = 30; struct City { int id; long long x, y, w; }; struct Flight { int from, to, s, t; }; inline int calc_dur(const City& a, const City& b) { double d = sqrt(pow(a.x - b.x, 2) + pow(a.y - b.y, 2)); return (int)ceil((60.0 * d / 800.0 + 40.0) / 5.0) * 5; } class Solver { int N, M, K; double R; vector cities; vector get_latest_departure_all(const vector& all_flights, int dst, int target) { vector best(N, -1e9); best[dst] = target; for (const auto& f : all_flights) { if (f.t <= best[f.to] && f.s > best[f.from]) { best[f.from] = f.s; } } return best; } long long compute_score_fast(int h_idx, const vector& spokes) { vector temp_flights; for (int k = 0; k < K; ++k) { int v_idx = spokes[k % spokes.size()]; int d = calc_dur(cities[h_idx], cities[v_idx]); int cur_t = TIME_MIN; int cur_loc = h_idx + 1; while (cur_t + d <= TIME_MAX) { int dest = (cur_loc == h_idx + 1) ? v_idx + 1 : h_idx + 1; temp_flights.push_back({cur_loc - 1, dest - 1, cur_t, cur_t + d}); cur_t += d; cur_loc = dest; } } sort(temp_flights.begin(), temp_flights.end(), [](const Flight& a, const Flight& b){ return a.s > b.s; }); long long v_ci = 0; for (int dst = 0; dst < N; ++dst) { for (int target = TARGET_START; target <= TARGET_END; target += TARGET_STEP) { vector ci_dep = get_latest_departure_all(temp_flights, dst, target); for (int src = 0; src < N; ++src) { if (src == dst) continue; double dx = cities[src].x - cities[dst].x; double dy = cities[src].y - cities[dst].y; if (sqrt(dx*dx + dy*dy) < 0.25 * R) continue; if (ci_dep[src] > TIME_MIN) { v_ci += cities[src].w * cities[dst].w; } } } } return v_ci; } public: void solve() { if (!(cin >> N >> R)) return; cities.resize(N); for (int i = 0; i < N; ++i) cin >> cities[i].x >> cities[i].y >> cities[i].w; cin >> M; for (int i = 0; i < M; ++i) { int a, b, sh, sm, th, tm; char c; cin >> a >> sh >> c >> sm >> b >> th >> c >> tm; } cin >> K; int best_hub_idx = 0; long long max_score = -1; vector> pop_rank; for(int i=0; i spokes; for(auto& p : pop_rank) { if(p.second == h_idx) continue; double dx = cities[h_idx].x - cities[p.second].x; double dy = cities[h_idx].y - cities[p.second].y; if(sqrt(dx*dx + dy*dy) >= 0.25 * R) spokes.push_back(p.second); } if(spokes.empty()) continue; long long score = compute_score_fast(h_idx, spokes); if (score > max_score) { max_score = score; best_hub_idx = h_idx; } } int u = best_hub_idx + 1; vector final_spokes; for(auto& p : pop_rank) { if(p.second == best_hub_idx) continue; double dx = cities[best_hub_idx].x - cities[p.second].x; double dy = cities[best_hub_idx].y - cities[p.second].y; if(sqrt(dx*dx + dy*dy) >= 0.25 * R) final_spokes.push_back(p.second + 1); } vector> schedule(K); for (int k = 0; k < K; ++k) { int v = final_spokes[k % final_spokes.size()]; int cur_t = TIME_MIN; int cur_loc = u; int d = calc_dur(cities[u-1], cities[v-1]); while (cur_t + d <= TIME_MAX) { int dest = (cur_loc == u) ? v : u; schedule[k].push_back({cur_loc, dest, cur_t, cur_t + d}); cur_t += d; cur_loc = dest; } } mt19937 engine(42); auto start_time = chrono::system_clock::now(); while (chrono::duration_cast(chrono::system_clock::now() - start_time).count() < 850) { int k = engine() % K; if (schedule[k].size() < 2) continue; int f_idx = engine() % schedule[k].size(); int diff = (engine() % 2 == 0 ? 5 : -5); int new_s = schedule[k][f_idx].s + diff, new_t = schedule[k][f_idx].t + diff; if (new_s >= TIME_MIN && new_t <= TIME_MAX) { if ((f_idx == 0 || schedule[k][f_idx-1].t <= new_s) && (f_idx == (int)schedule[k].size()-1 || new_t <= schedule[k][f_idx+1].s)) { schedule[k][f_idx].s = new_s; schedule[k][f_idx].t = new_t; } } } for (int k = 0; k < K; ++k) { printf("%d\n", (int)schedule[k].size()); for (auto& f : schedule[k]) { printf("%d %02d:%02d %d %02d:%02d\n", f.from, f.s/60, f.s%60, f.to, f.t/60, f.t%60); } } } }; int main() { Solver().solve(); return 0; }