結果

問題 No.1 道のショートカット
ユーザー cormoran
提出日時 2016-11-14 01:51:11
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,444 bytes
コンパイル時間 1,623 ms
コンパイル使用メモリ 176,140 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 16:27:45
合計ジャッジ時間 2,654 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i, j) for(int i=0; i < (int)(j); i++)
#define all(v) v.begin(),v.end()

template<class T> bool set_min(T &a, const T &b) { return a > b  ? a = b, true : false; }
template<class T> istream& operator >> (istream &is , vector<T> &v) { for(T &a : v) is >> a; return is; }
const int INF = 1 << 30;

struct Vertex {
    int index, cost, time;
};

class Solver {
  public:
    bool solve() {
        int N, C, V; cin >> N >> C >> V;
        vector<int> S(V), T(V), Y(V), M(V);
        cin >> S >> T >> Y >> M;

        vector<vector<Vertex>> E(N);
        rep(i, V) {
            S[i]--; T[i]--;
            E[S[i]].push_back(Vertex{T[i], Y[i], M[i]});
        }

        vector<vector<int>> dp(N, vector<int>(C + 1, INF));
        fill(all(dp[0]), 0);
        rep(i, N) {
            rep(c, C) {
                if(dp[i][c] < INF) {                        
                    for(auto nxt: E[i]) {
                        if(c + nxt.cost <= C) {                            
                            set_min(dp[nxt.index][c + nxt.cost], dp[i][c] + nxt.time);
                        }
                    }
                }
            }
        }
        int ans = *min_element(all(dp[N - 1]));
        cout << (ans < INF ? ans : -1) << endl;        
        return 0;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Solver s;
    s.solve();
    return 0;
}
0