結果
問題 | No.1364 [Renaming] Road to Cherry from Zelkova |
ユーザー | outline |
提出日時 | 2021-01-22 23:27:05 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 121 ms / 2,500 ms |
コード長 | 4,511 bytes |
コンパイル時間 | 1,981 ms |
コンパイル使用メモリ | 158,044 KB |
実行使用メモリ | 23,772 KB |
最終ジャッジ日時 | 2024-06-09 07:33:41 |
合計ジャッジ時間 | 6,590 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 2 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 1 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 6 ms
5,376 KB |
testcase_09 | AC | 4 ms
5,376 KB |
testcase_10 | AC | 7 ms
5,376 KB |
testcase_11 | AC | 5 ms
5,376 KB |
testcase_12 | AC | 6 ms
5,376 KB |
testcase_13 | AC | 72 ms
13,440 KB |
testcase_14 | AC | 100 ms
13,824 KB |
testcase_15 | AC | 93 ms
14,720 KB |
testcase_16 | AC | 65 ms
11,136 KB |
testcase_17 | AC | 34 ms
10,368 KB |
testcase_18 | AC | 119 ms
17,792 KB |
testcase_19 | AC | 121 ms
17,792 KB |
testcase_20 | AC | 121 ms
17,792 KB |
testcase_21 | AC | 121 ms
17,792 KB |
testcase_22 | AC | 120 ms
17,664 KB |
testcase_23 | AC | 26 ms
7,168 KB |
testcase_24 | AC | 24 ms
5,376 KB |
testcase_25 | AC | 68 ms
11,392 KB |
testcase_26 | AC | 107 ms
15,104 KB |
testcase_27 | AC | 75 ms
10,880 KB |
testcase_28 | AC | 49 ms
9,344 KB |
testcase_29 | AC | 70 ms
10,240 KB |
testcase_30 | AC | 51 ms
9,472 KB |
testcase_31 | AC | 34 ms
7,936 KB |
testcase_32 | AC | 54 ms
8,960 KB |
testcase_33 | AC | 100 ms
13,568 KB |
testcase_34 | AC | 97 ms
14,208 KB |
testcase_35 | AC | 89 ms
16,128 KB |
testcase_36 | AC | 86 ms
14,208 KB |
testcase_37 | AC | 53 ms
8,064 KB |
testcase_38 | AC | 75 ms
9,472 KB |
testcase_39 | AC | 74 ms
9,472 KB |
testcase_40 | AC | 74 ms
9,344 KB |
testcase_41 | AC | 74 ms
9,472 KB |
testcase_42 | AC | 74 ms
9,472 KB |
testcase_43 | AC | 54 ms
23,772 KB |
testcase_44 | AC | 36 ms
13,492 KB |
testcase_45 | AC | 49 ms
22,400 KB |
testcase_46 | AC | 6 ms
9,088 KB |
testcase_47 | AC | 2 ms
5,376 KB |
ソースコード
#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 = tuple<int, int, int>; 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, l, a); rg[v].emplace_back(u, 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, weight] : 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, weight] : 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, weight] : 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), dp2(N + 1); dp2[0] = 1; for(int from : topological_order){ for(auto [to, cost, weight] : g[from]){ if(!flag[to]) continue; dp2[to] += dp2[from] * weight; } } for(int from : topological_order){ for(auto [to, cost, weight] : g[from]){ if(!flag[to]) continue; dp[to] += (dp[from] + dp2[from] * cost) * weight; } } cout << dp[N] << endl; return 0; }