結果
問題 | No.1364 [Renaming] Road to Cherry from Zelkova |
ユーザー |
![]() |
提出日時 | 2021-01-24 16:41:39 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 209 ms / 2,500 ms |
コード長 | 1,833 bytes |
コンパイル時間 | 2,765 ms |
コンパイル使用メモリ | 211,260 KB |
最終ジャッジ日時 | 2025-01-18 07:51:46 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 45 |
ソースコード
#include "bits/stdc++.h" using namespace std; using ll = long long; using P = pair<ll, ll>; const ll INF = (1LL << 61); ll mod = (ll)1e9 + 7; ll N, M; vector<pair<ll, P>>E1[100010], E2[100010], G[100010]; ll dp[100010], dp2[100010]; signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M; for (int i = 0; i < M; i++) { ll u, v, l, a; cin >> u >> v >> l >> a; E1[u].push_back({ v, {l, a} }); E2[v].push_back({ u, {l, a} }); } vector<bool>used(N+1), used2(N+1); used[0] = true; queue<int>q; q.push(0); while (!q.empty()) { int v = q.front(); q.pop(); for (auto e : E1[v]) { if (!used[e.first]) { used[e.first] = true; q.push(e.first); } } } used2[N] = true; queue<int>q2; q2.push(N); while (!q2.empty()) { int v = q2.front(); q2.pop(); for (auto e : E2[v]) { if (!used2[e.first]) { used2[e.first] = true; q2.push(e.first); } } } for (int i = 0; i <= N; i++) { if (used[i] && used2[i]) { for (auto e : E1[i]) { if (used[e.first] && used2[e.first]) { G[i].push_back(e); } } } } vector<ll>ans; vector<ll>ind(N + 1); for (int i = 0; i <= N; i++) { for (auto e : G[i]) { ind[e.first]++; } } queue<ll>que; for (int i = 0; i <= N; i++) { if (ind[i] == 0)que.push(i); } while (!que.empty()) { ll now = que.front(); ans.push_back(now); que.pop(); for (auto e : G[now]) { ind[e.first]--; if (ind[e.first] == 0) { que.push(e.first); } } } if (ans.size() != N + 1) { cout << "INF" << endl; return 0; } dp[0] = 1; dp2[0] = 0; for (auto v : ans) { for (auto e : G[v]) { dp[e.first] += dp[v] * e.second.second; dp[e.first] %= mod; dp2[e.first] += dp2[v] * e.second.second + (dp[v] * e.second.first % mod) * e.second.second; dp2[e.first] %= mod; } } cout << dp2[N] << endl; return 0; }