結果

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

ソースコード

diff #

#include <iostream>
#include <climits>
#include <queue>
#include <vector>
#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];
    // sから行ける場所のvector
    vector<p_int> st[city_n + 1];
    REP(i, 0, load_n) cin >> s[i];
    REP(i, 0, load_n) cin >> t[i];
    REP(i, 0, load_n) st[s[i]].push_back(make_pair(t[i], 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 s_front = front.second;
        vector<p_int> t_goto = st[s_front];
        REP(i, 0, t_goto.size()){
            p_int t_i = t_goto[i];
            int c_next = c_front + c[t_i.second];
            if (c_next <= c_max) {
                int t_next = t_i.first;
                int time_next = dp[c_front][s_front] + time[t_i.second];
                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