一个唐诗问题
查看原帖
一个唐诗问题
649751
Blued楼主2024/9/24 17:43
#include <bits/stdc++.h>
#define int long long
#define db double
#define mk make_pair
#define pb push_back
#define pii pair<int, int>
using namespace std;

int n, V;
int f[5005][5005][2];
int a[5005], b[5005], c[5005];
int siz[5005];
vector<int>E[5005];
inline int read() {
	int x = 0, f = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-')f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return x * f;
}

inline void solve (int nw) {
	siz[nw] = 1;
	f[nw][0][0] = 0;
	f[nw][1][0] = a[nw];
	f[nw][1][1] = a[nw] - b[nw];
	for (auto v : E[nw]) {
		solve(v);
		for (int i = siz[nw]; i >= 0; i --) {
			for (int j = 0; j <= siz[v]; j ++) {
				f[nw][i + j][0] = min(f[nw][i + j][0], f[nw][i][0] + f[v][j][0]);
				f[nw][i + j][1] = min(f[nw][i + j][1], f[nw][i][1] + f[v][j][0]);
				f[nw][i + j][1] = min(f[nw][i + j][1], f[nw][i][1] + f[v][j][1]);
			}
		}
		siz[nw] += siz[v];
	}
	return ;
}

signed main() {
	n = read(), V = read();
	cin >> a[1] >> b[1];
	for (int i = 2; i <= n; i ++) {
		cin >> a[i] >> b[i] >> c[i];
		E[c[i]].pb(i);
	}
	memset(f, 0x3f, sizeof f);
	solve(1);
	int ans;
	for (int i = n; i >= 1; i --) {
		if (f[1][i][1] <= V || f[1][i][0] <= V) {
			ans = i;break;
		}
	}
	cout << ans;
	return 0;
}

第59行ans = i;break;该成return cout>> ans, 0;为什么会错

2024/9/24 17:43
加载中...