結果

問題 No.1 道のショートカット
ユーザー vjudge1
提出日時 2025-09-04 21:21:17
言語 TypeScript
(5.7.2)
結果
WA  
実行時間 -
コード長 1,407 bytes
コンパイル時間 7,771 ms
コンパイル使用メモリ 266,948 KB
実行使用メモリ 40,448 KB
最終ジャッジ日時 2025-09-04 21:21:32
合計ジャッジ時間 11,717 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 4
other WA * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

function main(): void {
    const input = require('fs').readFileSync(0, 'utf-8').trim().split(/\s+/);
    let index = 0;
    
    const [N, C, V] = [input[index++], input[index++], input[index++]].map(Number);
    
    // Read all arrays in one go
    const arrays = [[], [], [], []];
    for (let i = 0; i < 4; i++) {
        for (let j = 0; j < V; j++) {
            arrays[i].push(Number(input[index++]));
        }
    }
    
    const [S, T, Y, M] = arrays;
    S.forEach((val, i) => S[i] = val - 1); // Adjust S indices
    T.forEach((val, i) => T[i] = val - 1); // Adjust T indices
    
    // Build graph
    const graph: Array<Array<[number, number, number]>> = Array(N).fill(0).map(() => []);
    for (let i = 0; i < V; i++) {
        graph[S[i]].push([T[i], Y[i], M[i]]);
    }
    
    // DP setup
    const INF = 1e9;
    const dp = Array(N).fill(0).map(() => Array(C + 1).fill(INF));
    dp[0][C] = 0;
    
    // Process DP
    for (let i = 0; i < N; i++) {
        for (let k = C; k >= 0; k--) {
            if (dp[i][k] === INF) continue;
            
            for (const [to, cost, time] of graph[i]) {
                if (k >= cost) {
                    dp[to][k - cost] = Math.min(dp[to][k - cost], dp[i][k] + time);
                }
            }
        }
    }
    
    // Output result
    const result = Math.min(...dp[N - 1]);
    console.log(result === INF ? -1 : result);
}
0