結果

問題 No.5023 Airlines Optimization
コンテスト
ユーザー ebicochineal
提出日時 2026-02-28 17:06:28
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 864 ms / 1,000 ms
コード長 5,859 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,803 ms
コンパイル使用メモリ 135,156 KB
実行使用メモリ 7,848 KB
スコア 33,890,532
最終ジャッジ日時 2026-02-28 17:08:03
合計ジャッジ時間 94,803 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 100
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <random>
#include <chrono>
#include <cstdio>

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<City> cities;
    
    vector<int> get_latest_departure_all(const vector<Flight>& all_flights, int dst, int target) {
        vector<int> 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<int>& spokes) {
        vector<Flight> 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<int> 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<pair<long long, int>> pop_rank;
        for(int i=0; i<N; ++i) pop_rank.push_back({cities[i].w, i});
        sort(pop_rank.rbegin(), pop_rank.rend());
        for (int i = 0; i < min(N, 20); ++i) { 
            int h_idx = pop_rank[i].second;
            vector<int> 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<int> 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<vector<Flight>> 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::milliseconds>(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;
}
0