104872024-04-03 11:53:21Valaki2Autópálya inflációcpp17Runtime error 36/1001.363s8776 KiB
#include <bits/stdc++.h>
using namespace std;

#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 = 600;
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<int, 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(0, mp(dist[1][0], mp(1, 0))));
    while(!q.empty()) {
        pii curpair = q.top().se.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(-(cur_steps + 1), 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
1Accepted4ms5064 KiB
2Runtime error4ms5344 KiB
subtask20/8
3Runtime error4ms5568 KiB
4Runtime error4ms5772 KiB
5Runtime error4ms6116 KiB
6Runtime error4ms6216 KiB
7Runtime error4ms6180 KiB
subtask315/15
8Accepted4ms6084 KiB
9Accepted6ms5968 KiB
10Accepted6ms5972 KiB
11Accepted6ms6232 KiB
12Accepted6ms6440 KiB
13Accepted6ms6396 KiB
14Accepted7ms6704 KiB
15Accepted4ms6600 KiB
16Accepted6ms6924 KiB
17Accepted4ms7228 KiB
18Accepted4ms7192 KiB
subtask40/34
19Runtime error4ms7176 KiB
20Runtime error4ms7204 KiB
21Runtime error4ms7212 KiB
22Runtime error4ms7356 KiB
23Runtime error4ms7468 KiB
24Runtime error4ms7448 KiB
25Runtime error4ms7200 KiB
26Runtime error4ms7200 KiB
27Runtime error4ms7228 KiB
28Runtime error4ms7204 KiB
29Runtime error4ms7336 KiB
30Runtime error4ms7412 KiB
31Runtime error4ms7340 KiB
32Runtime error4ms7548 KiB
33Runtime error4ms7344 KiB
subtask521/21
34Accepted481ms7864 KiB
35Accepted1.151s8348 KiB
36Accepted1.177s8352 KiB
37Accepted1.189s8312 KiB
38Accepted1.19s8316 KiB
39Accepted1.185s8492 KiB
40Accepted1.287s8224 KiB
41Accepted1.363s8368 KiB
42Accepted189ms8012 KiB
43Accepted150ms8068 KiB
44Accepted151ms8184 KiB
45Accepted151ms8300 KiB
46Accepted175ms8312 KiB
47Accepted170ms8320 KiB
48Accepted261ms8476 KiB
49Accepted257ms8456 KiB
subtask60/22
50Runtime error4ms8300 KiB
51Runtime error4ms8300 KiB
52Runtime error4ms8296 KiB
53Runtime error4ms8304 KiB
54Runtime error4ms8376 KiB
55Runtime error4ms8520 KiB
56Runtime error4ms8776 KiB
57Runtime error4ms8640 KiB
58Runtime error4ms8560 KiB
59Runtime error4ms8552 KiB
60Runtime error4ms8556 KiB
61Runtime error4ms8548 KiB
62Runtime error4ms8612 KiB
63Runtime error4ms8620 KiB
64Runtime error4ms8532 KiB
65Runtime error4ms8620 KiB
66Runtime error4ms8624 KiB