2016SDAU课程练习四1002 Problem B

Problem B

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 163 Accepted Submission(s) : 61
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
简单题意: Eddy想让所有朋友都看到自己的画,问题:现在纸上有一些点,这些点之间用直线连接,求可以将这些点连接起来的最短长度,所有的点最后连到相同的地方。 解题思路: 最小生成树问题,定义一个结构体,保存两点极其距离,然后排序​。若两点不在一条直线上则构造一个矩形,求直角三角形的斜边长,然后把所有距离排序,依次选择两点间距离最短的边相加
ac代码
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <cmath> #include <iomanip> using namespace std; struct point {     double x;     double y; }; double juli(double x, double y,double x1,double y1) {     return sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)); } int main() {     int t;     double x;     double y;     double a[105][105];     point b[106];     int i,j;     int pao[1000];     while(cin>>t)     {         memset(pao,0,sizeof(pao));       for (i=1;i<=t;i++)       {           cin>>b[i].x;           cin>>b[i].y;       }       for(i=1;i<=t;i++)         for(j=i;j<=t;j++)       {           a[i][j]=juli(b[i].x,b[i].y,b[j].x,b[j].y);           a[j][i]=a[i][j];       }       double max;       int k;       double sum=0;       for(i=2;i<=t;i++)       {           max=999999;           for(j=2;j<=t;j++)           {               if(pao[j]==0&&max-a[1][j]>1e-8)               {                  k=j;                  max=a[1][j];               }           }           pao[k]=1;           sum+=max;           for(j=2;j<=t;j++)           {               if(pao[j]==0&&a[k][j]-a[1][j]<1e-8)                 a[1][j]=a[k][j];           }       }       cout<<fixed<<setprecision(2)<<sum<<endl;       }       return 0;     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值