Freckles - UVa 10034 最小生成树
来源:互联网 发布:thorx6软件下载 编辑:程序博客网 时间:2024/05/14 19:18
Problem A: Freckles
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 input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.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
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.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
131.0 1.02.0 2.02.0 4.0
Sample Output
3.41
题意:裸最小生成树。
AC代码如下:
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;struct node{ int u,v; double w;}edge[10010];bool cmp(node a,node b){ return a.w<b.w;}double x[110],y[110],ans;int p[110],n,num;int find(int x){ return x==p[x] ? x : p[x]=find(p[x]);}void Kruskal(){ int u,v,i; for(i=1;i<=n;i++) p[i]=i; for(i=1;i<=num;i++) { u=find(edge[i].u); v=find(edge[i].v); if(u!=v) { ans+=edge[i].w; p[v]=u; } }}int main(){ int T,t,i,j,k; scanf("%d",&T); for(t=1;t<=T;t++) { if(t!=1) printf("\n"); scanf("%d",&n); for(i=1;i<=n;i++) scanf("%lf%lf",&x[i],&y[i]); num=0; for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) { edge[++num].u=i; edge[num].v=j; edge[num].w=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); } sort(edge+1,edge+1+num,cmp); ans=0; Kruskal(); printf("%.2f\n",ans); }}
- uva 10034 Freckles(最小生成树Kruskal)
- UVA 10034 - Freckles(最小生成树)
- UVA 10034 Freckles 最小生成树
- Freckles - UVa 10034 最小生成树
- uva 10034 Freckles 最小生成树
- uva 10034 Freckles 最小生成树
- UVA 10034Freckles 【最小生成树】
- 【UVA 10034 Freckles】& Kruskal & 最小生成树
- UVa 10034 - Freckles (最小生成树模板题)
- uva 10034 Freckles(最小生成树Kruskal)
- UVa 10034 Freckles (最小生成树+kruskal)
- 10034 - Freckles(最小生成树)
- 最小生成树Freckles
- 最小生成树 Freckles
- UVA10034 - Freckles(最小生成树)
- 2560 Freckles (最小生成树)
- POJ 2560 Freckles 最小生成树
- HOJ 1811 Freckles (Prim 最小生成树)
- 函数 参数的布局
- 出来行迟早是要还的(篇四):游戏开发离职日志和面试日志
- ajax中,触发函数如何使用两个形参
- 51_定时器扫描矩阵键盘_数码管显示
- first day 懵懂无知
- Freckles - UVa 10034 最小生成树
- Linux把窗口快捷切换到最前的方法
- awk教程
- 被坑于是做出来的万年历
- 【从零学习openCV】ubuntu+openCV+qt安装配置
- github优秀开源项目大全-iOS
- Batch Gradient Descent and Stochastic Gradient Descent
- 安卓自学笔记:5:用ImageView创建一个图片浏览器
- 香港西贡自由行攻略