結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー outlineoutline
提出日時 2021-01-22 22:41:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,247 bytes
コンパイル時間 2,098 ms
コンパイル使用メモリ 153,692 KB
実行使用メモリ 23,152 KB
最終ジャッジ日時 2023-08-27 20:45:24
合計ジャッジ時間 8,656 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 39 ms
7,708 KB
testcase_24 AC 27 ms
5,404 KB
testcase_25 AC 115 ms
12,688 KB
testcase_26 AC 184 ms
17,044 KB
testcase_27 AC 106 ms
12,852 KB
testcase_28 AC 75 ms
10,124 KB
testcase_29 AC 94 ms
12,004 KB
testcase_30 AC 84 ms
10,308 KB
testcase_31 AC 53 ms
8,628 KB
testcase_32 AC 72 ms
10,576 KB
testcase_33 AC 158 ms
15,380 KB
testcase_34 AC 160 ms
15,940 KB
testcase_35 AC 147 ms
17,164 KB
testcase_36 AC 136 ms
15,496 KB
testcase_37 AC 65 ms
9,616 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 64 ms
23,152 KB
testcase_44 AC 42 ms
13,272 KB
testcase_45 AC 59 ms
22,224 KB
testcase_46 AC 6 ms
8,400 KB
testcase_47 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

struct mint {
    int x;
    mint() : x(0) {}
    mint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
    mint& operator+=(const mint& p){
        if((x += p.x) >= mod)   x -= mod;
        return *this;
    }
    mint& operator-=(const mint& p){
        if((x -= p.x) < 0)  x += mod;
        return *this;
    }
    mint& operator*=(const mint& p){
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }
    mint& operator/=(const mint& p){
        *this *= p.inverse();
        return *this;
    }
    mint operator-() const { return mint(-x); }
    mint operator+(const mint& p) const { return mint(*this) += p; }
    mint operator-(const mint& p) const { return mint(*this) -= p; }
    mint operator*(const mint& p) const { return mint(*this) *= p; }
    mint operator/(const mint& p) const { return mint(*this) /= p; }
    bool operator==(const mint& p) const { return x == p.x; }
    bool operator!=(const mint& p) const { return x != p.x; }
    mint pow(int64_t n) const {
        mint res = 1, mul = x;
        while(n > 0){
            if(n & 1)   res *= mul;
            mul *= mul;
            n >>= 1;
        }
        return res;
    }
    mint inverse() const { return pow(mod - 2); }
    friend ostream& operator<<(ostream& os, const mint& p){
        return os << p.x;
    }
    friend istream& operator>>(istream& is, mint& p){
        int64_t val;
        is >> val;
        p = mint(val);
        return is;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;
    using edge = pair<int, ll>;
    vector<vector<edge>> g(N + 1), rg(N + 1);
    for(int i = 0; i < M; ++i){
        int u, v, l, a;
        cin >> u >> v >> l >> a;
        g[u].emplace_back(v, (ll)l * a);
        rg[v].emplace_back(u, (ll)l * a);
    }

    vector<bool> from0(N + 1), fromN(N + 1), visited(N + 1);
    auto dfs = [&](auto&& self, vector<vector<edge>>& G, vector<bool>& f, int cur) -> void {
        visited[cur] = true;
        f[cur] = true;
        for(auto [nxt, cost] : G[cur]){
            if(!visited[nxt])   self(self, G, f, nxt);
        }
    };
    dfs(dfs, g, from0, 0);
    visited.assign(N + 1, false);
    dfs(dfs, rg, fromN, N);

    int V = 0;
    vector<bool> flag(N + 1);
    for(int i = 0; i <= N; ++i){
        flag[i] = from0[i] && fromN[i];
        V += flag[i];
    }
    vector<int> indeg(N + 1);
    for(int from = 0; from <= N; ++from){
        if(!flag[from]) continue;
        for(auto [to, cost] : g[from]){
            if(flag[to])    indeg[to] += 1;
        }
    }
    
    queue<int> que;
    for(int i = 0; i <= N; ++i){
        if(flag[i] && indeg[i] == 0)    que.emplace(i);
    }
    vector<int> topological_order;
    while(!que.empty()){
        int from = que.front();
        que.pop();
        topological_order.emplace_back(from);
        for(auto [to, cost] : g[from]){
            if(!flag[to])   continue;
            if(--indeg[to] == 0)    que.emplace(to);
        }
    }

    if((int)topological_order.size() != V){
        cout << "INF\n";
        return 0;
    }

    vector<mint> dp(N + 1);
    for(int from : topological_order){
        for(auto [to, cost] : g[from]){
            if(!flag[to])   continue;
            dp[to] += dp[from] + cost;
        }
    }

    cout << dp[N] << endl;

    return 0;
}
0