結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー どららどらら
提出日時 2021-01-23 01:42:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,602 bytes
コンパイル時間 2,576 ms
コンパイル使用メモリ 191,316 KB
実行使用メモリ 38,204 KB
最終ジャッジ日時 2023-08-28 11:16:10
合計ジャッジ時間 15,895 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,056 KB
testcase_01 AC 4 ms
8,056 KB
testcase_02 AC 3 ms
8,044 KB
testcase_03 AC 4 ms
8,060 KB
testcase_04 AC 4 ms
8,128 KB
testcase_05 AC 3 ms
8,096 KB
testcase_06 AC 3 ms
8,196 KB
testcase_07 AC 4 ms
8,108 KB
testcase_08 AC 19 ms
9,764 KB
testcase_09 AC 11 ms
8,664 KB
testcase_10 AC 20 ms
9,588 KB
testcase_11 AC 17 ms
9,524 KB
testcase_12 AC 20 ms
9,612 KB
testcase_13 AC 288 ms
27,996 KB
testcase_14 AC 400 ms
30,416 KB
testcase_15 AC 390 ms
31,328 KB
testcase_16 AC 280 ms
24,416 KB
testcase_17 AC 132 ms
20,720 KB
testcase_18 AC 484 ms
36,876 KB
testcase_19 AC 487 ms
36,552 KB
testcase_20 AC 486 ms
37,032 KB
testcase_21 AC 484 ms
36,784 KB
testcase_22 AC 492 ms
36,876 KB
testcase_23 AC 102 ms
16,088 KB
testcase_24 AC 87 ms
12,976 KB
testcase_25 AC 277 ms
24,124 KB
testcase_26 AC 436 ms
31,076 KB
testcase_27 AC 301 ms
24,152 KB
testcase_28 AC 191 ms
19,880 KB
testcase_29 AC 283 ms
23,252 KB
testcase_30 AC 199 ms
20,056 KB
testcase_31 AC 136 ms
18,108 KB
testcase_32 AC 209 ms
21,096 KB
testcase_33 AC 411 ms
28,804 KB
testcase_34 AC 402 ms
30,460 KB
testcase_35 AC 399 ms
32,444 KB
testcase_36 AC 390 ms
31,420 KB
testcase_37 AC 199 ms
19,760 KB
testcase_38 AC 279 ms
24,308 KB
testcase_39 AC 278 ms
24,436 KB
testcase_40 AC 277 ms
24,340 KB
testcase_41 AC 277 ms
24,172 KB
testcase_42 AC 276 ms
24,232 KB
testcase_43 AC 159 ms
38,204 KB
testcase_44 AC 116 ms
29,280 KB
testcase_45 WA -
testcase_46 AC 13 ms
14,576 KB
testcase_47 AC 4 ms
8,124 KB
権限があれば一括ダウンロードができます

ソースコード

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];
vector<ll> rG[100005];

bool cango1[100005];
bool cango2[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});
    rG[b].push_back(a);
  }

  queue<int> que;
  que.push(N);
  while(!que.empty()){
    int pos = que.front(); que.pop();
    if(cango1[pos]) continue;
    cango1[pos] = true;
    rep(i,rG[pos].size()){
      que.push(rG[pos][i]);
    }
  }
  que.push(0);
  while(!que.empty()){
    int pos = que.front(); que.pop();
    if(cango2[pos]) continue;
    cango2[pos] = true;
    rep(i,G[pos].size()){
      que.push(G[pos][i][0]);
    }
  }

  scc.scc();

  vector<int> cmp = scc.get_cmp();
  vector<pair<int,int>> order;
  for(int i=0; i<cmp.size(); i++){
    order.push_back(make_pair(cmp[i],i));
  }
  sort(order.begin(), order.end());

  vector<bool> visited(N+1,false);
  visited[0] = true;
  
  dp2[0] = 1;
  rep(ii,order.size()){
    int pos = order[ii].second;
    if(!cango1[pos] || !cango2[pos]) continue;
    visited[pos] = true;

    if(pos == N) break;
    rep(i,G[pos].size()){
      ll to = G[pos][i][0];
      ll l = G[pos][i][1];
      ll a = G[pos][i][2];

      if(visited[to]){
        cout << "INF" << endl;
        return 0;
      }

      dp1[to] += ((dp1[pos] * a)%MOD + (((a * dp2[pos]) % MOD) * l) % MOD) % MOD;
      dp1[to] %= MOD;
      dp2[to] += (dp2[pos] * a) % MOD;
      dp2[to] %= MOD;
    }
  }

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


0