結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー milanis48663220milanis48663220
提出日時 2021-01-22 22:43:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,750 bytes
コンパイル時間 1,214 ms
コンパイル使用メモリ 109,144 KB
実行使用メモリ 34,520 KB
最終ジャッジ日時 2023-08-27 20:50:03
合計ジャッジ時間 9,027 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,148 KB
testcase_01 AC 5 ms
7,220 KB
testcase_02 AC 4 ms
7,164 KB
testcase_03 AC 5 ms
7,476 KB
testcase_04 AC 4 ms
7,236 KB
testcase_05 AC 4 ms
7,284 KB
testcase_06 AC 4 ms
7,284 KB
testcase_07 AC 5 ms
7,176 KB
testcase_08 AC 13 ms
8,908 KB
testcase_09 AC 7 ms
7,752 KB
testcase_10 AC 12 ms
8,432 KB
testcase_11 AC 11 ms
8,464 KB
testcase_12 AC 12 ms
8,212 KB
testcase_13 AC 171 ms
25,940 KB
testcase_14 AC 220 ms
22,592 KB
testcase_15 AC 220 ms
24,880 KB
testcase_16 AC 148 ms
18,908 KB
testcase_17 AC 83 ms
19,576 KB
testcase_18 AC 274 ms
30,984 KB
testcase_19 AC 270 ms
31,028 KB
testcase_20 AC 269 ms
31,148 KB
testcase_21 AC 280 ms
31,200 KB
testcase_22 AC 280 ms
31,092 KB
testcase_23 AC 51 ms
14,116 KB
testcase_24 AC 31 ms
9,772 KB
testcase_25 AC 126 ms
18,440 KB
testcase_26 AC 205 ms
22,684 KB
testcase_27 AC 112 ms
17,084 KB
testcase_28 AC 89 ms
15,500 KB
testcase_29 AC 99 ms
16,060 KB
testcase_30 AC 94 ms
15,576 KB
testcase_31 AC 69 ms
14,564 KB
testcase_32 AC 75 ms
15,096 KB
testcase_33 AC 184 ms
20,400 KB
testcase_34 AC 187 ms
21,900 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 69 ms
13,536 KB
testcase_38 AC 90 ms
15,180 KB
testcase_39 AC 90 ms
15,448 KB
testcase_40 AC 89 ms
15,488 KB
testcase_41 AC 90 ms
15,436 KB
testcase_42 AC 91 ms
15,300 KB
testcase_43 AC 83 ms
34,520 KB
testcase_44 AC 62 ms
25,200 KB
testcase_45 AC 73 ms
30,808 KB
testcase_46 AC 25 ms
18,824 KB
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

class ModInt{
    public:
    ll v;
    ModInt(ll _v = 0){
        if(_v >= MOD) _v %= MOD;
        v = _v;
    }
    ModInt operator+(ll n){
        return ModInt((v+n)%MOD);
    }
    ModInt operator-(ll n){
        return ModInt((v-n+MOD)%MOD);
    }
    ModInt operator*(ll n){
        if(n >= MOD) n %= MOD;
        return ModInt((v*n)%MOD);
    }
    ModInt operator/(ll n){
        return ModInt((ModInt(n).inv()*v).v%MOD);
    }

    void operator+=(ll n){
        v = (v+n)%MOD;
    }
    void operator-=(ll n){
        v = (v-n+MOD)%MOD;
    }
    void operator*=(ll n){
        v = (v*n+MOD)%MOD;
    }
    
    
    ModInt operator+(ModInt n){
        return ModInt((v+n.v)%MOD);
    }
    ModInt operator-(ModInt n){
        return ModInt((v-n.v+MOD)%MOD);
    }
    ModInt operator*(ModInt n){
        return ModInt((v*n.v)%MOD);
    }
    ModInt operator/(ModInt n){
        return ModInt((n.inv()*v).v%MOD);
    }

    void operator+=(ModInt n){
        v = (v+n.v)%MOD;
    }
    void operator-=(ModInt n){
        v = (v-n.v+MOD)%MOD;
    }
    void operator*=(ModInt n){
        v = (v*n.v)%MOD;
    }

    void operator=(ModInt n){
        v = n.v;
    }
    bool operator==(ModInt n){
        return v == n.v;
    }
    bool operator!=(ModInt n){
        return v != n.v;
    }
    void operator=(ll n){
        v = n;
    }

    ModInt inv(){
        if(v == 1) return ModInt(1);
        else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD);
    }
};

ostream& operator<<(ostream& os, const ModInt& m){
    os << m.v;
    return os;
}

istream & operator >> (istream &in,  ModInt &m){
    in >> m.v;
    return in;
}

class Scc{
    public:
        int N;
        vector<vector<int>> G;
        vector<vector<int>> G_inv;
        vector<int> idx;

        Scc(int n){
            N = n;
            G = vector<vector<int>>(n, vector<int>());
            G_inv = vector<vector<int>>(n, vector<int>());
            used = vector<bool>(n, false);
            idx = vector<int>(n);
        }

        void add_edge(int from, int to){
            G[from].push_back(to);
            G_inv[to].push_back(from);
        }

        vector<vector<int>> scc(){
            vector<vector<int>> ans;
            for(int i = 0; i < N; i++){
                if(!used[i]) dfs1(i);
            }
            clear();
            int cur = 0;
            for(int i = vs.size()-1; i >= 0; i--){
                if(!used[vs[i]]) {
                    dfs2(vs[i], cur);
                    cur++;
                    ans.push_back(buf);
                    buf.clear();
                }
            }
            return ans;
        }

    private:
        vector<bool> used;
        vector<int> vs;
        vector<int> buf;
        void clear(){
            for(int i = 0; i < N; i++) used[i] = false;
        }

        void dfs1(int v){
            used[v] = true;
            for(int i = 0; i < G[v].size(); i++){
                if(!used[G[v][i]]) dfs1(G[v][i]);
            }
            vs.push_back(v);
        }

        void dfs2(int v, int k){
            used[v] = true;
            idx[v] = k;
            for(int i = 0; i < G_inv[v].size(); i++){
                if(!used[G_inv[v][i]]) dfs2(G_inv[v][i], k);
            }
            buf.push_back(v);
        }
};

struct edge{
    int to;
    ModInt l;
    int a;  
};

ModInt dp_sum[100001];
ModInt dp_cnt[100001];
vector<edge> g[100001];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    Scc scc(n+1);
    for(int i = 0; i < m; i++){
        int u, v, l, a;
        cin >> u >> v >> l >> a;
        scc.add_edge(u, v);
        g[u].push_back((edge){v, ModInt(l), a});
    }
    auto v = scc.scc();
    vector<int> tsort;
    for(auto u : v){
        if(u.size() > 1){
            cout << "INF" << endl;
            return 0;
        }
        tsort.push_back(u[0]);
    }
    dp_cnt[0] = 1;
    for(int u : tsort){
        for(edge e : g[u]){
            dp_cnt[e.to] += dp_cnt[u]*e.a;
            dp_sum[e.to] += dp_sum[u]*e.a+e.l*dp_cnt[u]*e.a;
        }
    }
    cout << dp_sum[n] << endl;
}
0