#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e18, N = 305;
vector<array<int, 2> > g[N];
int dp[N][N];
void dfs(int node, int par = -1, int parent_weight = 0)
{
bool isLeaf = true;
for(auto [to, wt]: g[node])
{
dfs(to, node, wt);
isLeaf = false;
}
// cout << "node = " << node << ", parent_weight = " << parent_weight << ", isLeaf = " << isLeaf << "\n";
if(isLeaf)
{
dp[node][parent_weight] = 0;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
dp[node][i] = min(dp[node][i], dp[node][j] + abs(j - i));
}
}
return;
}
for(int i = 0; i < N; i++)
{
dp[node][i] = INF;
}
vector<int> holyf(N);
bool omk = true;
for(auto [to, temp]: g[node])
{
for(int i = 0; i < N - parent_weight; i++)
{
holyf[i] = INF;
for(int j = 0; j < N; j++)
{
holyf[i] = min(holyf[i], dp[to][j] + abs(j - i));
}
if(omk)
{
dp[node][i + parent_weight] = holyf[i];
}
else
{
dp[node][i + parent_weight] += holyf[i];
}
}
omk = false;
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
dp[node][i] = min(dp[node][i], dp[node][j] + abs(j - i));
}
}
}
void Solve()
{
int n, m;
cin >> n >> m;
for(int i = 2; i <= n + m; i++)
{
int par, wt;
cin >> par >> wt;
// cout << "edge: {" << par << ", " << i << ", " << wt << "}\n";
g[par].push_back({i, wt});
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
dp[i][j] = INF;
}
}
dfs(1);
// for(int i = 1; i <= 10; i++)
// {
// for(int j = 0; j <= 20; j++)
// {
// cout << "dp[" << i << "][" << j << "] = " << dp[i][j] << "\n";
// }
// cout << "\n\n\n\n";
// }
int ans = INF;
for(int i = 0; i < N; i++)
{
ans = min(ans, dp[1][i]);
}
cout << ans << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
//cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
Compilation message
fireworks.cpp: In function 'void dfs(long long int, long long int, long long int)':
fireworks.cpp:14:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
14 | for(auto [to, wt]: g[node])
| ^
fireworks.cpp:38:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
38 | for(auto [to, temp]: g[node])
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
1872 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
980 KB |
Output is correct |
2 |
Correct |
9 ms |
1068 KB |
Output is correct |
3 |
Incorrect |
12 ms |
1092 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
1872 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
1872 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |