启发式搜索 70 pts
查看原帖
启发式搜索 70 pts
878013
_Supernova楼主2024/8/12 08:07
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair <int, int> pii;
typedef long double ld;
const int N = 104;
int t, n, ans;
struct node {
	int v, w;
} a[N]; 
bool cmp(node x, node y) {
	return ((ld)x.w / (ld)x.v > (ld)y.w / (ld)y.v);
}
ld h(int x, int l) {
	ld res = 0;
	for (int i = x; i <= n; ++i) {
		if (l >= a[i].v) {
			l -= a[i].v;
			res += (ld)a[i].w;
			if (l == 0) return res;
		}
		else {
			res += (ld)a[i].w / (ld)a[i].v * (ld)l;
			return res;
		}
	}
} 
void dfs(int step, int sum, int lft) {
	if (step > n && lft >= 0) {
		ans = max(ans, sum);
		return ;
	}
	if (step > n) return ;
	if (lft <= 0) return ;
	if (h(step, lft) + (ld)sum < (ld)ans) return ;
	dfs(step + 1, sum + a[step].w, lft - a[step].v);
	dfs(step + 1, sum, lft);
}
int main(void) {
	scanf("%d %d", &t, &n);
	for (int i = 1; i <= n; ++i) {
		cin >> a[i].v >> a[i].w;
	}
	sort(a + 1, a + n + 1, cmp);
	dfs(1, 0, t);
	printf("%d", ans);
	return 0;
}
2024/8/12 08:07
加载中...