10484 | 2024-04-03 11:33:15 | Valaki2 | Autópálya infláció | cpp17 | Time limit exceeded 15/100 | 2.101s | 84476 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();
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 35ms | 80584 KiB | ||||
2 | Time limit exceeded | 2.061s | 47840 KiB | ||||
subtask2 | 0/8 | ||||||
3 | Time limit exceeded | 2.075s | 42148 KiB | ||||
4 | Time limit exceeded | 2.036s | 42328 KiB | ||||
5 | Time limit exceeded | 2.068s | 42248 KiB | ||||
6 | Time limit exceeded | 2.072s | 44980 KiB | ||||
7 | Time limit exceeded | 2.063s | 41588 KiB | ||||
subtask3 | 15/15 | ||||||
8 | Accepted | 37ms | 81604 KiB | ||||
9 | Accepted | 41ms | 81476 KiB | ||||
10 | Accepted | 43ms | 81768 KiB | ||||
11 | Accepted | 43ms | 81832 KiB | ||||
12 | Accepted | 52ms | 81984 KiB | ||||
13 | Accepted | 52ms | 82192 KiB | ||||
14 | Accepted | 46ms | 82080 KiB | ||||
15 | Accepted | 43ms | 82296 KiB | ||||
16 | Accepted | 39ms | 82532 KiB | ||||
17 | Accepted | 41ms | 82852 KiB | ||||
18 | Accepted | 43ms | 82708 KiB | ||||
subtask4 | 0/34 | ||||||
19 | Time limit exceeded | 2.073s | 46664 KiB | ||||
20 | Time limit exceeded | 2.068s | 45044 KiB | ||||
21 | Time limit exceeded | 2.056s | 45348 KiB | ||||
22 | Time limit exceeded | 2.061s | 46840 KiB | ||||
23 | Time limit exceeded | 2.059s | 46832 KiB | ||||
24 | Time limit exceeded | 2.058s | 43720 KiB | ||||
25 | Time limit exceeded | 2.076s | 46792 KiB | ||||
26 | Time limit exceeded | 2.049s | 46708 KiB | ||||
27 | Time limit exceeded | 2.079s | 43348 KiB | ||||
28 | Time limit exceeded | 2.016s | 43732 KiB | ||||
29 | Time limit exceeded | 2.085s | 43736 KiB | ||||
30 | Time limit exceeded | 2.076s | 43452 KiB | ||||
31 | Time limit exceeded | 2.075s | 43636 KiB | ||||
32 | Time limit exceeded | 2.101s | 43780 KiB | ||||
33 | Time limit exceeded | 2.052s | 43760 KiB | ||||
subtask5 | 0/21 | ||||||
34 | Time limit exceeded | 2.072s | 43828 KiB | ||||
35 | Time limit exceeded | 2.059s | 45240 KiB | ||||
36 | Time limit exceeded | 2.028s | 46952 KiB | ||||
37 | Time limit exceeded | 2.065s | 46860 KiB | ||||
38 | Time limit exceeded | 2.068s | 46956 KiB | ||||
39 | Time limit exceeded | 2.049s | 46884 KiB | ||||
40 | Time limit exceeded | 2.076s | 46764 KiB | ||||
41 | Time limit exceeded | 2.079s | 46744 KiB | ||||
42 | Accepted | 1.105s | 83808 KiB | ||||
43 | Accepted | 1.003s | 83736 KiB | ||||
44 | Accepted | 994ms | 83740 KiB | ||||
45 | Accepted | 991ms | 83624 KiB | ||||
46 | Accepted | 1.07s | 83800 KiB | ||||
47 | Accepted | 1.031s | 83792 KiB | ||||
48 | Accepted | 1.478s | 84216 KiB | ||||
49 | Accepted | 1.468s | 84476 KiB | ||||
subtask6 | 0/22 | ||||||
50 | Time limit exceeded | 2.069s | 44608 KiB | ||||
51 | Time limit exceeded | 2.075s | 47660 KiB | ||||
52 | Time limit exceeded | 2.073s | 50624 KiB | ||||
53 | Time limit exceeded | 2.033s | 50796 KiB | ||||
54 | Time limit exceeded | 2.065s | 57124 KiB | ||||
55 | Time limit exceeded | 2.073s | 57380 KiB | ||||
56 | Time limit exceeded | 2.081s | 44756 KiB | ||||
57 | Time limit exceeded | 2.039s | 44828 KiB | ||||
58 | Accepted | 1.875s | 84104 KiB | ||||
59 | Time limit exceeded | 2.076s | 44236 KiB | ||||
60 | Time limit exceeded | 2.068s | 44036 KiB | ||||
61 | Time limit exceeded | 2.075s | 44144 KiB | ||||
62 | Time limit exceeded | 2.063s | 43856 KiB | ||||
63 | Time limit exceeded | 2.084s | 44232 KiB | ||||
64 | Time limit exceeded | 2.068s | 44340 KiB | ||||
65 | Time limit exceeded | 2.069s | 44232 KiB | ||||
66 | Time limit exceeded | 2.055s | 43976 KiB |