练习四 1002
来源:互联网 发布:华3交换机端口激活命令 编辑:程序博客网 时间:2024/04/30 12:19
Problem B
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 58 Accepted Submission(s) : 18
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.<br>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?<br>
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. <br><br>Input contains multiple test cases. Process to the end of file.<br>
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. <br>
Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
Sample Output
3.41
题意:
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 58 Accepted Submission(s) : 18
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.<br>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?<br>
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. <br><br>Input contains multiple test cases. Process to the end of file.<br>
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. <br>
Sample Input
3 1.0 1.0 2.0 2.0 2.0 4.0
Sample Output
3.41
题意:
计算把所有的点连接起来的最小线段长度。
思路:
简单的最小生成树。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
int p[10010],n,k;
struct lok
{
double x,y;
}a[10010];
struct point
{
int s,e;
double w;
}b[10010];
bool cmp(point a,point b)
{
return a.w<b.w;
}
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
double kru()
{
double sum=0;
for(int i=0;i<k;i++)
{
int c=find(b[i].s),d=find(b[i].e);
if(c!=d)
{
p[c]=d;
sum+=b[i].w;
}
}
return sum;
}
int main()
{
while(cin>>n)
{
if(n==0) break;
for(int i=1;i<=n;i++)
cin>>a[i].x>>a[i].y;
k=0;
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
b[k].s=i;
b[k].e=j;
b[k].w=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
k++;
}
}
for(int i=0;i<k;i++)
p[i]=i;
sort(b,b+k,cmp);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<kru()<<endl;
//printf("%.2lf\n",kru());
}
return 0;
}
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
int p[10010],n,k;
struct lok
{
double x,y;
}a[10010];
struct point
{
int s,e;
double w;
}b[10010];
bool cmp(point a,point b)
{
return a.w<b.w;
}
int find(int x)
{
return p[x]==x?x:p[x]=find(p[x]);
}
double kru()
{
double sum=0;
for(int i=0;i<k;i++)
{
int c=find(b[i].s),d=find(b[i].e);
if(c!=d)
{
p[c]=d;
sum+=b[i].w;
}
}
return sum;
}
int main()
{
while(cin>>n)
{
if(n==0) break;
for(int i=1;i<=n;i++)
cin>>a[i].x>>a[i].y;
k=0;
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
b[k].s=i;
b[k].e=j;
b[k].w=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
k++;
}
}
for(int i=0;i<k;i++)
p[i]=i;
sort(b,b+k,cmp);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<kru()<<endl;
//printf("%.2lf\n",kru());
}
return 0;
}
0 0
- 练习四 1002
- 练习四 1002
- 练习四1002
- 练习四
- 2016sdau课程练习专题四 1002
- 数据库练习四
- 查询练习(四)
- Scala练习(四)
- 听课练习(四)
- 笔试练习四
- 笔试练习四
- 练习四1001
- 练习四 1001
- 练习四 1004
- 练习四 1003
- 练习四 1005
- 练习四 1001
- 练习四 1003
- angularjs 指令—— 绑定策略(@、=、&)
- JAVA 基础点滴1
- 为AS化化妆——AS主题下载网站及样式基本配置
- 归并算法理解
- 关于安卓启动模拟器时出现~~~~have you declared this activity in your AndroidMainfest.xml?问题
- 练习四 1002
- 深入了解scanf() getchar()和gets()等函数之间的区别
- 软件项目管理中团队成员组成结构及比例
- ConvertUtils.register注册转换器
- Java enum的用法详解
- iOS EXC_BAD_ACCESS 的处理方法
- ios学习路线—iOS高级(NSThread)
- mysql(一)
- CentOS关闭图形界面(x window)