結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー どららどらら
提出日時 2021-01-23 01:04:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,790 bytes
コンパイル時間 1,964 ms
コンパイル使用メモリ 184,572 KB
実行使用メモリ 343,044 KB
最終ジャッジ日時 2023-08-28 10:27:13
合計ジャッジ時間 15,957 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,904 KB
testcase_01 AC 4 ms
5,836 KB
testcase_02 AC 3 ms
5,752 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 98 ms
12,380 KB
testcase_24 AC 86 ms
9,956 KB
testcase_25 AC 266 ms
19,024 KB
testcase_26 AC 403 ms
24,768 KB
testcase_27 AC 295 ms
19,236 KB
testcase_28 AC 177 ms
15,312 KB
testcase_29 AC 266 ms
18,784 KB
testcase_30 AC 185 ms
15,536 KB
testcase_31 AC 122 ms
13,660 KB
testcase_32 AC 200 ms
17,040 KB
testcase_33 AC 382 ms
22,840 KB
testcase_34 AC 376 ms
23,752 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 194 ms
15,412 KB
testcase_38 TLE -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

class SCC {
  int V;
  std::vector<std::vector<int>> G, rG;
  std::vector<int> vs;
  std::vector<bool> used;
  std::vector<int> cmp;

  void dfs(int v){
    used[v] = true;
    for(int i=0; i<G[v].size(); i++)
      if(!used[G[v][i]]) dfs(G[v][i]);
    vs.push_back(v);
  }
  
  void rdfs(int v, int k){
    used[v] = true;
    cmp[v] = k;
    for(int i=0; i<rG[v].size(); i++)
      if(!used[rG[v][i]]) rdfs(rG[v][i], k);
  }
public:
  SCC(int V):V(V),G(V),rG(V),used(V),cmp(V){}

  int size(){ return V; }
  std::vector<int> get_edge(int from){ return G[from]; }

  void add_edge(int from, int to){
    G[from].push_back(to);
    rG[to].push_back(from);
  }

  // 強連結成分(2点が相互に行き来できるグループ)数
  int scc(){
    for(int i=0; i<used.size(); i++) used[i] = 0;
    vs.clear();
    for(int v=0; v<V; v++)
      if(!used[v]) dfs(v);
    for(int i=0; i<used.size(); i++) used[i] = 0;
    int k = 0;
    for(int i=vs.size()-1; i>=0; i--)
      if(!used[vs[i]]) rdfs(vs[i], k++);
    return k;
  }

  // サイクルがあるか
  bool iscyclic(){ return scc() != V; }

  // 強連結成分番号
  std::vector<int> get_cmp(){ return cmp; }
  
  // 同じ辺を2度通らない、その頂点までの最長経路
  std::vector<int> depth(){
    scc();
    
    std::vector<pair<int,int>> order;
    std::vector<int> dp(V, 1);

    for(int i=0; i<V; i++){
      order.push_back(make_pair(cmp[i],i));
    }
    std::sort(order.begin(), order.end());

    for(int i=0; i<V; i++){
      int node = order[i].second;
      for(int j=0; j<G[node].size(); j++){
        dp[G[node][j]] = max(dp[G[node][j]], dp[node]+1);
      }
    }
    return dp;
  }
};

static const ll MOD = 1000000007;
vector<vector<ll>> G[100005];

ll dp1[100005];
ll dp2[100005];


int main(){
  int N, M;
  cin >> N >> M;
  SCC scc(N+1);
  rep(i,M){
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    scc.add_edge(a,b);
    G[a].push_back({b, c % MOD, d % MOD});
  }

  scc.scc();
  if(scc.iscyclic()){
    cout << "INF" << endl;
    return 0;
  }

  dp2[0] = 1;
  
  queue<int> que;
  que.push(0);
  while(!que.empty()){
    int pos = que.front(); que.pop();
    rep(i,G[pos].size()){
      ll to = G[pos][i][0];
      ll l = G[pos][i][1];
      ll a = G[pos][i][2];
      dp1[to] += (dp1[pos] + ((a * dp2[pos]) % MOD) * l) % MOD;
      dp1[to] %= MOD;
      dp2[to] += (dp2[pos] * a) % MOD;
      dp2[to] %= MOD;
      que.push(to);
    }
  }

  cout << dp1[N] << endl;
  
  return 0;
}

0