首页 > 代码库 > POJ2560 Freckles
POJ2560 Freckles
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
Description
In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad‘s back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley‘s engagement falls through.
Consider Dick‘s back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.
Consider Dick‘s back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.
Input
The first line contains 0 < n <= 100, the number of freckles on Dick‘s back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.
Sample Input
31.0 1.02.0 2.02.0 4.0
Sample Output
3.41
Source
Waterloo local 2000.09.23
最小生成树。注意double
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 using namespace std; 8 int read(){ 9 int x=0,f=1;char ch=getchar();10 while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}11 while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}12 return x*f;13 }14 const int mxn=120;15 int n;16 struct point{17 double x;double y;18 }p[mxn];19 double dist(point a,point b){20 return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );21 }22 struct edge{23 int x,y;24 double dis;25 }e[mxn*mxn];int cnt=0;26 int cmp(edge a,edge b){27 return a.dis<b.dis;28 }29 int fa[mxn];30 void init(){31 for(int i=1;i<=n;i++)fa[i]=i;32 }33 int find(int x){34 if(fa[x]==x)return x;35 return fa[x]=find(fa[x]);36 }37 double ans=0;38 void Kruskal(){39 int i,j;40 int ti=0;41 ans=0;42 for(i=1;i<=cnt;i++){43 int u=find(e[i].x),v=find(e[i].y);44 if(u==v)continue;45 ti++;46 ans+=e[i].dis;47 fa[u]=v;48 if(ti==n-1)break;49 }50 return;51 }52 int main(){53 int i,j;54 while(scanf("%d",&n)!=EOF){55 init();56 cnt=0;57 int u,v,dis;int m;58 int i,j;59 for(i=1;i<=n;i++){60 scanf("%lf%lf",&p[i].x,&p[i].y);61 }62 for(i=1;i<n;i++)63 for(j=i+1;j<=n;j++){64 e[++cnt]=(edge){i,j,dist(p[i],p[j])};65 }66 sort(e+1,e+cnt+1,cmp);67 Kruskal();68 printf("%.2f\n",ans);69 }70 return 0;71 }
POJ2560 Freckles
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。