#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=10005;
const double esp=0.00000000001;
int n,m,f,fa[N];
struct node{
int u,v,c,t;
double w;
}e[N];
bool cmp(node x,node y){
return x.w<y.w;
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
bool check(double mid){
for(int i=1;i<=m;i++){
e[i].w=e[i].t*mid+e[i].c;
}
sort(e+1,e+1+m,cmp);
for(int i=1;i<=n;i++)fa[i]=i;
int tot=0;
double sum=0;
for(int i=1;i<=m;i++){
if(tot==n-1)break;
int a=find(e[i].u),b=find(e[i].v);
if(a!=b){
fa[a]=b;
tot++;
sum+=e[i].w;
}
}
return tot==n-1&&f-sum>=0;
}
signed main(){
cin>>n>>m>>f;
for(int i=1;i<=m;i++){
cin>>e[i].u>>e[i].v>>e[i].c>>e[i].t;
}
double l=0,r=1e9,mid;
while(l+esp<r){
mid=(l+r)/2;
//cout<<mid<<endl;
if(check(mid))l=mid;
else r=mid;
}
printf("%.4lf\n",l);
return 0;
}
这是我的AC代码
但是
如果把二分的部分变成
double l=0,r=1e9,mid,ans;
while(l+esp<=r){
mid=(l+r)/2;
//cout<<mid<<endl;
if(check(mid))l=mid+1,ans=mid;
else r=mid-1;
}
printf("%.4lf\n",ans);
就不对了