結果

問題 No.1 道のショートカット
ユーザー fantasiabaetica
提出日時 2018-07-06 15:35:43
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,753 bytes
コンパイル時間 588 ms
コンパイル使用メモリ 66,604 KB
実行使用メモリ 103,928 KB
最終ジャッジ日時 2024-07-08 05:03:09
合計ジャッジ時間 7,213 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 7 TLE * 1 -- * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <climits>
#include <queue>
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define p_int pair<int, int>
using namespace std;

int main(){
    // 街の数、コスト上限、道の数
    int city_n, c_max, load_n;
    cin >> city_n >> c_max >> load_n;
    // 始点、終点、コスト、時間
    int s[load_n], t[load_n], c[load_n], time[load_n];
    REP(i, 0, load_n) cin >> s[i];
    REP(i, 0, load_n) cin >> t[i];
    REP(i, 0, load_n) cin >> c[i];
    REP(i, 0, load_n) cin >> time[i];

    // 使った費用を縦軸、現在地を横軸に取ってDP
    int dp[c_max + 1][city_n + 1];
    REP(i, 0, c_max + 1){
        REP(j, 0, city_n + 1){
            dp[i][j] = INT_MAX;
        }
    }
    dp[0][1] = 0;
    queue<p_int> q;
    q.push(make_pair(0, 1));
    while (!(q.empty())){
        // queueの先頭の要素を取り出し、そこから行ける都市を調べる
        p_int front = q.front();
        q.pop();
        int c_front = front.first;
        int t_front = front.second;
        REP(i, 0, load_n){
            int s_i = s[i];
            if (s_i != t_front) continue;
            int c_next = c_front + c[i];
            if (c_next <= c_max) {
                int t_next = t[i];
                int time_next = dp[c_front][t_front] + time[i];
                dp[c_next][t_next] = min(dp[c_next][t_next], time_next);
                //pushする内容がおかしい
                q.push(make_pair(c_next, t_next));
            }
        }
    }
    
    // dp[i][city_n]における最小値を返す
    int ans = INT_MAX;
    REP(i, 0, c_max + 1){
        ans = min(ans, dp[i][city_n]);
    }
    if (ans == INT_MAX) cout << -1 << endl;
    else cout << ans << endl;

    return 0;
}
0