10480 | 2024-04-03 11:25:25 | Valaki2 | Autópálya infláció | cpp17 | Time limit exceeded 15/100 | 2.102s | 84176 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 = 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 | 80520 KiB | ||||
2 | Time limit exceeded | 2.102s | 47800 KiB | ||||
subtask2 | 0/8 | ||||||
3 | Time limit exceeded | 2.063s | 42180 KiB | ||||
4 | Time limit exceeded | 2.055s | 42392 KiB | ||||
5 | Time limit exceeded | 2.065s | 42596 KiB | ||||
6 | Time limit exceeded | 2.065s | 45084 KiB | ||||
7 | Time limit exceeded | 2.079s | 41652 KiB | ||||
subtask3 | 15/15 | ||||||
8 | Accepted | 45ms | 81580 KiB | ||||
9 | Accepted | 43ms | 81608 KiB | ||||
10 | Accepted | 45ms | 81680 KiB | ||||
11 | Accepted | 46ms | 81992 KiB | ||||
12 | Accepted | 46ms | 82264 KiB | ||||
13 | Accepted | 46ms | 82328 KiB | ||||
14 | Accepted | 54ms | 82268 KiB | ||||
15 | Accepted | 37ms | 82396 KiB | ||||
16 | Accepted | 46ms | 82292 KiB | ||||
17 | Accepted | 37ms | 82220 KiB | ||||
18 | Accepted | 35ms | 82356 KiB | ||||
subtask4 | 0/34 | ||||||
19 | Time limit exceeded | 2.058s | 46384 KiB | ||||
20 | Time limit exceeded | 2.081s | 44876 KiB | ||||
21 | Time limit exceeded | 2.063s | 44044 KiB | ||||
22 | Time limit exceeded | 2.072s | 46548 KiB | ||||
23 | Time limit exceeded | 2.068s | 46684 KiB | ||||
24 | Time limit exceeded | 2.063s | 43476 KiB | ||||
25 | Time limit exceeded | 2.055s | 46548 KiB | ||||
26 | Time limit exceeded | 2.055s | 46532 KiB | ||||
27 | Time limit exceeded | 2.072s | 43200 KiB | ||||
28 | Time limit exceeded | 2.075s | 43476 KiB | ||||
29 | Time limit exceeded | 2.075s | 43188 KiB | ||||
30 | Time limit exceeded | 2.061s | 43256 KiB | ||||
31 | Time limit exceeded | 2.063s | 43412 KiB | ||||
32 | Time limit exceeded | 2.061s | 43608 KiB | ||||
33 | Time limit exceeded | 2.068s | 43424 KiB | ||||
subtask5 | 0/21 | ||||||
34 | Time limit exceeded | 2.081s | 43712 KiB | ||||
35 | Time limit exceeded | 2.065s | 45284 KiB | ||||
36 | Time limit exceeded | 2.081s | 46964 KiB | ||||
37 | Time limit exceeded | 2.058s | 46984 KiB | ||||
38 | Time limit exceeded | 2.076s | 46940 KiB | ||||
39 | Time limit exceeded | 2.065s | 46916 KiB | ||||
40 | Time limit exceeded | 2.052s | 46944 KiB | ||||
41 | Time limit exceeded | 2.046s | 46792 KiB | ||||
42 | Accepted | 1.243s | 83824 KiB | ||||
43 | Accepted | 1.123s | 83712 KiB | ||||
44 | Accepted | 1.123s | 83832 KiB | ||||
45 | Accepted | 1.121s | 83580 KiB | ||||
46 | Accepted | 1.207s | 83740 KiB | ||||
47 | Accepted | 1.167s | 83824 KiB | ||||
48 | Accepted | 1.692s | 84108 KiB | ||||
49 | Accepted | 1.672s | 84176 KiB | ||||
subtask6 | 0/22 | ||||||
50 | Time limit exceeded | 2.068s | 44072 KiB | ||||
51 | Time limit exceeded | 2.075s | 47168 KiB | ||||
52 | Time limit exceeded | 2.068s | 50592 KiB | ||||
53 | Time limit exceeded | 2.063s | 50472 KiB | ||||
54 | Time limit exceeded | 2.072s | 56984 KiB | ||||
55 | Time limit exceeded | 2.056s | 57048 KiB | ||||
56 | Time limit exceeded | 2.048s | 44552 KiB | ||||
57 | Time limit exceeded | 2.059s | 44808 KiB | ||||
58 | Time limit exceeded | 2.072s | 43584 KiB | ||||
59 | Time limit exceeded | 2.084s | 43888 KiB | ||||
60 | Time limit exceeded | 2.059s | 43888 KiB | ||||
61 | Time limit exceeded | 2.069s | 43808 KiB | ||||
62 | Time limit exceeded | 2.048s | 43688 KiB | ||||
63 | Time limit exceeded | 2.049s | 44180 KiB | ||||
64 | Time limit exceeded | 2.069s | 44040 KiB | ||||
65 | Time limit exceeded | 2.069s | 44256 KiB | ||||
66 | Time limit exceeded | 2.072s | 44240 KiB |