結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー kappybarkappybar
提出日時 2021-01-23 17:05:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 6,748 bytes
コンパイル時間 2,227 ms
コンパイル使用メモリ 194,260 KB
実行使用メモリ 37,404 KB
最終ジャッジ日時 2023-08-30 01:35:41
合計ジャッジ時間 18,098 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 14 ms
4,760 KB
testcase_09 AC 7 ms
4,380 KB
testcase_10 AC 14 ms
4,384 KB
testcase_11 AC 12 ms
4,404 KB
testcase_12 AC 15 ms
4,384 KB
testcase_13 AC 224 ms
19,388 KB
testcase_14 AC 284 ms
17,384 KB
testcase_15 AC 289 ms
20,072 KB
testcase_16 AC 204 ms
14,324 KB
testcase_17 AC 108 ms
14,824 KB
testcase_18 AC 385 ms
26,000 KB
testcase_19 AC 387 ms
25,676 KB
testcase_20 AC 388 ms
25,744 KB
testcase_21 AC 385 ms
25,740 KB
testcase_22 AC 388 ms
25,824 KB
testcase_23 AC 78 ms
9,228 KB
testcase_24 AC 76 ms
5,676 KB
testcase_25 AC 214 ms
13,744 KB
testcase_26 AC 336 ms
18,188 KB
testcase_27 AC 248 ms
13,032 KB
testcase_28 AC 151 ms
10,888 KB
testcase_29 AC 236 ms
12,188 KB
testcase_30 AC 156 ms
11,024 KB
testcase_31 AC 104 ms
9,740 KB
testcase_32 AC 181 ms
10,352 KB
testcase_33 AC 318 ms
16,388 KB
testcase_34 AC 305 ms
18,152 KB
testcase_35 WA -
testcase_36 AC 322 ms
19,972 KB
testcase_37 AC 182 ms
9,300 KB
testcase_38 AC 242 ms
11,800 KB
testcase_39 AC 242 ms
11,336 KB
testcase_40 AC 245 ms
11,724 KB
testcase_41 AC 244 ms
11,364 KB
testcase_42 AC 242 ms
11,660 KB
testcase_43 AC 157 ms
37,404 KB
testcase_44 AC 126 ms
20,836 KB
testcase_45 AC 145 ms
30,320 KB
testcase_46 AC 36 ms
16,140 KB
testcase_47 AC 2 ms
4,380 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;


#include <algorithm>

#include <algorithm>
#include <utility>
#include <vector>

namespace atcoder {
namespace internal {

template <class E> struct csr {
    std::vector<int> start;
    std::vector<E> elist;
    csr(int n, const std::vector<std::pair<int, E>>& edges)
        : start(n + 1), elist(edges.size()) {
        for (auto e : edges) {
            start[e.first + 1]++;
        }
        for (int i = 1; i <= n; i++) {
            start[i] += start[i - 1];
        }
        auto counter = start;
        for (auto e : edges) {
            elist[counter[e.first]++] = e.second;
        }
    }
};

// Reference:
// R. Tarjan,
// Depth-First Search and Linear Graph Algorithms
struct scc_graph {
  public:
    scc_graph(int n) : _n(n) {}

    int num_vertices() { return _n; }

    void add_edge(int from, int to) { edges.push_back({from, {to}}); }

    // @return pair of (# of scc, scc id)
    std::pair<int, std::vector<int>> scc_ids() {
        auto g = csr<edge>(_n, edges);
        int now_ord = 0, group_num = 0;
        std::vector<int> visited, low(_n), ord(_n, -1), ids(_n);
        visited.reserve(_n);
        auto dfs = [&](auto self, int v) -> void {
            low[v] = ord[v] = now_ord++;
            visited.push_back(v);
            for (int i = g.start[v]; i < g.start[v + 1]; i++) {
                auto to = g.elist[i].to;
                if (ord[to] == -1) {
                    self(self, to);
                    low[v] = std::min(low[v], low[to]);
                } else {
                    low[v] = std::min(low[v], ord[to]);
                }
            }
            if (low[v] == ord[v]) {
                while (true) {
                    int u = visited.back();
                    visited.pop_back();
                    ord[u] = _n;
                    ids[u] = group_num;
                    if (u == v) break;
                }
                group_num++;
            }
        };
        for (int i = 0; i < _n; i++) {
            if (ord[i] == -1) dfs(dfs, i);
        }
        for (auto& x : ids) {
            x = group_num - 1 - x;
        }
        return {group_num, ids};
    }

    std::vector<std::vector<int>> scc() {
        auto ids = scc_ids();
        int group_num = ids.first;
        std::vector<int> counts(group_num);
        for (auto x : ids.second) counts[x]++;
        std::vector<std::vector<int>> groups(ids.first);
        for (int i = 0; i < group_num; i++) {
            groups[i].reserve(counts[i]);
        }
        for (int i = 0; i < _n; i++) {
            groups[ids.second[i]].push_back(i);
        }
        return groups;
    }

  private:
    int _n;
    struct edge {
        int to;
    };
    std::vector<std::pair<int, edge>> edges;
};

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

struct scc_graph {
  public:
    scc_graph() : internal(0) {}
    scc_graph(int n) : internal(n) {}

    void add_edge(int from, int to) {
        int n = internal.num_vertices();
        assert(0 <= from && from < n);
        assert(0 <= to && to < n);
        internal.add_edge(from, to);
    }

    std::vector<std::vector<int>> scc() { return internal.scc(); }

  private:
    internal::scc_graph internal;
};

}  // namespace atcoder

using namespace atcoder;

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

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