結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー kappybarkappybar
提出日時 2021-01-22 22:35:24
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 4,006 bytes
コンパイル時間 1,914 ms
コンパイル使用メモリ 187,220 KB
実行使用メモリ 38,588 KB
最終ジャッジ日時 2023-08-28 11:04:14
合計ジャッジ時間 12,927 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 12 ms
4,376 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 14 ms
4,376 KB
testcase_11 AC 11 ms
4,376 KB
testcase_12 AC 16 ms
4,380 KB
testcase_13 AC 181 ms
14,896 KB
testcase_14 AC 282 ms
16,064 KB
testcase_15 AC 259 ms
16,768 KB
testcase_16 AC 189 ms
12,528 KB
testcase_17 AC 81 ms
10,528 KB
testcase_18 AC 327 ms
19,912 KB
testcase_19 AC 322 ms
19,736 KB
testcase_20 AC 325 ms
20,132 KB
testcase_21 AC 323 ms
20,036 KB
testcase_22 AC 327 ms
20,380 KB
testcase_23 AC 73 ms
8,112 KB
testcase_24 AC 76 ms
5,424 KB
testcase_25 AC 283 ms
16,972 KB
testcase_26 AC 462 ms
21,620 KB
testcase_27 AC 291 ms
13,024 KB
testcase_28 AC 205 ms
13,528 KB
testcase_29 AC 268 ms
12,136 KB
testcase_30 AC 202 ms
13,476 KB
testcase_31 AC 113 ms
10,484 KB
testcase_32 AC 202 ms
10,452 KB
testcase_33 AC 409 ms
18,748 KB
testcase_34 AC 407 ms
22,924 KB
testcase_35 AC 249 ms
15,116 KB
testcase_36 AC 249 ms
14,632 KB
testcase_37 AC 189 ms
9,072 KB
testcase_38 AC 247 ms
10,960 KB
testcase_39 AC 244 ms
11,040 KB
testcase_40 AC 246 ms
10,876 KB
testcase_41 AC 244 ms
11,104 KB
testcase_42 AC 245 ms
10,764 KB
testcase_43 AC 253 ms
38,588 KB
testcase_44 AC 218 ms
26,260 KB
testcase_45 AC 237 ms
36,796 KB
testcase_46 AC 5 ms
8,064 KB
testcase_47 WA -
権限があれば一括ダウンロードができます

ソースコード

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>;

vector<int> ToplogicalSort(vector<vector<int>> &G,vector<int> &Indegree){
    int n = (int)G.size();
    vector<int> res(n);
    queue<int> q;
    for(int i=0;i<n;i++){
        if(Indegree[i] == 0){
            q.push(i);
        }
    }
    int i = 0;
    while(!q.empty()){
        int now = q.front();
        res[i++] = now;
        q.pop();
        for(int v:G[now]){
            --Indegree[v];
            if(Indegree[v] == 0) q.push(v);
        }
    }
    if(i==n) return res;
    else return {-1};
}

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

mint dp[100005];
mint cnt[100005];
int seen[100005];


int main(){
    int n,m;
    cin >> n >> m;
    vector<vector<int>> G(n+1);
    vector<vector<edge>> graph(n+1);
    vector<int> Indegree(n+1,0);
    rep(i,m){
        ll u,v,l,a;
        cin >> u >> v >> l >> a;
        G[u].push_back(v);
        graph[u].push_back(edge{v,l,a});
        Indegree[v]++;
    }
    int n_ = 0;
    map<int,int> temp_real;
    map<int,int> real_temp;
    auto dfs = [&](auto&& dfs,int i) -> void{
        if(seen[i]==1) return;
        seen[i] = 1;
        temp_real[n_]  = i;
        real_temp[i] = n_;
        ++n_;
        for(int j:G[i]){
            if(seen[j]) continue;
            dfs(dfs,j);
        }
    };
    dfs(dfs,0);
    vector<int> Indegree_(n_,0);
    vector<vector<int>> G_(n_);
    rep(i,n+1){
        if(real_temp.count(i) == 0) continue;
        int idx = real_temp[i];
        for(int j:G[i]){
            if(real_temp.count(j) > 0){
                G_[idx].push_back(real_temp[j]);
                Indegree_[real_temp[j]] ++;
            }
        }
    }
    if(real_temp.count(n) == 0){
        cout << 0 << endl;
        return 0;
    }
    vector<int> top = ToplogicalSort(G_,Indegree_);
    if(top[0]==-1){
        cout << "INF" << endl;
        return 0;
    }
    vector<int> topo(n_);
    rep(i,n_){
        topo[i] = temp_real[top[i]];
    }
    dp[0] = 0;
    cnt[0] = 1;
    for(int i:topo){
        for(auto e:graph[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