結果

問題 No.3669 误差绝不允许
コンテスト
ユーザー akakimidori
提出日時 2026-09-04 23:50:50
言語 C++23(gnu拡張gcc16)
(gcc 16.1.0 + boost 1.92.0)
コンパイル:
g++-16 -O2 -lm -std=gnu++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 770 ms / 3,000 ms
+ 897µs
コード長 2,299 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,200 ms
コンパイル使用メモリ 535,816 KB
実行使用メモリ 15,732 KB
最終ジャッジ日時 2026-09-04 23:51:05
合計ジャッジ時間 13,842 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <algorithm>
#include <bitset>
#include <boost/multiprecision/cpp_int.hpp>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <vector>

using namespace boost::multiprecision;

using i64 = int64_t;
using u64 = uint64_t;
using i32 = int32_t;
using u32 = uint32_t;

template <class T>
std::vector<T> vec(T elem, int len) {
    return std::vector<T>(len, elem);
}

void run(void) {
    i32 n, m;
    std::cin >> n >> m;
    std::vector<std::tuple<int, int, int, int>> edge;
    std::vector<std::vector<int>> g(n);
    for (int i = 0; i < m; ++i) {
        i32 u, v, a, b;
        std::cin >> u >> v >> a >> b;
        u--;
        v--;
        edge.emplace_back(u, v, a, b);
        if (u != v) {
            g[u].push_back(i);
            g[v].push_back(i);
            //std::cout << i << ":" << u << " " << v << " " << a << " " << b << std::endl;
        }
    }
    std::vector<cpp_int> nu(n, 1);
    std::vector<cpp_int> de(n, 0);
    nu[0] = 0;
    de[0] = 1;
    using F = std::pair<std::pair<cpp_int, cpp_int>, int>;
    auto cmp = [&](F x, F y) {
        const auto a = x.first;
        const auto b = y.first;
        return a.first * b.second > a.second * b.first;
    };
    std::priority_queue<F, std::vector<F>, decltype(cmp)> pq(cmp);
    pq.emplace(std::make_pair(nu[0], de[0]), 0);
    while (pq.size()) {
        auto [p, v] = pq.top();
        auto [a, b] = p;
        pq.pop();
        if (nu[v] != a || de[v] != b) continue;
        for (const int j : g[v]) {
            const auto [p, q, x, y] = edge[j];
            const int u = p ^ q ^ v;
            cpp_int next_nu = y * a + x * b;
            cpp_int next_de = y * b;
            cpp_int g = gcd(next_nu, next_de);
            if (g != 1) {
                next_nu = next_nu / g;
                next_de = next_de / g;
            }
            if (next_nu * de[u] < next_de * nu[u]) {
                nu[u] = next_nu;
                de[u] = next_de;
                pq.emplace(std::make_pair(nu[u], de[u]), u);
            }
        }
    }
    for (int i = 1; i < n; ++i) {
        std::cout << nu[i] << " " << de[i] << "\n";
    }
}

int main(void) {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    run();
    return 0;
}
0