怎么过掉常数啊
查看原帖
怎么过掉常数啊
87007
橙剑oo楼主2020/5/14 16:47
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
struct node{int to,nxt;}e[5005];
double dp[2505][2505],p[2505],s[2505],a[2505],l=0,r=10000;
int head[2505],Size[2505],n,m,fa,cnt;
inline int read()
{
	int x=0,ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x;
}
void addedge(int u,int v)
{
	e[cnt].to=v;
	e[cnt].nxt=head[u];
	head[u]=cnt++;
}
void dfs(int u,int fa)
{
	dp[u][1]=a[u];
	Size[u]=1;
	for(register int i=head[u];~i;i=e[i].nxt)
	{
		int v=e[i].to;
		if(v==fa) continue;
		dfs(v,u);
		Size[u]+=Size[v];
		for(register int j=min(m,Size[u]);j>0;j--)
			for(register int k=0;k<=min(Size[v],j-1);k++)
				dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]);
	}
}
bool check(double x)
{
	a[0]=0;
	for(int i=1;i<=n;i++) a[i]=p[i]-1.0*x*s[i];
	memset(dp,-INF,sizeof(dp));
	dfs(0,-1);
	return dp[0][m]>=0;
}
int main()
{
	memset(head,-1,sizeof(head));
	scanf("%d%d",&m,&n);
	for(int i=1,v;i<=n;i++)
	{
		s[i]=read(); p[i]=read(); v=read();
		addedge(i,v); addedge(v,i);
	}
	m++;
	while(r-l>1e-5)
	{
		double mid=(l+r)/2.0;
		if(check(mid)) l=mid;
		else r=mid;
	}
	printf("%.3lf\n",l);
	return 0;
}
2020/5/14 16:47
加载中...