104842024-04-03 11:33:15Valaki2Autópálya inflációcpp17Time limit exceeded 15/1002.101s84476 KiB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC target ("avx2")
#pragma GCC optimize("O3","unroll-loops")

#define distance distance1337

#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 3100;
const int mod = 1e9 + 7;
const int k = 32; // log2 max c_i + epsilon

struct distance {
    bitset<1 + maxn> bs;
    distance() {
        bs = 0;
    }
    distance(int num) {
        if(num == -1) {
            bs.set();
        } else {
            bs = num;
        }
    }
    distance(bitset<1 + maxn> bs_) {
        bs = bs_;
    }
    bool operator < (const distance &other) const {
        // reversed
        for(int i = maxn; i >= 0; i--) {
            if(other.bs[i] ^ bs[i]) {
                if(other.bs[i] > bs[i]) {
                    return false;
                } else {
                    return true;
                }
            }
        }
        return false;
    }
    // debug
    void print() {
        cout << bs.to_ullong();
    }
};

const distance inf = distance(-1);

int n, m;
vector<pii > graph[1 + maxn];
distance dist[1 + maxn][1 + k];
bool done[1 + maxn][1 + k];
int steps[1 + maxn];

void solve() {
    cin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        graph[a].pb(mp(b, c));
        graph[b].pb(mp(a, c));
    }
    queue<int> bfsq;
    bfsq.push(1);
    while(!bfsq.empty()) {
        int cur = bfsq.front();
        bfsq.pop();
        for(pii nei : graph[cur]) {
            if(nei.fi != 1 && steps[nei.fi] == 0) {
                bfsq.push(nei.fi);
                steps[nei.fi] = steps[cur] + 1;
            }
        }
    }
    priority_queue<pair<distance, pii > > q;
    for(int i = 1; i <= n; i++) {
        for(int j = 0; j <= k; j++) {
            dist[i][j] = distance(-1);
        }
    }
    dist[1][0].bs = 0;
    q.push(mp(dist[1][0], mp(1, 0)));
    while(!q.empty()) {
        pii curpair = q.top().se;
        q.pop();
        int cur = curpair.fi, cur_steps = curpair.se;
        int cur_idx = cur_steps - steps[cur];
        // cout << cur << " " << cur_steps << " " << cur_idx << ":\n";
        if(done[cur][cur_idx]) {
            continue;
        }
        done[cur][cur_idx] = true;
        if(cur_steps >= n) {
            continue;
        }
        for(pii nei : graph[cur]) {
            int nei_idx = cur_steps + 1 - steps[nei.fi];
            if(nei_idx > k) {
                continue;
            }
            bitset<1 + maxn> tmp = (bitset<1 + maxn>(nei.se) << cur_steps);
            int carry = 0;
            for(int i = 0; i <= maxn; i++) {
                int sum = dist[cur][cur_idx].bs[i] + tmp[i] + carry;
                carry = sum / 2;
                tmp[i] = sum % 2;
            }
            distance neidist = distance(tmp);
            // distance neidist = distance(dist[cur][cur_idx].bs + (bitset<1 + maxn>(nei.se) << cur_steps));
            // reversed
            if(dist[nei.fi][nei_idx] < neidist) {
                dist[nei.fi][nei_idx] = neidist;
                q.push(mp(neidist, mp(nei.fi, cur_steps + 1)));
            }
        }
    }
    /*for(int i = 1; i <= n; i++) {
        for(int j = 0; j <= k; j++) {
            if(dist[i][j].bs[maxn] == 0) {
                dist[i][j].print();
            } else {
                cout << "-1";
            }
            cout << " ";
        }
        cout << "\n";
    }*/
    for(int i = 2; i <= n; i++) {
        distance ans_bs(-1);
        for(int j = 0; j <= k; j++) {
            ans_bs = max(ans_bs, dist[i][j]);
        }
        int ans = 0;
        int coeff = 1;
        for(int i = 0; i <= maxn; i++) {
            ans += coeff * ans_bs.bs[i];
            ans %= mod;
            coeff = (2 * coeff) % mod;
                    }
        cout << ans % mod << "\n";
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted35ms80584 KiB
2Time limit exceeded2.061s47840 KiB
subtask20/8
3Time limit exceeded2.075s42148 KiB
4Time limit exceeded2.036s42328 KiB
5Time limit exceeded2.068s42248 KiB
6Time limit exceeded2.072s44980 KiB
7Time limit exceeded2.063s41588 KiB
subtask315/15
8Accepted37ms81604 KiB
9Accepted41ms81476 KiB
10Accepted43ms81768 KiB
11Accepted43ms81832 KiB
12Accepted52ms81984 KiB
13Accepted52ms82192 KiB
14Accepted46ms82080 KiB
15Accepted43ms82296 KiB
16Accepted39ms82532 KiB
17Accepted41ms82852 KiB
18Accepted43ms82708 KiB
subtask40/34
19Time limit exceeded2.073s46664 KiB
20Time limit exceeded2.068s45044 KiB
21Time limit exceeded2.056s45348 KiB
22Time limit exceeded2.061s46840 KiB
23Time limit exceeded2.059s46832 KiB
24Time limit exceeded2.058s43720 KiB
25Time limit exceeded2.076s46792 KiB
26Time limit exceeded2.049s46708 KiB
27Time limit exceeded2.079s43348 KiB
28Time limit exceeded2.016s43732 KiB
29Time limit exceeded2.085s43736 KiB
30Time limit exceeded2.076s43452 KiB
31Time limit exceeded2.075s43636 KiB
32Time limit exceeded2.101s43780 KiB
33Time limit exceeded2.052s43760 KiB
subtask50/21
34Time limit exceeded2.072s43828 KiB
35Time limit exceeded2.059s45240 KiB
36Time limit exceeded2.028s46952 KiB
37Time limit exceeded2.065s46860 KiB
38Time limit exceeded2.068s46956 KiB
39Time limit exceeded2.049s46884 KiB
40Time limit exceeded2.076s46764 KiB
41Time limit exceeded2.079s46744 KiB
42Accepted1.105s83808 KiB
43Accepted1.003s83736 KiB
44Accepted994ms83740 KiB
45Accepted991ms83624 KiB
46Accepted1.07s83800 KiB
47Accepted1.031s83792 KiB
48Accepted1.478s84216 KiB
49Accepted1.468s84476 KiB
subtask60/22
50Time limit exceeded2.069s44608 KiB
51Time limit exceeded2.075s47660 KiB
52Time limit exceeded2.073s50624 KiB
53Time limit exceeded2.033s50796 KiB
54Time limit exceeded2.065s57124 KiB
55Time limit exceeded2.073s57380 KiB
56Time limit exceeded2.081s44756 KiB
57Time limit exceeded2.039s44828 KiB
58Accepted1.875s84104 KiB
59Time limit exceeded2.076s44236 KiB
60Time limit exceeded2.068s44036 KiB
61Time limit exceeded2.075s44144 KiB
62Time limit exceeded2.063s43856 KiB
63Time limit exceeded2.084s44232 KiB
64Time limit exceeded2.068s44340 KiB
65Time limit exceeded2.069s44232 KiB
66Time limit exceeded2.055s43976 KiB