#include <bits/stdc++.h>
#define ll long long
#define db long double
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define int long long
using namespace std;
const int N = 300, inf = 2e18;
int n, m, dp[N + 3][N + 3];
vector <pair <int, int> > q[N + 3];
void dfs(int v, int p) {
for (auto i : q[v]) {
int to = i.fi, w = i.se;
if (to == p) {
continue;
}
dfs(to, v);
}
for (int i = 0; i <= N; i++) {
int res = 0;
for (auto j : q[v]) {
int to = j.fi, w = j.se;
if (to == p) {
continue;
}
int x = inf;
for (int k = 0; k <= i; k++) {
x = min(x, dp[to][i - k] + abs(k - w));
}
res += x;
}
if (q[v].size() == 1 && v != 1) {
dp[v][i] = i;
} else {
dp[v][i] = res;
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for (int i = 2; i <= n + m; i++) {
int x, y;
cin>>x>>y;
q[x].pb({i, y});
q[i].pb({x, y});
}
if (n + m == 2) {
return cout<<0, 0;
}
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
dp[i][j] = inf;
}
}
dfs(1, 1);
int ans = inf;
for (int i = 0; i <= N; i++) {
ans = min(ans, dp[1][i]);
}
cout<<ans;
}
Compilation message
fireworks.cpp: In function 'void dfs(long long int, long long int)':
fireworks.cpp:19:24: warning: unused variable 'w' [-Wunused-variable]
int to = i.fi, w = i.se;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1016 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1144 KB |
Output is correct |
2 |
Correct |
5 ms |
1016 KB |
Output is correct |
3 |
Correct |
6 ms |
1016 KB |
Output is correct |
4 |
Correct |
9 ms |
1144 KB |
Output is correct |
5 |
Correct |
10 ms |
1016 KB |
Output is correct |
6 |
Correct |
11 ms |
1144 KB |
Output is correct |
7 |
Correct |
12 ms |
1144 KB |
Output is correct |
8 |
Correct |
14 ms |
1016 KB |
Output is correct |
9 |
Correct |
15 ms |
1144 KB |
Output is correct |
10 |
Correct |
16 ms |
1144 KB |
Output is correct |
11 |
Correct |
18 ms |
1144 KB |
Output is correct |
12 |
Correct |
19 ms |
1016 KB |
Output is correct |
13 |
Correct |
20 ms |
1272 KB |
Output is correct |
14 |
Correct |
22 ms |
1144 KB |
Output is correct |
15 |
Correct |
22 ms |
1144 KB |
Output is correct |
16 |
Correct |
22 ms |
1144 KB |
Output is correct |
17 |
Correct |
22 ms |
1144 KB |
Output is correct |
18 |
Correct |
22 ms |
1144 KB |
Output is correct |
19 |
Correct |
22 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1016 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Incorrect |
3 ms |
1016 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |