結果

問題 No.5023 Airlines Optimization
コンテスト
ユーザー e869120
提出日時 2026-02-23 12:45:15
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 954 ms / 1,000 ms
コード長 12,065 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,954 ms
コンパイル使用メモリ 164,360 KB
実行使用メモリ 43,740 KB
スコア 57,899,890
最終ジャッジ日時 2026-02-25 20:49:21
合計ジャッジ時間 103,966 ms
ジャッジサーバーID
(参考情報)
judge3 / judge6
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 100
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#pragma GCC optimize("O3")
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <chrono>
#include <random>

using namespace std;

int N, M, K;
double R;
vector<int> X, Y, W;
double req_time[50][50];
long long pair_weight[50][50];
bool valid_pair[50][50];

struct Flight {
    int u, v, dep, arr;
    bool operator<(const Flight& o) const {
        return dep > o.dep; // 出発時刻の降順 (逆順DP用)
    }
};

vector<Flight> sq_flights;
int sq_latest[50][50][21];
int ci_latest[50][50][21];
vector<int> city_candidates;

vector<int> active_t_list[50];
vector<int> sq_deps_from[50];

// 累積和配列
int sq_win_cum[50][50][22][182];

mt19937 rnd(42);

int time_to_slot(const string& t) {
    int h = stoi(t.substr(0, 2));
    int m = stoi(t.substr(3, 2));
    return (h - 6) * 12 + m / 5;
}

string slot_to_time(int slot) {
    int h = 6 + slot / 12;
    int m = (slot % 12) * 5;
    char buf[10];
    snprintf(buf, sizeof(buf), "%02d:%02d", h, m);
    return string(buf);
}

int get_rand_city() {
    return city_candidates[rnd() % city_candidates.size()];
}

// ★ 追加: 直前の都市 u からの移動時間 d に応じて確率を 1 / max(1, d) 倍する
int get_rand_city_near(int u) {
    if (u == -1) return get_rand_city(); // 基準がない場合は完全ランダム
    
    while (true) {
        int v = get_rand_city();
        if (u == v) continue;
        
        // 1スロット=5分なので、12で割ると「時間(hour)」になる
        double d_hours = req_time[u][v] / 12.0;
        
        // 採用確率: 1 / max(1, d)
        double prob = pow(1.0 / max(1.0, d_hours), 4.0);
        
        if ((double)(rnd() % 10000) / 10000.0 <= prob) {
            return v;
        }
    }
}

void calc_sq_latest(const vector<Flight>& flights, int latest[50][50][21]) {
    for (int j = 0; j < N; ++j) {
        for (int t_idx = 0; t_idx < 21; ++t_idx) {
            for (int u = 0; u < N; ++u) {
                latest[u][j][t_idx] = -1;
            }
            latest[j][j][t_idx] = 60 + t_idx * 6; 
        }
    }
    for (const auto& f : flights) {
        for (int j = 0; j < N; ++j) {
            for (int t_idx = 0; t_idx < 21; ++t_idx) {
                if (latest[f.v][j][t_idx] >= f.arr) {
                    if (f.dep > latest[f.u][j][t_idx]) {
                        latest[f.u][j][t_idx] = f.dep;
                    }
                }
            }
        }
    }
}

int get_delayed_dep(int u, int cur_time) {
    int dep_time = cur_time;
    bool delayed = true;
    while (delayed) {
        delayed = false;
        for (int sq_dep : sq_deps_from[u]) {
            if (sq_dep >= dep_time && sq_dep <= dep_time + 1) {
                dep_time = sq_dep + 1; 
                delayed = true;
            }
        }
    }
    return dep_time;
}

void adjust_seq(vector<int>& seq) {
    if (seq.empty()) seq.push_back(get_rand_city_near(-1));
    int cur_time = 0;
    vector<int> nseq;
    nseq.push_back(seq[0]);
    
    for (int i = 1; i < seq.size(); ++i) {
        if (nseq.back() == seq[i]) continue;
        int u = nseq.back();
        int v = seq[i];
        
        int dep_time = get_delayed_dep(u, cur_time);
        int req = req_time[u][v];
        
        if (dep_time + req <= 180) {
            cur_time = dep_time + req;
            nseq.push_back(v);
        } else {
            break;
        }
    }
    while (true) {
        int u = nseq.back();
        // ★ 変更: 距離に応じた確率で次の都市を選ぶ
        int v = get_rand_city_near(u); 
        
        int dep_time = get_delayed_dep(u, cur_time);
        int req = req_time[u][v];
        
        if (dep_time + req <= 180) {
            cur_time = dep_time + req;
            nseq.push_back(v);
        } else {
            break;
        }
    }
    seq = nseq;
}

