結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー SSRSSSRS
提出日時 2021-01-22 21:55:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 328 ms / 2,500 ms
コード長 2,408 bytes
コンパイル時間 2,029 ms
コンパイル使用メモリ 185,496 KB
実行使用メモリ 22,060 KB
最終ジャッジ日時 2023-08-28 11:02:00
合計ジャッジ時間 11,305 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 12 ms
4,556 KB
testcase_09 AC 6 ms
4,376 KB
testcase_10 AC 13 ms
4,376 KB
testcase_11 AC 11 ms
4,380 KB
testcase_12 AC 14 ms
4,380 KB
testcase_13 AC 177 ms
16,380 KB
testcase_14 AC 248 ms
15,328 KB
testcase_15 AC 242 ms
17,168 KB
testcase_16 AC 185 ms
12,552 KB
testcase_17 AC 86 ms
12,780 KB
testcase_18 AC 319 ms
21,636 KB
testcase_19 AC 319 ms
21,920 KB
testcase_20 AC 319 ms
21,560 KB
testcase_21 AC 314 ms
21,640 KB
testcase_22 AC 328 ms
21,648 KB
testcase_23 AC 69 ms
8,280 KB
testcase_24 AC 73 ms
5,640 KB
testcase_25 AC 182 ms
12,384 KB
testcase_26 AC 293 ms
16,256 KB
testcase_27 AC 218 ms
12,288 KB
testcase_28 AC 129 ms
10,012 KB
testcase_29 AC 207 ms
12,132 KB
testcase_30 AC 135 ms
10,388 KB
testcase_31 AC 92 ms
9,120 KB
testcase_32 AC 166 ms
10,984 KB
testcase_33 AC 272 ms
14,844 KB
testcase_34 AC 246 ms
16,100 KB
testcase_35 AC 234 ms
13,592 KB
testcase_36 AC 228 ms
13,084 KB
testcase_37 AC 165 ms
9,672 KB
testcase_38 AC 228 ms
11,556 KB
testcase_39 AC 225 ms
11,736 KB
testcase_40 AC 228 ms
11,472 KB
testcase_41 AC 224 ms
11,208 KB
testcase_42 AC 226 ms
11,548 KB
testcase_43 AC 115 ms
22,060 KB
testcase_44 AC 88 ms
17,752 KB
testcase_45 AC 111 ms
19,880 KB
testcase_46 AC 4 ms
7,900 KB
testcase_47 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<tuple<int, int, int>>> E1(N + 1);
  vector<vector<tuple<int, int, int>>> E2(N + 1);
  for (int i = 0; i < M; i++){
    int u, v, l, a;
    cin >> u >> v >> l >> a;
    E1[u].push_back(make_tuple(l, a, v));
    E2[v].push_back(make_tuple(l, a, u));
  }
  vector<bool> used1(N + 1, false);
  used1[0] = true;
  queue<int> Q1;
  Q1.push(0);
  while (!Q1.empty()){
    int v = Q1.front();
    Q1.pop();
    for (auto e : E1[v]){
      int w = get<2>(e);
      if (!used1[w]){
        used1[w] = true;
        Q1.push(w);
      }
    }
  }
  vector<bool> used2(N + 1, false);
  used2[N] = true;
  queue<int> Q2;
  Q2.push(N);
  while (!Q2.empty()){
    int v = Q2.front();
    Q2.pop();
    for (auto e : E2[v]){
      int w = get<2>(e);
      if (!used2[w]){
        used2[w] = true;
        Q2.push(w);
      }
    }
  }
  if (!used1[N]){
    cout << 0 << endl;
  } else {
    vector<vector<tuple<int, int, int>>> E3(N + 1);
    for (int i = 0; i <= N; i++){
      if (used1[i] && used2[i]){
        for (auto e : E1[i]){
          int w = get<2>(e);
          if (used1[w] && used2[w]){
            E3[i].push_back(e);
          }
        }
      }
    }
    vector<int> d(N + 1, 0);
    for (int i = 0; i <= N; i++){
      for (auto e : E3[i]){
        d[get<2>(e)]++;
      }
    }
    queue<int> Q;
    for (int i = 0; i <= N; i++){
      if (d[i] == 0){
        Q.push(i);
      }
    }
    vector<int> b;
    while (!Q.empty()){
      int v = Q.front();
      Q.pop();
      b.push_back(v);
      for (auto e : E3[v]){
        int w = get<2>(e);
        d[w]--;
        if (d[w] == 0){
          Q.push(w);
        }
      }
    }
    bool ok = true;
    for (int i = 0; i <= N; i++){
      if (d[i] > 0){
        ok = false;
      }
    }
    if (!ok){
      cout << "INF" << endl;
    } else {
      vector<long long> dp1(N + 1, 0);
      vector<long long> dp2(N + 1, 0);
      dp1[0] = 1;
      for (int v : b){
        for (auto e : E3[v]){
          int l = get<0>(e);
          int a = get<1>(e);
          int w = get<2>(e);
          long long t = dp1[v] * a % MOD;
          dp1[w] += dp1[v] * a;
          dp1[w] %= MOD;
          dp2[w] += (dp2[v] + dp1[v] * l % MOD) * a;
          dp2[w] %= MOD;
        }
      }
      cout << dp2[N] << endl;
    }
  }
}
0