0点流向2-n号点,且流向每个点的流量为F[i],最后原问题就变成了判定从0点到1点的最大流和sum{F[i]}(1<i<=n)是否相等,如果相等表示此R是可以的,
否则此R是不合法的,就这样一直二分R下去,如果二分到最后R的值大于所有两点之间的距离,那么此题无解。
*/
#include<math.h> #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define inf 2000000000 struct node { double x,y,z; }point[110]; double dis(node a,node b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)); } int F[110],L[110]; double mp[110][110],Max; void init(int n) { int i,j;Max=0; for(i=1;i<=n;i++) for(j=1;j<=n;j++) { mp[i][j]=dis(point[i],point[j]); if(mp[i][j]>Max) Max=mp[i][j]; } } #define N 300 #define M 100000 struct edge { int from,to,c,next; }edge[M]; int ant,head[N],dep[N],gap[N]; void add(int a,int b,int c) { edge[ant].from=a; edge[ant].to=b; edge[ant].c=c; edge[ant].next=head[a]; head[a]=ant++; edge[ant].from=b; edge[ant].to=a; edge[ant].c=0; edge[ant].next=head[b]; head[b]=ant++; } void BFS(int start,int end) { int i,to,u; memset(dep,-1,sizeof(dep)); memset(gap,0,sizeof(gap)); queue < int > que ; dep[end]=0; gap[0]=1; que.push(end); while(!que.empty()) { u=que.front(); que.pop(); for(i=head[u];i!=-1;i=edge[i].next) { to=edge[i].to; if(dep[to]!=-1||edge[i].c!=0)continue; que.push(to); dep[to]=dep[u]+1; ++gap[dep[to]]; } } } int s[N],cur[N]; int Gap(int start,int end,int n) { int res=0; BFS(start,end); int u=start,top=0; memcpy(cur,head,sizeof(head)); while(dep[start]<n) { int i; if(u==end) { double Min=inf; int flag; for(i=0;i<top;i++) { if(edge[s[i]].c<Min) { Min=edge[s[i]].c; flag=i; } } res+=Min; for(i=0;i<top;i++) { edge[s[i]].c-=Min; edge[s[i]^1].c+=Min; } top=flag; u=edge[s[top]].from; } if(u!=end&&gap[dep[u]-1]==0)break; for(i=cur[u];i!=-1;i=edge[i].next) { if(edge[i].c!=0&&dep[edge[i].to]+1==dep[u]) break; } if(i!=-1) { cur[u]=i; s[top++]=i; u=edge[i].to; } else { int Min=n; for(i=head[u];i!=-1;i=edge[i].next) { if(edge[i].c==0)continue; if(Min>dep[edge[i].to]) { cur[u]=i; Min=dep[edge[i].to]; } } --gap[dep[u]]; dep[u]=Min+1; ++gap[dep[u]]; if(u!=start) u=edge[s[--top]].from; } } return res; } int judge(double width,int n) { int i,j; int maxf=0; ant=0; memset(head,-1,sizeof(head)); for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(mp[i][j]<=width) { add(i+n,j,inf); add(j+n,i,inf); } } add(i,i+n,L[i]); if(i>1){ add(0,i,F[i]); maxf+=F[i]; } } return Gap(0,1,2*n+1)==maxf; } int main() { int n,i; double l,r,mid; while(scanf("%d",&n)!=EOF) { for(i=1;i<=n;i++) { scanf("%lf%lf%lf%d%d",&point[i].x,&point[i].y,&point[i].z,&F[i],&L[i]); } init(n); r=1e20,l=0; while(r-l>0.00000001) { mid=(l+r)/2; if(judge(mid,n)) r=mid; else l=mid; } if(mid>Max) printf("-1\n"); else printf("%.7lf\n",mid); } return 0; }