結果

問題 No.1 道のショートカット
ユーザー vjudge1
提出日時 2025-09-04 21:34:15
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 2,198 bytes
コンパイル時間 1,331 ms
コンパイル使用メモリ 105,852 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-04 21:34:20
合計ジャッジ時間 3,140 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;

const int INF = 1e9;

struct Edge {
    int to, cost, time;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int N, C, V;
    cin >> N >> C >> V;
    
    // Pre-allocate all memory
    vector<int> S(V), T(V), Y(V), M(V);
    
    // Fast input reading
    for (int i = 0; i < V; i++) { cin >> S[i]; S[i]--; }
    for (int i = 0; i < V; i++) { cin >> T[i]; T[i]--; }
    for (int i = 0; i < V; i++) cin >> Y[i];
    for (int i = 0; i < V; i++) cin >> M[i];
    
    // Build graph with pre-allocation
    vector<vector<Edge>> graph(N);
    vector<int> graph_size(N, 0);
    for (int i = 0; i < V; i++) {
        graph_size[S[i]]++;
    }
    for (int i = 0; i < N; i++) {
        graph[i].reserve(graph_size[i]);
    }
    for (int i = 0; i < V; i++) {
        graph[S[i]].push_back({T[i], Y[i], M[i]});
    }
    
    // Use 2D array with flat memory for better cache performance
    vector<int> dp_flat(N * (C + 1), INF);
    auto dp = [&](int i, int j) -> int& {
        return dp_flat[i * (C + 1) + j];
    };
    
    dp(0, C) = 0;
    
    // Main DP loop with maximum optimization
    for (int i = 0; i < N; i++) {
        const auto& edges = graph[i];
        const int edge_count = edges.size();
        
        for (int k = C; k >= 0; k--) {
            const int current_val = dp(i, k);
            if (current_val == INF) continue;
            
            for (int j = 0; j < edge_count; j++) {
                const Edge& e = edges[j];
                if (k >= e.cost) {
                    int& target = dp(e.to, k - e.cost);
                    const int new_time = current_val + e.time;
                    if (new_time < target) {
                        target = new_time;
                    }
                }
            }
        }
    }
    
    // Find minimum result with SIMD-friendly loop
    int result = INF;
    const int last_row_start = (N - 1) * (C + 1);
    for (int k = 0; k <= C; k++) {
        result = min(result, dp_flat[last_row_start + k]);
    }
    
    cout << (result == INF ? -1 : result) << '\n';
    
    return 0;
}
0