結果

問題 No.3669 误差绝不允许
コンテスト
ユーザー t98slider
提出日時 2026-09-06 12:18:46
言語 C++23
(gcc 15.3.0 + boost 1.92.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
TLE  
実行時間 -
コード長 4,499 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,557 ms
コンパイル使用メモリ 616,592 KB
実行使用メモリ 53,920 KB
最終ジャッジ日時 2026-09-06 12:19:04
合計ジャッジ時間 14,406 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 12 TLE * 1 -- * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using Bint = mp::int512_t;

//約分を毎回しない
template<class T> struct yuri {
    T num, den;
    yuri() : num(0), den(1) {}
    yuri(T a) : num(a), den(1) {}
    yuri(int a) : num(a), den(1) {}
    yuri(long long a) : num(a), den(1) {}
    yuri(T a, T b) : num(a), den(b) {}
    void safe(){
        if(num < 0) den *= -1, num *= -1;
        T v = gcd(num, den);
        num /= v, den /= v;
    }
    yuri& operator++() { num += den; return *this; }
    yuri& operator--() { num -= den; return *this; }
    yuri& operator+=(const yuri& rhs) {
        num *= rhs.den;
        num += rhs.num * den;
        den *= rhs.den;
        safe();
        return *this;
    }
    yuri& operator-=(const yuri& rhs) {
        num *= rhs.den;
        num -= rhs.num * den;
        den *= rhs.den;
        return *this;
    }
    yuri& operator*=(const yuri& rhs) {
        num *= rhs.num;
        den *= rhs.den;
        return *this;
    }
    yuri& operator/=(const yuri& rhs) {
        num *= rhs.den;
        den *= rhs.num;
        return *this ; 
    }
    yuri operator+() const { return *this; }
    yuri operator-() const { return yuri() - *this; }
    friend yuri operator+(const yuri lhs, const yuri rhs) {
        return yuri(lhs) += rhs;
    }
    friend yuri operator-(const yuri& lhs, const yuri& rhs) {
        return yuri(lhs) -= rhs;
    }
    friend yuri operator*(const yuri& lhs, const yuri& rhs) {
        return yuri(lhs) *= rhs;
    }
    friend yuri operator/(const yuri& lhs, const yuri& rhs) {
        return yuri(lhs) /= rhs;
    }
    friend bool operator==(const yuri& lhs, const yuri& rhs) {
        return (lhs.num * rhs.den == rhs.num * lhs.den);
    }
    friend bool operator!=(const yuri& lhs, const yuri& rhs) {
        return (lhs.num * rhs.den != rhs.num * lhs.den);
    }
    friend bool operator<(const yuri& lhs, const yuri& rhs) {
        return (lhs.num*rhs.den<lhs.den*rhs.num);
    }
    friend bool operator<=(const yuri& lhs, const yuri& rhs) {
        return (lhs.num*rhs.den<=lhs.den*rhs.num);
    }
    friend bool operator>(const yuri& lhs, const yuri& rhs) {
        return (lhs.num*rhs.den>lhs.den*rhs.num);
    }
    friend bool operator>=(const yuri& lhs, const yuri& rhs) {
        return (lhs.num*rhs.den>=lhs.den*rhs.num);
    }
    friend ostream& operator << (ostream &os, const yuri rhs) noexcept {
        return os << rhs.num << ' ' << rhs.den;
    }
};

template <class T> struct csr {
    struct Node {
        csr* g;
        int u;
        template<class... Args>
        void emplace_back(Args&&... args){
            g->add_edge(u, T(std::forward<Args>(args)...));
        }
        auto begin(){ return g->E.begin() + g->start[u]; }
        auto end(){ return g->E.begin() + g->start[u + 1]; }
        int size(){ return g->start[u + 1] - g->start[u]; }
        T& operator[](int p){ return *(begin() + p); }
    };
    int N;
    std::vector<int> start;
    std::vector<T> E;
    std::vector<std::pair<int,T>> edge;
    csr(int n) : N(n), start(n + 1) {edge.reserve(n);}
    void add_edge(int u, T v){
		assert(0 <= u && u < N);
        start[u + 1]++;
        edge.emplace_back(u, v);
    }
    void build(){
        E.resize(edge.size());
        for(int i = 0; i < N; i++) start[i + 1] += start[i];
        auto cnt = start;
        for(auto [u, v] : edge) E[cnt[u]++] = v;
    }
	const int size() {return N;}
    Node operator[](int u) {return Node{this, u};}
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    csr<pair<int,yuri<Bint>>> g(n);
    int u, v, a, b;
    for(int i = 0; i < m; i++){
        cin >> u >> v >> a >> b;
        u--, v--;
        g[u].emplace_back(v, yuri<Bint>(a, b));
        g[v].emplace_back(u, yuri<Bint>(a, b));
    }
    g.build();
    vector<yuri<Bint>> dp(n, yuri<Bint>(1 << 30, 1));
    priority_queue<pair<yuri<Bint>,int>, vector<pair<yuri<Bint>,int>>, greater<pair<yuri<Bint>,int>>> pq;
    dp[0] = yuri<Bint>(0, 1);
    pq.push({dp[0], 0});
    while(!pq.empty()){
        auto [d, v] = pq.top();
        pq.pop();
        if(d > dp[v]) continue;
        for(auto [u, w] : g[v]){
            auto L = d + w;
            if(L >= dp[u]) continue;
            dp[u] = L;
            pq.emplace(dp[u], u);
        }
    }
    for(int v = 1; v < n; v++){
        dp[v].safe();
        cout << dp[v] << '\n';
    }
}
0