104822024-04-03 11:28:52Valaki2Autópálya inflációcpp17Time limit exceeded 23/1002.099s19700 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 = 3100;
const int mod = 1e9 + 7;
const int k = 2; // 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
1Accepted8ms9324 KiB
2Time limit exceeded2.099s7492 KiB
subtask28/8
3Accepted1.059s11184 KiB
4Accepted822ms10864 KiB
5Accepted810ms10820 KiB
6Accepted1.042s13928 KiB
7Accepted291ms10660 KiB
subtask315/15
8Accepted7ms10320 KiB
9Accepted8ms10316 KiB
10Accepted8ms10324 KiB
11Accepted8ms10592 KiB
12Accepted9ms10808 KiB
13Accepted8ms10972 KiB
14Accepted8ms10944 KiB
15Accepted8ms10912 KiB
16Accepted8ms11052 KiB
17Accepted7ms11176 KiB
18Accepted7ms11396 KiB
subtask40/34
19Time limit exceeded2.062s15392 KiB
20Accepted1.776s13944 KiB
21Accepted1.809s13948 KiB
22Time limit exceeded2s15548 KiB
23Time limit exceeded2.01s15652 KiB
24Accepted1.5s12800 KiB
25Time limit exceeded2.059s10976 KiB
26Time limit exceeded2.058s11032 KiB
27Accepted1.264s12076 KiB
28Accepted657ms12292 KiB
29Accepted615ms12176 KiB
30Accepted437ms12240 KiB
31Accepted644ms12248 KiB
32Accepted830ms12376 KiB
33Accepted833ms12380 KiB
subtask50/21
34Accepted218ms11876 KiB
35Wrong answer460ms12724 KiB
36Wrong answer493ms14288 KiB
37Wrong answer497ms14028 KiB
38Wrong answer481ms14160 KiB
39Wrong answer479ms14148 KiB
40Wrong answer513ms12444 KiB
41Wrong answer513ms12316 KiB
42Accepted108ms12232 KiB
43Accepted93ms12224 KiB
44Accepted93ms12224 KiB
45Accepted94ms12220 KiB
46Accepted104ms12336 KiB
47Wrong answer101ms12336 KiB
48Accepted146ms12520 KiB
49Accepted149ms12512 KiB
subtask60/22
50Wrong answer1.493s13520 KiB
51Time limit exceeded2.013s16364 KiB
52Time limit exceeded2.053s11700 KiB
53Time limit exceeded2.065s11676 KiB
54Time limit exceeded2.049s19700 KiB
55Time limit exceeded2.046s11820 KiB
56Wrong answer1.391s12876 KiB
57Wrong answer1.394s13068 KiB
58Accepted175ms12716 KiB
59Accepted671ms13024 KiB
60Accepted643ms13024 KiB
61Accepted610ms13024 KiB
62Wrong answer555ms12900 KiB
63Wrong answer688ms13048 KiB
64Accepted837ms13152 KiB
65Accepted834ms13168 KiB
66Accepted694ms13100 KiB