結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー kappybarkappybar
提出日時 2021-01-23 16:51:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,634 bytes
コンパイル時間 3,212 ms
コンパイル使用メモリ 187,556 KB
実行使用メモリ 46,712 KB
最終ジャッジ日時 2023-08-29 22:37:40
合計ジャッジ時間 15,921 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 18 ms
5,632 KB
testcase_09 AC 8 ms
4,376 KB
testcase_10 AC 16 ms
4,716 KB
testcase_11 AC 15 ms
5,192 KB
testcase_12 AC 17 ms
4,700 KB
testcase_13 AC 295 ms
26,460 KB
testcase_14 AC 376 ms
21,992 KB
testcase_15 AC 373 ms
26,660 KB
testcase_16 AC 266 ms
17,992 KB
testcase_17 AC 146 ms
20,988 KB
testcase_18 AC 497 ms
35,172 KB
testcase_19 AC 497 ms
35,260 KB
testcase_20 AC 496 ms
35,384 KB
testcase_21 AC 496 ms
35,184 KB
testcase_22 AC 491 ms
35,164 KB
testcase_23 AC 97 ms
12,600 KB
testcase_24 AC 78 ms
6,096 KB
testcase_25 AC 258 ms
15,452 KB
testcase_26 AC 419 ms
19,492 KB
testcase_27 AC 277 ms
12,916 KB
testcase_28 AC 179 ms
12,748 KB
testcase_29 AC 254 ms
12,252 KB
testcase_30 AC 186 ms
12,616 KB
testcase_31 AC 128 ms
13,320 KB
testcase_32 AC 190 ms
11,284 KB
testcase_33 AC 373 ms
17,168 KB
testcase_34 AC 370 ms
20,476 KB
testcase_35 WA -
testcase_36 AC 396 ms
25,556 KB
testcase_37 AC 181 ms
9,612 KB
testcase_38 AC 245 ms
11,216 KB
testcase_39 AC 244 ms
11,228 KB
testcase_40 AC 245 ms
11,328 KB
testcase_41 AC 244 ms
11,312 KB
testcase_42 AC 244 ms
11,348 KB
testcase_43 AC 178 ms
46,712 KB
testcase_44 AC 142 ms
28,928 KB
testcase_45 AC 156 ms
28,132 KB
testcase_46 AC 41 ms
21,652 KB
testcase_47 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y));
#define chmax(x,y) x = max((x),(y));
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;

template<int mod> struct ModInt{
    long long x=0; 
    constexpr ModInt(long long x=0):x((x%mod+mod)%mod){}
    constexpr ModInt operator+(const ModInt& r)const{return ModInt(*this)+=r;}
    constexpr ModInt operator-(const ModInt& r)const{return ModInt(*this)-=r;}
    constexpr ModInt operator*(const ModInt& r)const{return ModInt(*this)*=r;}
    constexpr ModInt operator/(const ModInt& r)const{return ModInt(*this)/=r;}
    constexpr ModInt& operator+=(const ModInt& r){ if((x+=r.x)>=mod) x-=mod; return *this;}
    constexpr ModInt& operator-=(const ModInt& r){ if((x-=r.x)<0) x+=mod; return *this;}
    constexpr ModInt& operator*=(const ModInt& r){ if((x*=r.x)>=mod) x%=mod; return *this;}
    constexpr ModInt& operator/=(const ModInt& r){ return *this*=r.inv();}
    ModInt inv() const {
        long long s=x,sx=1,sy=0,t=mod,tx=0,ty=1;
        while(s%t!=0){
            long long temp=s/t,u=s-t*temp,ux=sx-temp*tx,uy=sy-temp*ty;
            s=t;sx=tx;sy=ty;
            t=u;tx=ux;ty=uy;
        }
        return ModInt(tx);
    }
    ModInt pow(long long n) const {
        ModInt a=1;
        ModInt b=*this;
        while(n>0){
            if(n&1) a*=b;
            b*=b;
            n>>=1;
        }
        return a;
    }
    friend constexpr ostream& operator<<(ostream& os,const ModInt<mod>& a) {return os << a.x;}
    friend constexpr istream& operator>>(istream& is,ModInt<mod>& a) {return is >> a.x;}
};
using mint = ModInt<MOD>;


//StronglyConnectedCompoinent
struct scc_graph{
    int n;
    vector<vector<int>> graph;
    vector<vector<int>> revgraph;
    vector<int> postorder;
    vector<int> seen;

    scc_graph(int n):n(n),graph(n),revgraph(n),seen(n,-1){}

    void add_edge(int from,int to){
        graph[from].push_back(to);
        revgraph[to].push_back(from);
    }

    vector<vector<int>> scc(){
        fill(seen.begin(),seen.end(),-1);
        for(int i=0;i<n;i++){
            if(seen[i]<0) dfs(i);
        }
        fill(seen.begin(),seen.end(),-1);

        int k = 0;
        for(int i=(int)postorder.size()-1;i>=0;i--){
            if(seen[postorder[i]]<0) revdfs(postorder[i],k++);
        }

        vector<vector<int>> scc_groups(k);
        for(int i=0;i<n;i++){
            scc_groups[seen[i]].push_back(i);
        }

        return scc_groups;
    }

    void dfs(int i){
        seen[i] = 1;
        for(int j:graph[i]){
            if(seen[j]>=0) continue;
            dfs(j);
        }
        postorder.push_back(i);
    }

    void revdfs(int i,int k){
        seen[i] = k;
        for(int j:revgraph[i]){
            if(seen[j]>=0) continue;
            revdfs(j,k);
        }
    }
};

struct edge{
    ll to,l,a;
};

mint dp[100005];
mint cnt[100005];

int main(){
    int n,m;
    cin >> n >> m;
    vector<vector<edge>> G(n+1);
    scc_graph graph(n+1);
    rep(i,m){
        ll u,v,l,a;
        cin >> u >> v >> l >> a;
        G[u].push_back(edge{v,l,a});
        graph.add_edge(u,v);
    }
    vector<vector<int>> topo = graph.scc();
    map<int,int> real_temp;
    rep(i,topo.size()){
        for(int j:topo[i]){
            real_temp[j] = i;
        }
    }
    vector<int> seen(topo.size(),0);
    vector<bool> dfs_res(topo.size(),false);
    int n_temp = real_temp[n];
    bool cycle = false;
    auto dfs = [&](auto&& dfs,int i) -> bool{
        if(seen[i] == 1) return dfs_res[i];
        seen[i] = 1;
        bool ok = false;
        if(i == n_temp) ok = true;
        for(int j:topo[i]){
            for(auto e:G[j]){
                int to = real_temp[e.to];
                if(seen[to] == 1) continue;
                bool temp = dfs(dfs,to);
                if(temp) ok = true;
            }
        }
        if(ok && (int)topo[i].size() != 1){
            cycle = true;
        }
        return dfs_res[i] = ok;
    };
    dfs(dfs,0);
    if(seen[n_temp] == 0){
        cout << 0 << endl;
        return 0;
    }
    if(cycle){
        cout << "INF" << endl;
        return 0;
    }
    dp[0] = 0;
    cnt[0] = 1;
    rep(ii,topo.size()){
        int i = topo[ii][0];
        for(auto e:G[i]){
            dp[e.to] += dp[i] * e.a + cnt[i] * e.a * e.l;
            cnt[e.to] += cnt[i] * e.a;
        }
    }
    cout << dp[n] << endl;

    return 0;
}
0