結果

問題 No.2739 Time is money
ユーザー Yusuke.TYusuke.T
提出日時 2024-04-21 02:11:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 3,024 bytes
コンパイル時間 1,343 ms
コンパイル使用メモリ 130,588 KB
実行使用メモリ 817,536 KB
最終ジャッジ日時 2024-04-21 02:11:05
合計ジャッジ時間 4,720 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 MLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

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, int, int>& l, tuple<int, int, int>& r) {
        return ((ll)X * get<1>(l) + get<2>(l)) > ((ll)X * get<1>(r) + get<2>(r));
    }
};

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

    vector<vector<ipair>> conn(N, vector<ipair>(N, { -1, -1 })); // { 移動にかかる時間, 通行料 }
    union_find uf(N); // 到達可能性の確認用

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

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

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

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

    while (Q.size()) {
        auto current = Q.top();
        Q.pop();

        if (get<0>(current) == N - 1) {
            cout << (get<1>(current) + (int)ceil((double)get<2>(current) / X)) << endl;
            return 0;
        }

        rep(nxt, 0, N) {
            auto& tc = conn[get<0>(current)][nxt];
            if (tc.first >= 0) {
                Q.push({ nxt, get<1>(current) + tc.first, get<2>(current) + tc.second });
            }
        }
    }

    return 0;
}
0