結果

問題 No.1 道のショートカット
ユーザー ktgktg
提出日時 2016-04-20 22:26:43
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 2,159 bytes
コンパイル時間 902 ms
コンパイル使用メモリ 89,792 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 16:23:07
合計ジャッジ時間 1,920 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <iterator>

#define pb push_back
#define puts(x) cout << #x << " : " << x << endl;
#pragma GCC diagnostic ignored "-Wconversion"

#define REP(i,n) for (int i=0;i<(n);i++)
#define REPE(i,n) for (int i=0;i<=(n);i++)

#define init(a,b) memset((a), (b), (sizeof(a)));

#define PI 3.14159265
#define EPS (1e-10)
#define EQ(a,b) (abs((a)-(b)) < EPS)

using namespace std;

typedef long long ll;
//firstは最短距離、secondは頂点の番号
typedef pair<int, int> P;

const int INF = 1000*1000*1000;
const int MAX_V=100; // 点の数
const int MAX_M=500; // 金
struct edge { int to, distance, money; };
vector<edge> G[MAX_V];
int dp[MAX_V][MAX_M];

int main() {
    int N, C, V;cin>>N>>C>>V;
    vector<int> S(V), T(V), Y(V), M(V);
    for(int i=0;i<V;i++){cin>>S[i];S[i]--;}
    for(int i=0;i<V;i++){cin>>T[i];T[i]--;}
    for(int i=0;i<V;i++){cin>>Y[i];}
    for(int i=0;i<V;i++){cin>>M[i];}
    for(int i=0;i<V;i++){
        G[S[i]].push_back(edge{T[i], M[i],Y[i]});
    }

    priority_queue<P, vector<P>, greater<P> > PQ;
    for(int i=0;i<N;i++) fill(dp[i], dp[i] + 300, INF);
    fill(dp[0], dp[0] + 300, 0);
    PQ.push(P(0, C));
    while (!PQ.empty()) {
        P p = PQ.top(); PQ.pop();
        int v = p.first;
        int dist = p.second / 10000;
        int money = p.second % 10000;

        if (dp[v][money] < dist) continue;

        for (int i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            int rest_money = money - e.money;
            if(rest_money<0)continue;
            if (dp[e.to][rest_money] > dp[v][money] + e.distance) {
                dp[e.to][rest_money] = dp[v][money] + e.distance;
                PQ.push(P(e.to, dp[e.to][rest_money] * 10000 + rest_money));
            }
        }
    }
    int mn = INF;
    for(int i=0;i<300;i++){
        if (dp[N-1][i] > 0)
            mn = min(mn, dp[N-1][i]);
    }
    if(mn==INF)cout<<-1<<endl;
    else cout<<mn<<endl;
    return 0;
}
0