P4951 [USACO01OPEN]Earthquake

度限制最小生成树

就是01分数规划,裸的。

#include<bits/stdc++.h>
using namespace std;
#define orz cout<<"lytcltcltcltcltcltcl"<<endl
inline int r(){int s=0,k=1;char c=getchar();while(!isdigit(c)){if(c=='-')k=-1;c=getchar();}while(isdigit(c)){s=s*10+c-'0';c=getchar();}return s*k;}
int n,m,fa[1000001];
double s;
struct node
{
	int from,to;
	double dis,time;
}a[1000001];
double k;
bool cmp(node x,node y)
{
	return x.dis+k*x.time<y.dis+k*y.time;
}
int father(int x)
{
	if(fa[x]!=x)fa[x]=father(fa[x]);
	return fa[x];
}
bool check(double mid)
{
	k=mid;
	double sum=0;
	sort(a+1,a+m+1,cmp);
	for(int i=1;i<=n;i++)fa[i]=i;
	for(int i=1;i<=m;i++)
	{
		int x=a[i].from,y=a[i].to;
		int fax=father(x),fay=father(y);
		if(fax==fay)continue;
		fa[fax]=fay;
		sum+=a[i].dis+k*a[i].time;
	}
//	cout<<"ahh"<<sum<<" "<<s<<endl;
	if(sum<=s)
	{
//		cout<<"I'm ok\n";
		return 1;
	}
	return 0;
}
int main()
{
	n=r();m=r();s=r();
	for(int i=1;i<=m;i++)
	{
		a[i].from=r();
		a[i].to=r();
		a[i].dis=r();
		a[i].time=r();
	}
	double l=0,r=2e9,ans=0;
	while(r-l>1e-6)
	{
		double mid=(l+r)/2;
		if(check(mid))l=mid,ans=mid;
		else r=mid;
	}
	printf("%.4lf",ans);
}
上一篇:markdown的骚气操作(一)


下一篇:4.dubbo配置