結果

問題 No.2739 Time is money
ユーザー Shibaken_8128Shibaken_8128
提出日時 2024-04-20 11:36:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,412 bytes
コンパイル時間 1,662 ms
コンパイル使用メモリ 125,952 KB
実行使用メモリ 31,260 KB
最終ジャッジ日時 2024-04-20 11:36:38
合計ジャッジ時間 8,361 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
31,260 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 99 ms
17,208 KB
testcase_03 AC 180 ms
23,136 KB
testcase_04 AC 95 ms
16,636 KB
testcase_05 AC 127 ms
17,300 KB
testcase_06 AC 193 ms
21,596 KB
testcase_07 AC 250 ms
26,184 KB
testcase_08 AC 205 ms
26,220 KB
testcase_09 AC 214 ms
26,684 KB
testcase_10 AC 197 ms
25,620 KB
testcase_11 AC 308 ms
26,784 KB
testcase_12 AC 184 ms
24,920 KB
testcase_13 AC 178 ms
24,976 KB
testcase_14 AC 164 ms
25,160 KB
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip>
#include <climits>
#include <cmath>
#include <functional>
#include <numeric>
#include <regex>
#include <array>
#include <fstream>
#include <sstream>

//#include<atcoder/modint>
//using namespace atcoder;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define repl(i,l,r) for (int i = l; i < (int)(r); i++)
#define all(a) a.begin(),a.end()
#define Pii pair<int,int>
#define Pll pair<long,long>
#define INFi 1000000001
#define INFl 1000000000000000001
#define ll long long
using namespace std;



template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T> void printArray(vector<T>&A){
    for(T&a:A){
        cout<<a<<" ";
    }
    cout<<endl;
}
template<class T> void printArrayln(vector<T>&A){
    for(T&a:A){
        cout<<a<<endl;
    }
}


template<class T1,class T2> std::ostream &operator<<(std::ostream &out, const pair<T1,T2> &A){
    cout<<"{"<<A.first<<","<<A.second<<"}";
    return out;
}

template<class T1,class T2> std::ostream &operator<<(std::ostream &out, const map<T1,T2> &M){
    for(const auto&A:M){
        cout<<"{"<<A.first<<","<<A.second<<"}";
    }
    return out;
}

template<class T1> std::ostream &operator<<(std::ostream &out, const set<T1> &M){
    cout<<"{";
    for(const auto&A:M){
        cout<<A<<", ";
    }
    cout<<"}"<<endl;
    return out;
}


template<class T1> std::ostream &operator<<(std::ostream &out, const multiset<T1> &M){
    cout<<"{";
    for(const auto&A:M){
        cout<<A<<", ";
    }
    cout<<"}"<<endl;
    return out;
}

template<class T> std::ostream &operator<<(std::ostream &out, const vector<T> &A){
    for(const T &a:A){
        cout<<a<<" ";
    }
    return out;
}

void print() { cout << endl; }
 
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
  cout << H << " ";
  print(T...);
}


template<class T> std::istream &operator>>(std::istream &in,vector<T>&A){
    for(T&a:A){
        std::cin>>a;
    }
    return in;
}

struct Edge{
    int to;
    long cost;
    long time;
};

using Graph = vector<vector<Edge>>;

struct State{
    int v;
    long cost;
    long time;
    bool operator>(const State &s) const{
        if(time==s.time){
            return cost<s.cost;
        }
        return time>s.time;
    }
};

int main(void){
    std::cin.tie(0)->sync_with_stdio(0);
    int N,M,X;cin>>N>>M>>X;
    Graph G(N);
    rep(i,M){
        int a,b,c,t;cin>>a>>b>>c>>t;
        a--;b--;
        G[a].push_back({b,c,t});
        G[b].push_back({a,c,t});
    }
    
    priority_queue<State,vector<State>,greater<State>> pq;

    pq.push({0,0,0});
    vector<State> dist(N,{0,INFl,INFl});
    dist[0]={0,0,0};
    int cnt = 0;
    while(!pq.empty() && cnt<5000000){
        cnt ++;
        State s=pq.top();pq.pop();
        if(s.v == N-1){
            break;
        }
        for(Edge e:G[s.v]){
            long t = s.time+e.time;
            long m = s.cost - e.cost;            
            if(m<0){
                // お金が足りない
                long m2 = abs(m); //あとm円足りない
                long h = (m2+X-1)/X; //h時間働いてm円稼ぐ
                t+=h;
                m+=h*X;
            }
            State next = {e.to,m,t};
            if(dist[e.to]>next){
                dist[e.to]=next;
                pq.push(next);
            }
        }
    }
    if(dist[N-1].time==INFl){
        cout<<-1<<endl;
    }else{
        cout<<dist[N-1].time<<endl;
    }
}

/*
5 6 10
1 3 7 2
4 1 2 5
3 4 1 3
2 3 5 4
4 5 6 8
2 5 4 1


9
*/
0