結果

問題 No.2739 Time is money
ユーザー Yusuke.T
提出日時 2024-04-21 02:53:12
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,363 bytes
コンパイル時間 6,016 ms
コンパイル使用メモリ 176,648 KB
最終ジャッジ日時 2025-02-21 07:43:24
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 15 TLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <iomanip>

constexpr int MAX_INT = 2147483647;
constexpr int MIN_INT = -2147483648;

#define rep(n, a, b) for (int (n) = (a); (n) < (b); (n)++)
#define rrep(n, a, b) for (int (n) = (a); (n) >= (b); (n)--)
#define llrep(n, a, b) for (long long (n) = (a); (n) < (b); (n)++)
#define llrrep(n, a, b) for (long long (n) = (a); (n) >= (b); (n)--)
#define itrAll(x) (x).begin(), (x).end()
#define inputvec(v) for (auto& x : v) {std::cin >> x;}
using namespace std;

using uint = unsigned int;
using ushort = unsigned short;
using byte = unsigned char;
using ll = long long;
using ull = unsigned long long;
using ldouble = long double;
using ipair = pair<int, int>;
using llpair = pair<long long, long long>;

template<typename T>
void chmin(T& v1, T v2) {
	if (v1 > v2) v1 = v2;
}

template<typename T>
void chmax(T& v1, T v2) {
	if (v1 < v2) v1 = v2;
}

class union_find
{
private:
	vector<size_t> parent;

public:
	union_find(size_t N) {
		parent = vector<size_t>(N);
		for (size_t i = 0; i < N; i++) {
			parent[i] = i;
		}
	}

	int get_root(size_t x) {
		if (parent[x] == x) {
			return x;
		}
		else {
			return (parent[x] = get_root(parent[x]));
		}
	}

	bool unite(size_t x, size_t y) {
		auto rx = get_root(x), ry = get_root(y);
		if (rx == ry) {
			return false;
		}
		else {
			parent[rx] = ry;
			return true;
		}
	}

	bool has_same_parent(size_t x, size_t y) {
		return get_root(x) == get_root(y);
	}
};

int N, M, X;

struct comparer
{
    bool operator()(tuple<int, ll, ll>& l, tuple<int, ll, ll>& r) {
        return (get<1>(l) * X + get<2>(l)) > (get<1>(r) * X + get<2>(r));
    }
};

int main() {
    cin >> N >> M >> X;

    map<int, map<int, llpair>> cost; // { 移動にかかる時間, 通行料 }
    union_find uf(N); // 到達可能性の確認用

    rep(_, 0, M) {
        int ui, vi, ci, ti;
        cin >> ui >> vi >> ci >> ti;

        ui--, vi--;
        ipair p = { ti,ci };
        cost[ui][vi] = p;
        cost[vi][ui] = p;
        uf.unite(ui, vi);
    }

    if (!uf.has_same_parent(0, N - 1)) { // 到達不能
        cout << -1 << endl;
        return 0;
    }

    set<int> visited;
    priority_queue<tuple<int, ll, ll>, vector<tuple<int, ll, ll>>, comparer> Q; // { 位置, 経過時間, かかったお金 }
    Q.push({ 0,0LL,0LL });

    while (Q.size()) {
        auto [now, elapsed, spend] = Q.top();
        Q.pop();
        visited.insert(now);

        if (now == N - 1) {
            cout << (elapsed + (int)ceil((double)spend / X)) << endl;
            return 0;
        }

        if (cost.find(now) != cost.end()) {
            vector<int> tmp(cost[now].size());
            for (auto& [nxt, tc] : cost[now]) {
                if (visited.find(nxt) == visited.end()) {
                    Q.push({ nxt, elapsed + tc.first, spend + tc.second });
                }
                else {
                    tmp.push_back(nxt);
                }
            }

            for (auto key : tmp) {
                // 計算時間短縮......?
                cost[now].erase(key);
                cost[key].erase(now);
            }
        }
    }

    return 0;
}
0