結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー outlineoutline
提出日時 2021-01-22 23:27:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 2,500 ms
コード長 4,511 bytes
コンパイル時間 2,308 ms
コンパイル使用メモリ 155,976 KB
実行使用メモリ 23,500 KB
最終ジャッジ日時 2023-08-28 12:08:40
合計ジャッジ時間 8,405 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,500 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 7 ms
4,384 KB
testcase_13 AC 92 ms
13,292 KB
testcase_14 AC 120 ms
13,700 KB
testcase_15 AC 120 ms
14,680 KB
testcase_16 AC 81 ms
11,140 KB
testcase_17 AC 45 ms
10,252 KB
testcase_18 AC 167 ms
17,564 KB
testcase_19 AC 160 ms
17,568 KB
testcase_20 AC 153 ms
17,452 KB
testcase_21 AC 154 ms
17,512 KB
testcase_22 AC 159 ms
17,508 KB
testcase_23 AC 32 ms
7,016 KB
testcase_24 AC 27 ms
5,248 KB
testcase_25 AC 83 ms
11,240 KB
testcase_26 AC 138 ms
14,884 KB
testcase_27 AC 86 ms
10,644 KB
testcase_28 AC 56 ms
9,120 KB
testcase_29 AC 78 ms
10,164 KB
testcase_30 AC 61 ms
9,048 KB
testcase_31 AC 42 ms
7,896 KB
testcase_32 AC 62 ms
8,976 KB
testcase_33 AC 126 ms
13,352 KB
testcase_34 AC 124 ms
14,156 KB
testcase_35 AC 125 ms
15,864 KB
testcase_36 AC 112 ms
14,076 KB
testcase_37 AC 60 ms
8,080 KB
testcase_38 AC 82 ms
9,648 KB
testcase_39 AC 83 ms
9,312 KB
testcase_40 AC 82 ms
9,584 KB
testcase_41 AC 83 ms
9,524 KB
testcase_42 AC 84 ms
9,504 KB
testcase_43 AC 64 ms
23,500 KB
testcase_44 AC 42 ms
13,332 KB
testcase_45 AC 58 ms
22,264 KB
testcase_46 AC 6 ms
9,060 KB
testcase_47 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

struct mint {
    int x;
    mint() : x(0) {}
    mint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
    mint& operator+=(const mint& p){
        if((x += p.x) >= mod)   x -= mod;
        return *this;
    }
    mint& operator-=(const mint& p){
        if((x -= p.x) < 0)  x += mod;
        return *this;
    }
    mint& operator*=(const mint& p){
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }
    mint& operator/=(const mint& p){
        *this *= p.inverse();
        return *this;
    }
    mint operator-() const { return mint(-x); }
    mint operator+(const mint& p) const { return mint(*this) += p; }
    mint operator-(const mint& p) const { return mint(*this) -= p; }
    mint operator*(const mint& p) const { return mint(*this) *= p; }
    mint operator/(const mint& p) const { return mint(*this) /= p; }
    bool operator==(const mint& p) const { return x == p.x; }
    bool operator!=(const mint& p) const { return x != p.x; }
    mint pow(int64_t n) const {
        mint res = 1, mul = x;
        while(n > 0){
            if(n & 1)   res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    mint inverse() const { return pow(mod - 2); }
    friend ostream& operator<<(ostream& os, const mint& p){
        return os << p.x;
    }
    friend istream& operator>>(istream& is, mint& p){
        int64_t val;
        is >> val;
        p = mint(val);
        return is;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;
    using edge = tuple<int, int, int>;
    vector<vector<edge>> g(N + 1), rg(N + 1);
    for(int i = 0; i < M; ++i){
        int u, v, l, a;
        cin >> u >> v >> l >> a;
        g[u].emplace_back(v, l, a);
        rg[v].emplace_back(u, l, a);
    }

    vector<bool> from0(N + 1), fromN(N + 1), visited(N + 1);
    auto dfs = [&](auto&& self, vector<vector<edge>>& G, vector<bool>& f, int cur) -> void {
        visited[cur] = true;
        f[cur] = true;
        for(auto [nxt, cost, weight] : G[cur]){
            if(!visited[nxt])   self(self, G, f, nxt);
        }
    };
    dfs(dfs, g, from0, 0);
    visited.assign(N + 1, false);
    dfs(dfs, rg, fromN, N);

    int V = 0;
    vector<bool> flag(N + 1);
    for(int i = 0; i <= N; ++i){
        flag[i] = from0[i] && fromN[i];
        V += flag[i];
    }
    vector<int> indeg(N + 1);
    for(int from = 0; from <= N; ++from){
        if(!flag[from]) continue;
        for(auto [to, cost, weight] : g[from]){
            if(flag[to])    indeg[to] += 1;
        }
    }
    
    queue<int> que;
    for(int i = 0; i <= N; ++i){
        if(flag[i] && indeg[i] == 0)    que.emplace(i);
    }
    vector<int> topological_order;
    while(!que.empty()){
        int from = que.front();
        que.pop();
        topological_order.emplace_back(from);
        for(auto [to, cost, weight] : g[from]){
            if(!flag[to])   continue;
            if(--indeg[to] == 0)    que.emplace(to);
        }
    }

    if((int)topological_order.size() != V){
        cout << "INF\n";
        return 0;
    }

    vector<mint> dp(N + 1), dp2(N + 1);
    dp2[0] = 1;
    for(int from : topological_order){
        for(auto [to, cost, weight] : g[from]){
            if(!flag[to])   continue;
            dp2[to] += dp2[from] * weight;
        }
    }
    for(int from : topological_order){
        for(auto [to, cost, weight] : g[from]){
            if(!flag[to])   continue;
            dp[to] += (dp[from] + dp2[from] * cost) * weight;
        }
    }

    cout << dp[N] << endl;

    return 0;
}
0