結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー lorent_kyoprolorent_kyopro
提出日時 2021-01-23 00:45:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,411 bytes
コンパイル時間 2,674 ms
コンパイル使用メモリ 224,688 KB
実行使用メモリ 25,964 KB
最終ジャッジ日時 2023-08-28 10:24:30
合計ジャッジ時間 8,768 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,380 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 34 ms
9,468 KB
testcase_24 AC 30 ms
8,128 KB
testcase_25 AC 100 ms
18,156 KB
testcase_26 AC 170 ms
25,816 KB
testcase_27 AC 106 ms
20,772 KB
testcase_28 AC 71 ms
13,816 KB
testcase_29 AC 96 ms
20,000 KB
testcase_30 AC 71 ms
14,276 KB
testcase_31 AC 46 ms
11,268 KB
testcase_32 AC 72 ms
18,156 KB
testcase_33 AC 141 ms
24,188 KB
testcase_34 AC 132 ms
22,800 KB
testcase_35 AC 119 ms
17,960 KB
testcase_36 AC 113 ms
17,084 KB
testcase_37 AC 71 ms
15,220 KB
testcase_38 AC 95 ms
19,388 KB
testcase_39 AC 95 ms
19,552 KB
testcase_40 AC 100 ms
19,436 KB
testcase_41 AC 93 ms
19,472 KB
testcase_42 AC 93 ms
19,720 KB
testcase_43 AC 65 ms
24,296 KB
testcase_44 WA -
testcase_45 AC 63 ms
22,892 KB
testcase_46 AC 6 ms
7,860 KB
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define ALL(v) v.begin(), v.end()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
const array<string, 2> YESNO = {"NO", "YES"};
const array<string, 2> YesNo = {"No", "Yes"};
const array<string, 2> yesno = {"no", "yes"};
void YES(bool b = true) { cout << YESNO[b] << '\n'; }
void Yes(bool b = true) { cout << YesNo[b] << '\n'; }
void yes(bool b = true) { cout << yesno[b] << '\n'; }
template<typename T, typename U>
inline bool chmax(T& a, const U& b) {
    if (a < b){
        a = b;
        return true;
    }
    return false;
}
template<typename T, typename U>
inline bool chmin(T& a, const U& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
void UNIQUE(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()));
}
template<typename T>
int lb(const vector<T> v, T x) {
    return distance(v.begin(), lower_bound(v.begin(), v.end(), x));
}
template<typename T>
int ub(const vector<T> v, T x) {
    return distance(v.begin(), upper_bound(v.begin(), v.end(), x));
}
/**
 * @brief 多次元 vector の作成
 * @author えびちゃん
 */
namespace detail {
    template<typename T, int N>
    auto make_vec(vector<int>& sizes, T const& x) {
        if constexpr (N == 1) {
            return vector(sizes[0], x);
        } else {
            int size = sizes[N-1];
            sizes.pop_back();
            return vector(size, make_vec<T, N-1>(sizes, x));
        }
    }
}
template<typename T, int N>
auto make_vec(int const(&sizes)[N], T const& x = T()) {
    vector<int> s(N);
    for (int i = 0; i < N; ++i) s[i] = sizes[N-i-1];
    return detail::make_vec<T, N>(s, x);
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        if (it == v.begin()) os << *it;
        else os << ' ' << *it;
    }
    return os;
}
template<typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << p.first << ' ' << p.second;
    return os;
}
__attribute__((constructor))
void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}
#include <atcoder/modint>
using namespace atcoder;
using mint = modint1000000007;
// using mint = modint998244353;

int main() {
    int n, m;
    cin >> n >> m;
    auto g = make_vec<tuple<ll, ll, ll>>({n+1, 0});
    auto rg = make_vec<tuple<ll, ll, ll>>({n+1, 0});

    rep(i, m) {
        ll u, v, l, a;
        cin >> u >> v >> l >> a;
        g[u].eb(v, l, a);
        rg[v].eb(u, l, a);
    }

    vector<bool> ok(n+1), rok(n+1);
    auto dfs = [&](auto self, int u) -> void {
        ok[u] = true;
        for (auto [v, l, a] : g[u]) {
            if (ok[v]) continue;
            self(self, v);
        }
    };
    auto rdfs = [&](auto self, int u) -> void {
        rok[u] = true;
        for (auto [v, l, a] : rg[u]) {
            if (rok[v]) continue;
            self(self, v);
        }
    };
    dfs(dfs, 0);
    rdfs(rdfs, n);

    if (!(ok[n] && rok[n])) {
        cout << 0 << '\n';
        return 0;
    }

    auto ng = make_vec<tuple<ll, ll, ll>>({n+1, 0});
    vector<int> in(n+1);
    rep(i, n+1) {
        if (ok[i] && rok[i]) {
            for (auto [v, l, a] : g[i]) {
                if (ok[v] && rok[v]) {
                    ng[i].eb(v, l, a);
                    in[v]++;
                }
            }
        }
    }

    queue<int> q;
    vector<int> topolo;
    rep(i, n+1) if (in[i] == 0) q.push(i), topolo.pb(i);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (auto [v, l, a] : g[u]) {
            in[v]--;
            if (in[v] == 0) {
                q.push(v);
                topolo.pb(v);
            }
        }
    }
    rep(i, n+1) if (in[i] != 0) {
        cout << "INF\n";
        return 0;
    }

    vector<pair<mint, mint>> dp(n+1);
    dp[0].fi = 1;
    for (auto u : topolo) {
        for (auto [v, l, a] : g[u]) {
            dp[v].fi += dp[u].fi * a;
            dp[v].se += (dp[u].se + dp[u].fi * l) * a;
        }
    }
    cout << dp[n].se.val() << '\n';
}
0