专题四--1002
2016-07-06 23:42
183 查看
题目
Problem Description
Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends ‘s view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.Input contains multiple test cases. Process to the end of file.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
Sample Output
3.41
题目大意
最小生成树的裸题,给你一些点的坐标,然后求连通这些点的最短的线段的长度。注意要将数据处理一下,先求出两个点之间的长度。AC代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int MAX=105;
struct point {
double x;
double y;
} po [MAX];
struct link {
int x;
int y;
double d;
};
int c[MAX];
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y*b.y)*(a.y*b.y));
}
int find(int a)
{
while(c[a]!=a)
a=c[a];
return a;
}
void merg(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
c[a]=b;
}
bool cmp(link a,link b)
{
return a.d<b.d;
}
int main()
{
//freopen("date.in","r",stdin);
//freopen("date.out","w",stdout);
int n,x,y,i,j,res;
link tem;
vector<link> dis;
while(scanf("%d",&n)) {
res=0;
dis.clear();
for(i=1; i<=n; i++) {
scanf("%lf%lf",&po[i].x,&po[i].y);
for(j=i-1; j>=1; j--) {
tem.x=i;
tem.y=j;
tem.d=dist(po[i],po[j]);
dis.push_back(tem);
}
}
vector<link>::iterator b=dis.begin();
vector<link>::iterator e=dis.end();
sort(b,e,cmp);
for(; b!=e; b++) {
if(find(b->x)!=find(b->y)) {
res+=(b->d);
merg(b->x,b->y);
}
}
printf("%.2lf\n",res);
}
}
[/code]
来自为知笔记(Wiz)
相关文章推荐
- 专题四--1001
- 西工大空院电子信息工程本科主干课程回顾总结
- TCP和UDP通信
- csr8670--sink工程的大致工作流程分析(以speaker为例)二
- Web 数据实时收集入Hbase ,通过Hive 分析 (JS SDK)
- 微信获取code和openid
- 记一次开发:Qt简单电话本程序
- BZOJ1795 : [Ioi2008]Pyramid Base 金字塔地基
- HTML学习笔记(下)
- HTML学习笔记(下)
- kvm虚拟化安装配置
- Android网络框架源码分析一----Volley
- C程序的坎坷人生
- Ruff 硬件开发初体验
- mysql事件
- Java插入音频文件,导出jar包可播放
- javaScript如何准确地知道属于哪一类对象!!!
- 5.2.6 I/O核心子系统
- 物理学基石 —— 电
- csr8670--sink工程的大致工作流程分析(以speaker为例)一