結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー HaaHaa
提出日時 2021-01-22 23:00:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 308 ms / 2,500 ms
コード長 1,867 bytes
コンパイル時間 1,914 ms
コンパイル使用メモリ 178,188 KB
実行使用メモリ 19,944 KB
最終ジャッジ日時 2023-08-28 11:07:28
合計ジャッジ時間 11,465 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,872 KB
testcase_01 AC 6 ms
9,040 KB
testcase_02 AC 6 ms
8,888 KB
testcase_03 AC 6 ms
9,020 KB
testcase_04 AC 6 ms
8,852 KB
testcase_05 AC 7 ms
9,008 KB
testcase_06 AC 6 ms
8,888 KB
testcase_07 AC 6 ms
8,820 KB
testcase_08 AC 15 ms
9,732 KB
testcase_09 AC 11 ms
9,616 KB
testcase_10 AC 16 ms
9,692 KB
testcase_11 AC 15 ms
9,360 KB
testcase_12 AC 18 ms
9,628 KB
testcase_13 AC 168 ms
14,632 KB
testcase_14 AC 253 ms
16,952 KB
testcase_15 AC 239 ms
16,500 KB
testcase_16 AC 177 ms
14,648 KB
testcase_17 AC 79 ms
11,616 KB
testcase_18 AC 306 ms
17,472 KB
testcase_19 AC 305 ms
17,340 KB
testcase_20 AC 306 ms
17,352 KB
testcase_21 AC 305 ms
17,560 KB
testcase_22 AC 308 ms
17,448 KB
testcase_23 AC 67 ms
10,904 KB
testcase_24 AC 77 ms
10,964 KB
testcase_25 AC 178 ms
14,176 KB
testcase_26 AC 285 ms
16,944 KB
testcase_27 AC 214 ms
15,896 KB
testcase_28 AC 126 ms
12,888 KB
testcase_29 AC 208 ms
15,544 KB
testcase_30 AC 132 ms
12,672 KB
testcase_31 AC 87 ms
11,676 KB
testcase_32 AC 165 ms
14,600 KB
testcase_33 AC 273 ms
16,352 KB
testcase_34 AC 242 ms
15,104 KB
testcase_35 AC 230 ms
14,848 KB
testcase_36 AC 228 ms
14,856 KB
testcase_37 AC 169 ms
13,896 KB
testcase_38 AC 231 ms
15,272 KB
testcase_39 AC 230 ms
15,420 KB
testcase_40 AC 232 ms
15,208 KB
testcase_41 AC 230 ms
15,216 KB
testcase_42 AC 230 ms
15,296 KB
testcase_43 AC 106 ms
19,944 KB
testcase_44 AC 82 ms
12,776 KB
testcase_45 AC 97 ms
15,344 KB
testcase_46 AC 9 ms
10,440 KB
testcase_47 AC 6 ms
8,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=1000000007;
constexpr ll INF=1e18;

struct E{
    int to;
    ll l;
    ll a;
};

int n, m;
int M=110000;
vector<vector<E>> edge(M,vector<E>(0));
vector<bool> lop(M,true);
VI inc(M,0);
VI ways(M,0);

void dag(){
    VI h(n,0);
    REP(i,n){
        for(auto nx:edge[i]){
            h[nx.to]++;
        }
    }
    queue<int> q;
    REP(i,n){
        if(h[i]==0){
            q.push(i);
        }
    }
    while(!q.empty()){
        int v=q.front();
        lop[v]=0;
        q.pop();
        for(auto nx:edge[v]){
            if(--h[nx.to]==0){
                q.push(nx.to);
            }
        }
    }
}

bool ok=1;
void initdfs(int v){
    if(v==n-1)
        ok=0;
    for(auto nx:edge[v]){
        inc[nx.to]++;
        if(inc[nx.to]==1)
            initdfs(nx.to);
    }
}

void initdfs2(int v){
    for(auto nx:edge[v]){
        inc[nx.to]--;
        if(lop[v])
            lop[nx.to]=1;
        if(inc[nx.to]==0)
            initdfs2(nx.to);
    }
}

VI ww(M,0), lp(M,0);
void dfs(int v){
    for(auto nx:edge[v]){
        ww[nx.to]=(ww[nx.to]+ww[v]*nx.a%MOD+lp[v]*nx.a%MOD*nx.l%MOD)%MOD;
        lp[nx.to]=(lp[nx.to]+lp[v]*nx.a)%MOD;
        if(--inc[nx.to]==0)
            dfs(nx.to);
    }
}

int main(){
    cin >> n >> m; n++;
    int u, v, l, a;
    REP(i,m){
        cin >> u >> v >> l >> a;
        edge[u].push_back(E{v,l,a});
    }
    dag();
    initdfs(0);
    initdfs2(0);
    if(lop[n-1]){
        if(!ok)
            cout << "INF" << endl;
        if(ok)
            cout << 0 << endl;
        return 0;
    }
    inc[0]++;
    initdfs(0);
    lp[0]=1;
    dfs(0);
    cout << ww[n-1] << endl;
    return 0;
}
0