結果

問題 No.1 道のショートカット
コンテスト
ユーザー Irmystp
提出日時 2014-11-26 20:39:51
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 1,402 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 922 ms
コンパイル使用メモリ 177,588 KB
実行使用メモリ 184,080 KB
最終ジャッジ日時 2026-04-03 01:14:50
合計ジャッジ時間 1,996 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

const int INF = 1<<29;

struct edge{
    int from;
    int to;
    int dist;
    int cost;
    edge(){} ~edge(){}
    edge(int a, int b, int c): to(a), dist(b), cost(c) {}
    bool operator <(const edge& r) const {
        return dist > r.dist;
    }
};

int N, C, V;
vector<edge> G(1500);
int d[51][301];

int main(){
    scanf("%d%d%d", &N, &C, &V);
    for(int x = 0; x < V; x++){
        scanf("%d", &G[x].from);
    }
    for(int x = 0; x < V; x++){
        scanf("%d", &G[x].to);
    }
    for(int x = 0; x < V; x++){
        scanf("%d", &G[x].cost);
    }
    for(int x = 0; x < V; x++){
        scanf("%d", &G[x].dist);
    }
    for(int x = 0; x <= N; x++){
        fill(d[x], d[x]+C+1, INF);
    }
    d[1][C] = 0;
    for(int x = 0; x < N; x++){
        bool updated = false;
        for(auto& e : G){
            for(int y = e.cost; y <= C; y++){
                if(d[e.from][y] + e.dist < d[e.to][y - e.cost]){
                    updated = true;
                    d[e.to][y - e.cost] = d[e.from][y] + e.dist;
                }
            }
        }
        if(!updated) break;
    }
    int res = INF;
    for(int x = 0; x <= C; x++){
        res = min(res, d[N][x]);
    }
    if(res < INF){
        printf("%d\n", res);
    }else{
        puts("-1");
    }
    return 0;
}
0