結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー 👑 NachiaNachia
提出日時 2021-01-22 23:47:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 165 ms / 2,500 ms
コード長 1,350 bytes
コンパイル時間 2,252 ms
コンパイル使用メモリ 208,900 KB
実行使用メモリ 20,748 KB
最終ジャッジ日時 2023-08-28 12:11:04
合計ジャッジ時間 8,600 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,340 KB
testcase_01 AC 3 ms
8,204 KB
testcase_02 AC 3 ms
8,196 KB
testcase_03 AC 4 ms
8,188 KB
testcase_04 AC 3 ms
8,268 KB
testcase_05 AC 3 ms
8,408 KB
testcase_06 AC 3 ms
8,332 KB
testcase_07 AC 4 ms
8,532 KB
testcase_08 AC 8 ms
9,112 KB
testcase_09 AC 6 ms
8,644 KB
testcase_10 AC 9 ms
8,988 KB
testcase_11 AC 8 ms
8,968 KB
testcase_12 AC 9 ms
8,992 KB
testcase_13 AC 94 ms
16,900 KB
testcase_14 AC 129 ms
18,520 KB
testcase_15 AC 126 ms
18,732 KB
testcase_16 AC 85 ms
15,808 KB
testcase_17 AC 42 ms
12,960 KB
testcase_18 AC 157 ms
20,744 KB
testcase_19 AC 160 ms
20,732 KB
testcase_20 AC 165 ms
20,748 KB
testcase_21 AC 157 ms
20,696 KB
testcase_22 AC 158 ms
20,636 KB
testcase_23 AC 32 ms
10,800 KB
testcase_24 AC 30 ms
10,144 KB
testcase_25 AC 80 ms
14,016 KB
testcase_26 AC 139 ms
17,208 KB
testcase_27 AC 93 ms
15,748 KB
testcase_28 AC 58 ms
12,556 KB
testcase_29 AC 88 ms
15,632 KB
testcase_30 AC 59 ms
12,996 KB
testcase_31 AC 41 ms
11,664 KB
testcase_32 AC 69 ms
14,888 KB
testcase_33 AC 123 ms
16,648 KB
testcase_34 AC 115 ms
15,608 KB
testcase_35 AC 126 ms
18,004 KB
testcase_36 AC 116 ms
17,560 KB
testcase_37 AC 67 ms
13,588 KB
testcase_38 AC 87 ms
15,032 KB
testcase_39 AC 87 ms
15,004 KB
testcase_40 AC 89 ms
15,096 KB
testcase_41 AC 88 ms
15,152 KB
testcase_42 AC 89 ms
14,932 KB
testcase_43 AC 53 ms
16,584 KB
testcase_44 AC 45 ms
16,872 KB
testcase_45 AC 49 ms
14,908 KB
testcase_46 AC 7 ms
10,872 KB
testcase_47 AC 3 ms
8,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

const ULL M=1000000007;

struct Edge{
  int to;
  ULL d,t;
};

int N,n;
vector<Edge> E[100001];
vector<int> rE[100001];
bool reach[100001];
int I[100001];
vector<int> Ord;
ULL dpc[100001];
ULL dp[100001];

int main() {
  scanf("%d%d",&N,&n); N++;
  rep(i,N) I[i]=0;
  rep(i,n){
    int u,v,d,t; scanf("%d%d%d%d",&u,&v,&d,&t);
    E[u].push_back({v,(ULL)d,(ULL)t});
    rE[v].push_back(u);
    I[v]++;
  }
  {
    queue<int> Q;
    rep(i,N) reach[i]=false;
    reach[0]=true; Q.push(0);
    while(Q.size()){
      int p=Q.front(); Q.pop();
      for(Edge e:E[p]) if(!reach[e.to]) { reach[e.to]=true; Q.push(e.to); }
    }
    rep(i,N) if(!reach[i]){ for(Edge e:E[i]) I[e.to]--; E[i].clear(); }

    rep(i,N) if(I[i]==0) Q.push(i);
    vector<int> Iin(N);
    while(Q.size()){
      int p=Q.front(); Q.pop();
      Ord.push_back(p);
      Iin[p]=1;
      for(auto& e:E[p]) if((--I[e.to])==0) Q.push(e.to);
    }
    if(Iin[0]==0 || Iin[N-1]==0){ printf("INF\n"); return 0; }
  }
  rep(i,N) dp[i]=dpc[i]=0;
  dpc[0]=1;
  for(int p:Ord){
    for(Edge e:E[p]){
      dpc[e.to] = (dpc[e.to]+dpc[p]*e.t)%M;
      dp[e.to] = (dp[e.to]+(dp[p]+e.d*dpc[p])%M*e.t)%M;
    }
  }
  printf("%llu\n",dp[N-1]);
  return 0;
}
0