104812024-04-03 11:27:46Valaki2Autópálya inflációcpp17Runtime error 36/1001.978s9956 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<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
1Accepted4ms4976 KiB
2Runtime error4ms5256 KiB
subtask20/8
3Runtime error4ms5464 KiB
4Runtime error4ms5676 KiB
5Runtime error4ms5884 KiB
6Runtime error4ms5948 KiB
7Runtime error4ms6160 KiB
subtask315/15
8Accepted4ms6556 KiB
9Accepted6ms6380 KiB
10Accepted6ms6388 KiB
11Accepted7ms6392 KiB
12Accepted7ms6444 KiB
13Accepted7ms6672 KiB
14Accepted7ms6880 KiB
15Accepted4ms7080 KiB
16Accepted4ms7160 KiB
17Accepted4ms7464 KiB
18Accepted4ms7420 KiB
subtask40/34
19Runtime error4ms7588 KiB
20Runtime error4ms7664 KiB
21Runtime error4ms7888 KiB
22Runtime error4ms7964 KiB
23Runtime error4ms7760 KiB
24Runtime error4ms7888 KiB
25Runtime error4ms8032 KiB
26Runtime error4ms8012 KiB
27Runtime error4ms8012 KiB
28Runtime error4ms8132 KiB
29Runtime error4ms8088 KiB
30Runtime error4ms8044 KiB
31Runtime error4ms7972 KiB
32Runtime error4ms8200 KiB
33Runtime error4ms8068 KiB
subtask521/21
34Accepted592ms8336 KiB
35Accepted1.414s9836 KiB
36Accepted1.437s9852 KiB
37Accepted1.455s9840 KiB
38Accepted1.501s9844 KiB
39Accepted1.498s9956 KiB
40Accepted1.934s9932 KiB
41Accepted1.978s9904 KiB
42Accepted232ms8144 KiB
43Accepted192ms8076 KiB
44Accepted192ms8312 KiB
45Accepted192ms8308 KiB
46Accepted222ms8328 KiB
47Accepted217ms8328 KiB
48Accepted317ms8404 KiB
49Accepted314ms8408 KiB
subtask60/22
50Runtime error4ms8184 KiB
51Runtime error4ms8184 KiB
52Runtime error4ms8256 KiB
53Runtime error4ms8352 KiB
54Runtime error4ms8340 KiB
55Runtime error4ms8264 KiB
56Runtime error4ms8292 KiB
57Runtime error4ms8212 KiB
58Runtime error4ms8176 KiB
59Runtime error4ms8180 KiB
60Runtime error4ms8180 KiB
61Runtime error4ms8320 KiB
62Runtime error4ms8320 KiB
63Runtime error4ms8404 KiB
64Runtime error4ms8368 KiB
65Runtime error4ms8356 KiB
66Runtime error4ms8172 KiB