結果
問題 | No.1364 [Renaming] Road to Cherry from Zelkova |
ユーザー | Haa |
提出日時 | 2021-01-22 23:00:42 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 352 ms / 2,500 ms |
コード長 | 1,867 bytes |
コンパイル時間 | 2,219 ms |
コンパイル使用メモリ | 181,292 KB |
実行使用メモリ | 20,240 KB |
最終ジャッジ日時 | 2024-12-29 09:13:07 |
合計ジャッジ時間 | 12,188 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 45 |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll,ll> P; typedef vector<ll> VI; typedef vector<VI> VVI; #define REP(i,n) for(int i=0;i<(n);i++) #define ALL(v) v.begin(),v.end() constexpr ll MOD=1000000007; constexpr ll INF=1e18; struct E{ int to; ll l; ll a; }; int n, m; int M=110000; vector<vector<E>> edge(M,vector<E>(0)); vector<bool> lop(M,true); VI inc(M,0); VI ways(M,0); void dag(){ VI h(n,0); REP(i,n){ for(auto nx:edge[i]){ h[nx.to]++; } } queue<int> q; REP(i,n){ if(h[i]==0){ q.push(i); } } while(!q.empty()){ int v=q.front(); lop[v]=0; q.pop(); for(auto nx:edge[v]){ if(--h[nx.to]==0){ q.push(nx.to); } } } } bool ok=1; void initdfs(int v){ if(v==n-1) ok=0; for(auto nx:edge[v]){ inc[nx.to]++; if(inc[nx.to]==1) initdfs(nx.to); } } void initdfs2(int v){ for(auto nx:edge[v]){ inc[nx.to]--; if(lop[v]) lop[nx.to]=1; if(inc[nx.to]==0) initdfs2(nx.to); } } VI ww(M,0), lp(M,0); void dfs(int v){ for(auto nx:edge[v]){ ww[nx.to]=(ww[nx.to]+ww[v]*nx.a%MOD+lp[v]*nx.a%MOD*nx.l%MOD)%MOD; lp[nx.to]=(lp[nx.to]+lp[v]*nx.a)%MOD; if(--inc[nx.to]==0) dfs(nx.to); } } int main(){ cin >> n >> m; n++; int u, v, l, a; REP(i,m){ cin >> u >> v >> l >> a; edge[u].push_back(E{v,l,a}); } dag(); initdfs(0); initdfs2(0); if(lop[n-1]){ if(!ok) cout << "INF" << endl; if(ok) cout << 0 << endl; return 0; } inc[0]++; initdfs(0); lp[0]=1; dfs(0); cout << ww[n-1] << endl; return 0; }