vector<Flight> get_flights_for_plane(const vector<int>& seq) {
    vector<Flight> res;
    int cur_time = 0;
    for (int i = 0; i + 1 < seq.size(); ++i) {
        int u = seq[i];
        int v = seq[i + 1];
        
        int dep_time = get_delayed_dep(u, cur_time);
        int req = req_time[u][v];
        
        res.push_back({u, v, dep_time, dep_time + req});
        cur_time = dep_time + req;
    }
    return res;
}

long long eval(vector<Flight> ci_flights) {
    sort(ci_flights.begin(), ci_flights.end());
    
    for (int j = 0; j < N; ++j) {
        bool active[21] = {false};
        for (const auto& f : ci_flights) {
            if (f.v == j) {
                int t = (f.arr <= 60) ? 0 : (f.arr - 60 + 5) / 6;
                if (t >= 0 && t < 21) active[t] = true;
            }
        }
        active_t_list[j].clear();
        for (int t = 0; t < 21; ++t) {
            if (active[t]) {
                active_t_list[j].push_back(t);
            }
        }
    }

    for (int j = 0; j < N; ++j) {
        for (int t_idx : active_t_list[j]) {
            for (int u = 0; u < N; ++u) {
                ci_latest[u][j][t_idx] = -1;
            }
            ci_latest[j][j][t_idx] = 60 + t_idx * 6;
        }
    }

    for (const auto& f : ci_flights) {
        for (int j = 0; j < N; ++j) {
            for (int t_idx : active_t_list[j]) {
                if (ci_latest[f.v][j][t_idx] >= f.arr) {
                    if (f.dep > ci_latest[f.u][j][t_idx]) {
                        ci_latest[f.u][j][t_idx] = f.dep;
                    }
                }
            }
        }
    }

    long long v_ci = 0;
    for (int j = 0; j < N; ++j) {
        int num_active = active_t_list[j].size();
        for (int m = 0; m < num_active; ++m) {
            int eff_t = active_t_list[j][m];
            int t_start = eff_t;
            int t_end = (m + 1 < num_active) ? active_t_list[j][m + 1] - 1 : 20;

            for (int i = 0; i < N; ++i) {
                if (!valid_pair[i][j]) continue;
                
                int ci_val = ci_latest[i][j][eff_t];
                if (ci_val == -1) continue; 
                
                int safe_ci_val = ci_val > 180 ? 180 : ci_val;
                
                int wins = sq_win_cum[i][j][t_end + 1][safe_ci_val] - sq_win_cum[i][j][t_start][safe_ci_val];
                v_ci += pair_weight[i][j] * wins;
            }
        }
    }
    return v_ci;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    auto start_time = chrono::high_resolution_clock::now();

    if (!(cin >> N >> R)) return 0;
    X.resize(N); Y.resize(N); W.resize(N);
    for (int i = 0; i < N; ++i) {
        cin >> X[i] >> Y[i] >> W[i];
        int cnt = W[i] / 100000;
        if (cnt <= 0) cnt = 1;
        for (int c = 0; c < cnt; ++c) city_candidates.push_back(i);
    }

    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            double d = hypot(X[i] - X[j], Y[i] - Y[j]);
            double exact_time = d / 800.0 * 60.0 + 40.0;
            req_time[i][j] = ceil(exact_time / 5.0);
            
            valid_pair[i][j] = (d >= 0.25 * R && i != j);
            pair_weight[i][j] = (long long)W[i] * W[j];
        }
    }

    cin >> M;
    for (int i = 0; i < M; ++i) {
        int u, v; string s, t;
        cin >> u >> s >> v >> t;
        --u; --v;
        int dep_slot = time_to_slot(s);
        int arr_slot = time_to_slot(t);
        sq_flights.push_back({u, v, dep_slot, arr_slot});
        sq_deps_from[u].push_back(dep_slot);
    }
    
    for (int i = 0; i < N; ++i) {
        sort(sq_deps_from[i].begin(), sq_deps_from[i].end());
        sq_deps_from[i].erase(unique(sq_deps_from[i].begin(), sq_deps_from[i].end()), sq_deps_from[i].end());
    }
    
    sort(sq_flights.begin(), sq_flights.end());
    calc_sq_latest(sq_flights, sq_latest);

    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
            if (!valid_pair[i][j]) continue;
            for (int X_val = 0; X_val <= 180; ++X_val) {
                int cnt = 0;
                sq_win_cum[i][j][0][X_val] = 0;
                for (int t = 0; t < 21; ++t) {
                    if (sq_latest[i][j][t] < X_val) {
                        cnt++;
                    }
                    sq_win_cum[i][j][t + 1][X_val] = cnt;
                }
            }
        }
    }

    cin >> K;

    vector<vector<int>> best_seqs;
    vector<Flight> fixed_flights;

    double total_limit = 0.95; 

    for (int k = 0; k < K; ++k) {
        vector<int> cur_seq;
        adjust_seq(cur_seq); 
        
        vector<Flight> cur_flights = get_flights_for_plane(cur_seq);
        vector<Flight> test_flights = fixed_flights;
        test_flights.insert(test_flights.end(), cur_flights.begin(), cur_flights.end());
        
        long long cur_score = eval(test_flights);
        long long best_k_score = cur_score;
        vector<int> best_k_seq = cur_seq;

        double T0 = 1e12; 
        double T1 = 1e9;
        int iter = 0;
        
        double end_time_for_k = total_limit * (k + 1) / K;
        double start_time_for_k = total_limit * k / K;
        double time_allocated = end_time_for_k - start_time_for_k;
        
        while (true) {
            if ((iter & 63) == 0) {
                double elapsed = chrono::duration<double>(chrono::high_resolution_clock::now() - start_time).count();
                if (elapsed > end_time_for_k) break;
                
                double progress = max(0.0, min(1.0, (elapsed - start_time_for_k) / time_allocated));
                double temp = T0 * pow(T1 / T0, progress);
                
                int type = rnd() % 6;
                vector<int> old_seq = cur_seq;
                
                // ★ 変更: 近傍操作時も直前の都市からの距離を考慮して選ばせる
                if (type == 0 && cur_seq.size() > 1) { 
                    int idx = 1 + rnd() % (cur_seq.size() - 1);
                    cur_seq[idx] = get_rand_city_near(cur_seq[idx - 1]);
                } else if (type == 1) { 
                    int idx = rnd() % (cur_seq.size() + 1);
                    int prev_city = (idx == 0) ? -1 : cur_seq[idx - 1];
                    cur_seq.insert(cur_seq.begin() + idx, get_rand_city_near(prev_city));
                } else if (type == 2 && cur_seq.size() > 2) { 
                    int idx = rnd() % cur_seq.size();
                    cur_seq.erase(cur_seq.begin() + idx);
                } else if (type >= 3 && cur_seq.size() > 1) { 
                    int idx1 = rnd() % cur_seq.size();
                    int idx2 = rnd() % cur_seq.size();
                    swap(cur_seq[idx1], cur_seq[idx2]);
                }
                
                adjust_seq(cur_seq);
                
                vector<Flight> new_flights = get_flights_for_plane(cur_seq);
                vector<Flight> eval_flights = fixed_flights;
                eval_flights.insert(eval_flights.end(), new_flights.begin(), new_flights.end());
                
                long long new_score = eval(eval_flights);
                
                if (new_score >= cur_score || exp((new_score - cur_score) / temp) > (double)(rnd() % 10000) / 10000.0) {
                    cur_score = new_score;
                    if (cur_score > best_k_score) {
                        best_k_score = cur_score;
                        best_k_seq = cur_seq;
                    }
                } else {
                    cur_seq = old_seq; 
                }
            }
            iter++;
        }
        
        best_seqs.push_back(best_k_seq);
        vector<Flight> final_k_flights = get_flights_for_plane(best_k_seq);
        fixed_flights.insert(fixed_flights.end(), final_k_flights.begin(), final_k_flights.end());
    }

    for (int k = 0; k < K; ++k) {
        vector<Flight> f_list = get_flights_for_plane(best_seqs[k]);
        cout << f_list.size() << "\n";
        for (const auto& f : f_list) {
            cout << f.u + 1 << " " << slot_to_time(f.dep) << " " 
                 << f.v + 1 << " " << slot_to_time(f.arr) << "\n";
        }
    }

    return 0;
}
0