标签:sqrt let limit prim line case esc inline with
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 27191 | Accepted: 7557 |
Description
Input
Output
Sample Input
4 0 0 0 0 1 1 1 1 2 1 0 3 0
Sample Output
1.000
Source
1 #include <algorithm> 2 #include <cstdio> 3 #include <cmath> 4 5 #define max(a,b) (a>b?a:b) 6 #define min(a,b) (a<b?a:b) 7 8 inline void read(int &x) 9 { 10 x=0; register char ch=getchar(); 11 for(; ch>‘9‘||ch<‘0‘; ) ch=getchar(); 12 for(; ch>=‘0‘&&ch<=‘9‘; ch=getchar()) x=x*10+ch-‘0‘; 13 } 14 15 const double eps(1e-4); 16 const int N(1026); 17 struct Node { 18 int x,y,h; 19 }city[N]; 20 int n; 21 22 double L,R,Mid,ans; 23 struct Edge { 24 int u,v; 25 double w; 26 Edge(int u=0,int v=0,double w=0.0):u(u),v(v),w(w){} 27 bool operator < (const Edge&x)const { return w<x.w; } 28 }road[N*N]; 29 30 inline double Dis(Node a,Node b) 31 { 32 double x=1.0*(a.x-b.x)*(a.x-b.x); 33 double y=1.0*(a.y-b.y)*(a.y-b.y); 34 return abs(a.h-b.h)-Mid*sqrt(x+y); 35 } 36 37 int fa[N]; 38 int find(int x) { return x==fa[x]?x:fa[x]=find(fa[x]); } 39 40 inline bool check() 41 { 42 double ret=0; int cnt=0,m=0; 43 for(int i=1; i<=n; fa[i]=i++) 44 for(int j=1; j<=n; ++j) 45 if(i!=j) road[++m]=Edge(i,j,Dis(city[i],city[j])); 46 std::sort(road+1,road+m+1); 47 for(int fx,fy,i=1; i<=m; ++i) 48 { 49 fx=find(road[i].u), 50 fy=find(road[i].v); 51 if(fx==fy) continue; 52 fa[fx]=fy; ret+=road[i].w; 53 if(++cnt==n-1) return ret<0; 54 } 55 } 56 57 int Presist() 58 { 59 for(; scanf("%d",&n)&&n; ) 60 { 61 for(int i=1; i<=n; ++i) 62 { 63 read(city[i].x), 64 read(city[i].y), 65 read(city[i].h), 66 R=max(R,1.0*city[i].h); 67 } 68 for(L=0; L+eps<R; ) 69 { 70 Mid=(L+R)/2.0; 71 if(check()) R=Mid; 72 else L=Mid; 73 } 74 printf("%.3lf\n",R); 75 } 76 return 0; 77 } 78 79 int Aptal=Presist(); 80 int main(int argc,char**argv){;}
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 6 #define max(a,b) (a>b?a:b) 7 8 inline void read(int &x) 9 { 10 x=0; register char ch=getchar(); 11 for(; ch>‘9‘||ch<‘0‘; ) ch=getchar(); 12 for(; ch>=‘0‘&&ch<=‘9‘; ch=getchar()) x=x*10+ch-‘0‘; 13 } 14 15 const double INF(10000000.0); 16 const double eps(1e-9); 17 const int N(1026); 18 19 double h[N][N],d[N][N]; 20 struct Node { 21 int x,y,h; 22 }city[N]; 23 int n; 24 25 double L,R,Mid,ans,dis[N]; 26 bool vis[N]; 27 28 inline double Dis(Node a,Node b) 29 { 30 double x=1.0*(a.x-b.x)*(a.x-b.x); 31 double y=1.0*(a.y-b.y)*(a.y-b.y); 32 return (double)sqrt(x+y); 33 } 34 35 inline bool check() 36 { 37 for(int i=1; i<=n; ++i) vis[i]=0; 38 for(int i=2; i<=n; ++i) dis[i]=h[1][i]-Mid*d[1][i]; 39 double ret=0.0,minn; vis[1]=1; 40 for(int i=2,u; i<=n; ++i) 41 { 42 minn=INF; 43 for(int j=2; j<=n; ++j) 44 if(!vis[j]&&minn>dis[j]) minn=dis[u=j]; 45 if(minn==INF) break; 46 ret+=minn; vis[u]=1; 47 for(int v=2; v<=n; ++v) 48 if(!vis[v]&&dis[v]>h[u][v]-Mid*d[u][v]) 49 dis[v]=h[u][v]-Mid*d[u][v]; 50 } 51 return ret<=0; 52 } 53 54 int Presist() 55 { 56 for(; scanf("%d",&n)&&n; ) 57 { 58 for(int i=1; i<=n; ++i) 59 { 60 read(city[i].x), 61 read(city[i].y), 62 read(city[i].h), 63 R=max(R,city[i].h); 64 } 65 for(int i=1; i<=n; ++i) 66 for(int j=i+1; j<=n; ++j) 67 { 68 d[i][j]=d[j][i]=Dis(city[i],city[j]); 69 h[i][j]=h[j][i]=abs(city[i].h-city[j].h)*1.0; 70 } 71 for(L=0; L+eps<R; ) 72 { 73 Mid=(L+R)/2.0; 74 if(check()) R=Mid; 75 else L=Mid; 76 } 77 printf("%.3lf\n",R); 78 } 79 return 0; 80 } 81 82 int Aptal=Presist(); 83 int main(int argc,char**argv){;}
标签:sqrt let limit prim line case esc inline with
原文地址:http://www.cnblogs.com/Shy-key/p/7673898.html