結果

問題 No.1111 コード進行
ユーザー jjjjjjjtgpptmjjjjjjjjjtgpptmjj
提出日時 2020-07-10 22:50:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 4,080 bytes
コンパイル時間 2,497 ms
コンパイル使用メモリ 211,096 KB
実行使用メモリ 294,096 KB
最終ジャッジ日時 2023-08-01 18:54:08
合計ジャッジ時間 11,339 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
291,836 KB
testcase_01 AC 119 ms
292,148 KB
testcase_02 AC 119 ms
291,920 KB
testcase_03 AC 119 ms
291,860 KB
testcase_04 AC 119 ms
291,956 KB
testcase_05 AC 120 ms
291,848 KB
testcase_06 AC 119 ms
291,896 KB
testcase_07 AC 118 ms
291,860 KB
testcase_08 AC 118 ms
291,940 KB
testcase_09 AC 118 ms
291,964 KB
testcase_10 AC 118 ms
291,868 KB
testcase_11 AC 118 ms
291,864 KB
testcase_12 AC 121 ms
292,160 KB
testcase_13 AC 119 ms
291,932 KB
testcase_14 AC 120 ms
292,468 KB
testcase_15 AC 117 ms
291,864 KB
testcase_16 AC 119 ms
292,032 KB
testcase_17 AC 119 ms
291,940 KB
testcase_18 AC 120 ms
291,848 KB
testcase_19 AC 122 ms
291,852 KB
testcase_20 AC 121 ms
291,860 KB
testcase_21 AC 119 ms
291,864 KB
testcase_22 AC 118 ms
291,872 KB
testcase_23 AC 117 ms
292,152 KB
testcase_24 AC 119 ms
291,864 KB
testcase_25 AC 122 ms
291,988 KB
testcase_26 AC 124 ms
292,580 KB
testcase_27 AC 119 ms
291,884 KB
testcase_28 AC 118 ms
291,904 KB
testcase_29 AC 119 ms
291,872 KB
testcase_30 AC 155 ms
292,392 KB
testcase_31 AC 118 ms
291,992 KB
testcase_32 AC 179 ms
292,440 KB
testcase_33 AC 204 ms
294,020 KB
testcase_34 AC 123 ms
291,964 KB
testcase_35 AC 118 ms
292,068 KB
testcase_36 AC 120 ms
291,868 KB
testcase_37 AC 122 ms
291,908 KB
testcase_38 AC 163 ms
292,368 KB
testcase_39 AC 273 ms
294,096 KB
testcase_40 AC 283 ms
292,848 KB
testcase_41 AC 117 ms
291,852 KB
testcase_42 AC 118 ms
292,120 KB
testcase_43 AC 239 ms
293,048 KB
testcase_44 AC 117 ms
291,872 KB
testcase_45 AC 118 ms
291,856 KB
testcase_46 AC 119 ms
291,964 KB
testcase_47 AC 119 ms
291,984 KB
testcase_48 AC 126 ms
292,124 KB
testcase_49 AC 120 ms
292,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }
#define dump(x) cerr<<#x<<": "<<x<<endl;
#define bit(k) (1LL<<(k))
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << "{" <<p.first << ", " << p.second << "}";
  return os;
}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18+1;
const ll MOD = (ll)1e9+7;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

struct mint {
    long long x;
    mint(long long _x=0):x((_x%MOD+MOD)%MOD){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint modpow(long long t) const {
        if (!t) return 1;
        mint a = modpow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime MOD
    mint inv() const {
        return modpow(MOD-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
    friend std::ostream& operator<<(std::ostream& os, const mint& a){
        os << a.x;
        return os;
    }
};
// dp[i][j]:i個決めたときの複雑さjの個数
// ans = dp[N][K];
// その前がなんのコードだったかを持たないといけないからdp[i][j][k]になるか?
// 300 ^ 4になって間に合わない
// コード進行は有向グラフに落ちるので、そこからなんかできないかなー

// bfsで探索しながら、N-1回遷移したら終わる感じにしたらどう?
const int sz = 333;
mint dp[sz][sz][sz];
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    ll N, M, K;
    cin >> N >> M >> K;
    vvec<l_l> G(sz);
    rep(i,M){
        ll p, q, c;
        cin >> p >> q >> c;
        p--, q--;
        G[p].push_back({q, c});
    }
    rep(i,sz)rep(j,sz)rep(k,sz)dp[i][j][k] = 0;

    mint ans = 0;

    /* calc */
    // {頂点,移動回数,複雑度合計,個数}
    queue<pair<l_l,pair<ll, mint>>> q;
    rep(i,sz)q.push({{i, 1}, {0, 1}});
    rep(i,sz)dp[i][1][0] =1;
    while(not q.empty()){
        auto tmp = q.front();
        q.pop();
        int cur = tmp.first.first;
        int cnt = tmp.first.second;
        ll sum = tmp.second.first;
        mint dp_sum = tmp.second.second;
        if(cnt == N)continue;
        if(dp_sum.x != dp[cur][cnt][sum].x)continue;
        for(auto &p: G[cur]){
            int nxt = p.first;
            int C = p.second;
            if(sum + C > K)continue;
            dp[nxt][cnt+1][sum + C] += dp_sum;
            q.push({{nxt, cnt+1},{sum + C, dp[nxt][cnt+1][sum + C]}});
        }
    }
    rep(i,sz){
        ans += dp[i][N][K];
    }
    cout << ans << endl;
}
0