結果

問題 No.1 道のショートカット
ユーザー tokkaka
提出日時 2016-07-16 19:23:52
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,548 bytes
コンパイル時間 1,025 ms
コンパイル使用メモリ 91,676 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2024-07-20 16:24:28
合計ジャッジ時間 2,327 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 39 RE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <functional>
#include <tuple>
#include <climits>
#include <algorithm>

using namespace std;

struct Road
{
    int destination;
    int cost;
    int time;
    Road() : destination(0), cost(0), time(0) {}
    Road(int d, int c, int t) : destination(d), cost(c), time(t) {}
};

std::vector<std::vector<Road>> road;
std::vector<std::vector<tuple<int,int>>> dp;
enum {
    arrival_time = 0,
    minimum_time = 1
};


int solve(int start, int goal, int money, int time)
{
    if(money < 0) return -1;
    if(start == goal) return time;
    if(road[start].empty()) return -1;
    if(get<arrival_time>(dp[start][money]) != -2 && get<arrival_time>(dp[start][money]) < time)
        return get<minimum_time>(dp[start][money]);

    int min=INT_MAX;
    for(const auto &r : road[start]) {
        int t = solve(r.destination, goal, money-r.cost, time+r.time);
        if(t < min && t != -1) min = t;
    }
    if(min == INT_MAX) min = -1;
    dp[start][money] = make_tuple(time, min);
    return min;
}

int main()
{
    int N, C, V;
    cin >> N >> C >> V;
    std::vector<int> S(V), T(V), Y(V), M(V);
    for(auto & s : S) cin >> s;
    for(auto & t : T) cin >> t;
    for(auto & y : Y) cin >> y;
    for(auto & m : M) cin >> m;
    road.resize(N+1);
    for(int i=0; i<V; i++)
        road[S[i]].push_back(Road(T[i], Y[i], M[i]));
    dp.resize(V+1);
    for(auto &a : dp) {
        a.resize(C+1, make_tuple(-2, -2));
    }
    cout << solve(1, N, C, 0) << std::endl;
}
0