結果

問題 No.2739 Time is money
ユーザー Yusuke.TYusuke.T
提出日時 2024-04-21 03:03:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,690 ms / 2,000 ms
コード長 3,450 bytes
コンパイル時間 2,324 ms
コンパイル使用メモリ 148,204 KB
実行使用メモリ 50,944 KB
最終ジャッジ日時 2024-04-21 03:03:51
合計ジャッジ時間 20,054 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 396 ms
28,928 KB
testcase_03 AC 802 ms
38,936 KB
testcase_04 AC 375 ms
28,160 KB
testcase_05 AC 484 ms
29,172 KB
testcase_06 AC 1,000 ms
37,200 KB
testcase_07 AC 1,436 ms
49,152 KB
testcase_08 AC 897 ms
48,620 KB
testcase_09 AC 934 ms
47,872 KB
testcase_10 AC 705 ms
47,360 KB
testcase_11 AC 1,690 ms
48,128 KB
testcase_12 AC 1,140 ms
49,792 KB
testcase_13 AC 1,150 ms
49,664 KB
testcase_14 AC 1,103 ms
50,944 KB
testcase_15 AC 751 ms
42,152 KB
testcase_16 AC 333 ms
42,496 KB
testcase_17 AC 1,546 ms
47,904 KB
testcase_18 AC 1,544 ms
47,784 KB
testcase_19 AC 670 ms
42,028 KB
権限があれば一括ダウンロードができます

ソースコード

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>(r) * X + get<2>(r)) - (get<1>(l) * X + get<2>(l)) < 0;
    }
};

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();

        if (visited.find(now) != visited.end()) {
            continue;
        }
        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