結果
| 問題 | No.5023 Airlines Optimization |
| コンテスト | |
| ユーザー |
e869120
|
| 提出日時 | 2026-02-22 14:21:42 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 954 ms / 1,000 ms |
| コード長 | 11,482 bytes |
| 記録 | |
| コンパイル時間 | 2,902 ms |
| コンパイル使用メモリ | 157,040 KB |
| 実行使用メモリ | 7,968 KB |
| スコア | 52,805,613 |
| 最終ジャッジ日時 | 2026-02-25 20:38:49 |
| 合計ジャッジ時間 | 103,434 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
| 純コード判定しない問題か言語 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 100 |
ソースコード
#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];
int map_t[50][21];
mt19937 rnd(42);
// 時刻文字列をスロット(5分単位)に変換
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);
}
// ★ 追加: 現在の都市 u から近い都市を優先して選ぶ(トーナメント選択)
int get_rand_city_near(int u) {
// 基準都市がない場合は完全にランダム
if (u == -1) return city_candidates[rnd() % city_candidates.size()];
int best_c = -1;
double best_req = 1e9;
// 5つの候補をランダムに出し、最も所要時間が短いものを採用する
for(int i = 0; i < 5; ++i) {
int c = city_candidates[rnd() % city_candidates.size()];
if (c == u) continue;
if (req_time[u][c] < best_req) {
best_req = req_time[u][c];
best_c = c;
}
}
if (best_c == -1) return city_candidates[rnd() % city_candidates.size()];
return best_c;
}
// スクエア航空向けの事前計算用DP
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;
}
}
}
}
}
}
// シーケンスを時間内(180スロット)に収まるよう調整
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 req = req_time[nseq.back()][seq[i]];
if (cur_time + req <= 180) {
cur_time += req;
nseq.push_back(seq[i]);
} else {
break;
}
}
while (true) {
int u = nseq.back();
int v = get_rand_city_near(u); // ★ ランダムから近距離優先に変更
while (u == v) v = get_rand_city_near(u);
int req = req_time[u][v];
if (cur_time + req <= 180) {
cur_time += req;
nseq.push_back(v);
} else {
break;
}
}
seq = nseq;
}
// 1機分のシーケンスからフライト一覧を生成
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 req = req_time[u][v];
res.push_back({u, v, cur_time, cur_time + req});
cur_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();
int last_active = -1;
for (int t = 0; t < 21; ++t) {
if (active[t]) {
active_t_list[j].push_back(t);
last_active = t;
}
map_t[j][t] = last_active;
}
}
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 i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (!valid_pair[i][j]) continue;
long long weight = pair_weight[i][j];
for (int t = 0; t < 21; ++t) {
int eff_t = map_t[j][t];
int ci_val = (eff_t == -1) ? -1 : ci_latest[i][j][eff_t];
if (ci_val > sq_latest[i][j][t]) {
v_ci += weight;
}
}
}
}
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;
sq_flights.push_back({u, v, time_to_slot(s), time_to_slot(t)});
}
sort(sq_flights.begin(), sq_flights.end());
calc_sq_latest(sq_flights, sq_latest);
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 & 15) == 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() % 3;
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);
}
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);
// ★ 追加: 長距離フライト(24スロット = 120分 = 2時間以上)が含まれているかチェック
bool has_long_flight = false;
for (int i = 0; i + 1 < cur_seq.size(); ++i) {
if (req_time[cur_seq[i]][cur_seq[i+1]] > 24) {
has_long_flight = true;
break;
}
}
// ★ 追加: 長距離便がある場合の採用確率のペナルティ処理
double prob = 0.0;
if (new_score >= cur_score) {
// スコアが改善しても、長距離が含まれる場合は 30% の確率でしか通さない
prob = has_long_flight ? 0.3 : 1.0;
} else {
prob = exp((new_score - cur_score) / temp);
// 悪化遷移で、さらに長距離が含まれる場合は確率を 1/20 にする
if (has_long_flight) prob *= 0.05;
}
if (prob > (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;
}
e869120