160872025-03-30 20:29:55szilAutópálya inflációcpp17Accepted 100/1001.914s5748 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll MOD = 1e9+7;
const int MAXN = 3001;
const int MAXM = 6001;

const int BLOCK_CNT = 50;
const int BLOCK_SIZE = 60;
const int TH = 30;

const ll MULTI = (1ll<<(1+BLOCK_SIZE))%MOD;

struct bigint {

    vector<uint64_t> data;

    bigint() {
        data.assign(BLOCK_CNT, ~0ull);
    }

    void clear() {
        data.assign(BLOCK_CNT, 0ull);
    }

    bool operator<(const bigint &rhs) {
        for (int i = 0; i < BLOCK_CNT; i++) {
            if (data[i] != rhs.data[i]) {
                return data[i] < rhs.data[i];
            }
        }
        return 0;
    }

    bigint operator+(const bigint &rhs) {
        bigint res;
        res.clear();
        bool carry = 0;
        for (int i = BLOCK_CNT-1; i >= 0; i--) {
            res.data[i] = data[i] + rhs.data[i] + carry;
            carry = (res.data[i] >> (BLOCK_SIZE + 1)) & 1;
            res.data[i] &= ~(1ull << (BLOCK_SIZE + 1));
        }

        return res;
    }

    void inflate() {
        bool carry = 0;
        for (int i = BLOCK_CNT-1; i >= 0; i--) {
            bool next_carry = (data[i] >> BLOCK_SIZE) & 1;
            data[i] &= ~(1ull << BLOCK_SIZE);
            data[i] <<= 1;
            data[i] |= carry;
            carry = next_carry;
        }
    }

    ll calc_ans() {
        ll ans = 0;
        ll mul = 1;
        for (int i = BLOCK_CNT-1; i >= 0; i--) {
            data[i] %= MOD;
            ans += data[i] * mul;
            ans %= MOD;
            mul *= MULTI;
            mul %= MOD;
        }
        return ans;
    }
};

int u[MAXM], v[MAXM];

void solve() {
    int n, m; cin >> n >> m;
    vector<bigint> w(m+1);
    for (int i = 1; i <= m; i++) {
        w[i].clear();
        cin >> u[i] >> v[i] >> w[i].data.back();
    }
    vector<bigint> dist_last(n+1);
    vector<int> first_seen(n+1, n);
    vector<bool> vis_last(n+1);
    dist_last[1].clear();
    vis_last[1] = 1;
    int is_ok = -1;
    first_seen[1] = 0;
    for (int i = 1; i <= n; i++) {

        vector<bigint> dist_curr = dist_last;
        vector<bool> vis_curr = vis_last;
        for (int j = 1; j <= m; j++) {
            if (first_seen[u[j]] + TH >= i && first_seen[v[j]] + TH >= i && vis_last[u[j]] && dist_last[u[j]] + w[j] < dist_curr[v[j]]) {
                dist_curr[v[j]] = dist_last[u[j]] + w[j];
                vis_curr[v[j]] = 1;
                first_seen[v[j]] = min(first_seen[v[j]], i);
            }
        }
        for (int j = 1; j <= m; j++) {
            if (first_seen[u[j]] + TH >= i && first_seen[v[j]] + TH >= i && vis_last[v[j]] && dist_last[v[j]] + w[j] < dist_curr[u[j]]) {
                dist_curr[u[j]] = dist_last[v[j]] + w[j];
                vis_curr[u[j]] = 1;
                first_seen[u[j]] = min(first_seen[u[j]], i);
            }
        }

        dist_last = dist_curr;
        vis_last = vis_curr;

        for (int j = 1; j <= m; j++) {
            if (first_seen[u[j]] + TH >= i && first_seen[v[j]] + TH >= i) w[j].inflate();
        }
    }

    for (int i = 2; i <= n; i++) {
        cout << dist_last[i].calc_ans() << "\n";
    }
    cout << "\n";
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1; 
    // cin >> t;
    while (t--) solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
2Accepted1.437s5660 KiB
subtask28/8
3Accepted1.269s4440 KiB
4Accepted1.299s4336 KiB
5Accepted1.307s4148 KiB
6Accepted1.299s4340 KiB
7Accepted1.633s4336 KiB
subtask315/15
8Accepted1ms316 KiB
9Accepted1ms316 KiB
10Accepted1ms316 KiB
11Accepted1ms316 KiB
12Accepted1ms508 KiB
13Accepted1ms316 KiB
14Accepted1ms316 KiB
15Accepted1ms316 KiB
16Accepted1ms316 KiB
17Accepted1ms316 KiB
18Accepted1ms316 KiB
subtask434/34
19Accepted1.411s5660 KiB
20Accepted1.472s5660 KiB
21Accepted1.478s5672 KiB
22Accepted1.427s5660 KiB
23Accepted1.48s5528 KiB
24Accepted1.544s5528 KiB
25Accepted1.402s5432 KiB
26Accepted1.424s5660 KiB
27Accepted1.774s5652 KiB
28Accepted1.347s4204 KiB
29Accepted1.35s4332 KiB
30Accepted1.348s3892 KiB
31Accepted1.322s4148 KiB
32Accepted1.623s5644 KiB
33Accepted1.646s5428 KiB
subtask521/21
34Accepted61ms1076 KiB
35Accepted71ms1588 KiB
36Accepted70ms1772 KiB
37Accepted70ms1588 KiB
38Accepted68ms1740 KiB
39Accepted68ms1736 KiB
40Accepted78ms1588 KiB
41Accepted79ms1732 KiB
42Accepted37ms1076 KiB
43Accepted43ms1076 KiB
44Accepted43ms1076 KiB
45Accepted43ms1080 KiB
46Accepted39ms1076 KiB
47Accepted37ms820 KiB
48Accepted50ms1076 KiB
49Accepted50ms1276 KiB
subtask622/22
50Accepted1.61s5532 KiB
51Accepted1.493s5748 KiB
52Accepted1.475s5688 KiB
53Accepted1.416s5664 KiB
54Accepted1.411s5684 KiB
55Accepted1.442s5684 KiB
56Accepted1.82s5684 KiB
57Accepted1.776s5656 KiB
58Accepted158ms1588 KiB
59Accepted1.365s4332 KiB
60Accepted1.376s4148 KiB
61Accepted1.373s4336 KiB
62Accepted1.378s4148 KiB
63Accepted1.33s4148 KiB
64Accepted1.605s5428 KiB
65Accepted1.618s5428 KiB
66Accepted1.914s5660 KiB