10487 | 2024-04-03 11:53:21 | Valaki2 | Autópálya infláció | cpp17 | Runtime error 36/100 | 1.363s | 8776 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();
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 4ms | 5064 KiB | ||||
2 | Runtime error | 4ms | 5344 KiB | ||||
subtask2 | 0/8 | ||||||
3 | Runtime error | 4ms | 5568 KiB | ||||
4 | Runtime error | 4ms | 5772 KiB | ||||
5 | Runtime error | 4ms | 6116 KiB | ||||
6 | Runtime error | 4ms | 6216 KiB | ||||
7 | Runtime error | 4ms | 6180 KiB | ||||
subtask3 | 15/15 | ||||||
8 | Accepted | 4ms | 6084 KiB | ||||
9 | Accepted | 6ms | 5968 KiB | ||||
10 | Accepted | 6ms | 5972 KiB | ||||
11 | Accepted | 6ms | 6232 KiB | ||||
12 | Accepted | 6ms | 6440 KiB | ||||
13 | Accepted | 6ms | 6396 KiB | ||||
14 | Accepted | 7ms | 6704 KiB | ||||
15 | Accepted | 4ms | 6600 KiB | ||||
16 | Accepted | 6ms | 6924 KiB | ||||
17 | Accepted | 4ms | 7228 KiB | ||||
18 | Accepted | 4ms | 7192 KiB | ||||
subtask4 | 0/34 | ||||||
19 | Runtime error | 4ms | 7176 KiB | ||||
20 | Runtime error | 4ms | 7204 KiB | ||||
21 | Runtime error | 4ms | 7212 KiB | ||||
22 | Runtime error | 4ms | 7356 KiB | ||||
23 | Runtime error | 4ms | 7468 KiB | ||||
24 | Runtime error | 4ms | 7448 KiB | ||||
25 | Runtime error | 4ms | 7200 KiB | ||||
26 | Runtime error | 4ms | 7200 KiB | ||||
27 | Runtime error | 4ms | 7228 KiB | ||||
28 | Runtime error | 4ms | 7204 KiB | ||||
29 | Runtime error | 4ms | 7336 KiB | ||||
30 | Runtime error | 4ms | 7412 KiB | ||||
31 | Runtime error | 4ms | 7340 KiB | ||||
32 | Runtime error | 4ms | 7548 KiB | ||||
33 | Runtime error | 4ms | 7344 KiB | ||||
subtask5 | 21/21 | ||||||
34 | Accepted | 481ms | 7864 KiB | ||||
35 | Accepted | 1.151s | 8348 KiB | ||||
36 | Accepted | 1.177s | 8352 KiB | ||||
37 | Accepted | 1.189s | 8312 KiB | ||||
38 | Accepted | 1.19s | 8316 KiB | ||||
39 | Accepted | 1.185s | 8492 KiB | ||||
40 | Accepted | 1.287s | 8224 KiB | ||||
41 | Accepted | 1.363s | 8368 KiB | ||||
42 | Accepted | 189ms | 8012 KiB | ||||
43 | Accepted | 150ms | 8068 KiB | ||||
44 | Accepted | 151ms | 8184 KiB | ||||
45 | Accepted | 151ms | 8300 KiB | ||||
46 | Accepted | 175ms | 8312 KiB | ||||
47 | Accepted | 170ms | 8320 KiB | ||||
48 | Accepted | 261ms | 8476 KiB | ||||
49 | Accepted | 257ms | 8456 KiB | ||||
subtask6 | 0/22 | ||||||
50 | Runtime error | 4ms | 8300 KiB | ||||
51 | Runtime error | 4ms | 8300 KiB | ||||
52 | Runtime error | 4ms | 8296 KiB | ||||
53 | Runtime error | 4ms | 8304 KiB | ||||
54 | Runtime error | 4ms | 8376 KiB | ||||
55 | Runtime error | 4ms | 8520 KiB | ||||
56 | Runtime error | 4ms | 8776 KiB | ||||
57 | Runtime error | 4ms | 8640 KiB | ||||
58 | Runtime error | 4ms | 8560 KiB | ||||
59 | Runtime error | 4ms | 8552 KiB | ||||
60 | Runtime error | 4ms | 8556 KiB | ||||
61 | Runtime error | 4ms | 8548 KiB | ||||
62 | Runtime error | 4ms | 8612 KiB | ||||
63 | Runtime error | 4ms | 8620 KiB | ||||
64 | Runtime error | 4ms | 8532 KiB | ||||
65 | Runtime error | 4ms | 8620 KiB | ||||
66 | Runtime error | 4ms | 8624 KiB |