Problem B
2016-07-05 11:45
239 查看
[align=left]Problem Description[/align]
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>
[align=left]Input[/align]
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>
[align=left]Output[/align]
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>
[align=left]Sample Input[/align]
3
1.0 1.0
2.0 2.0
2.0 4.0
[align=left]Sample Output[/align]
3.41代码:#include<stdio.h>
#include<math.h>
#define INF 99999999
#define MAX 101
double c[MAX][MAX];
double dis[MAX],visit[MAX];
typedef struct
{
double x;
double y;
}Point;
double distance(Point a,Point b)
{
double len;
len=sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
return len;
}
int main()
{
Point p[105];
int i,j,k,n;
double sum,l;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{
if(i==j)c[i][j]=0; //对a[][]进行初始化,一般都要;
else c[i][j]=INF;
}
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
l=distance(p[i],p[j]);
c[i][j]=c[j][i]=l;
}
for(i=1;i<=n;i++)
{
dis[i]=c[1][i];
visit[i]=0;
}
visit[1]=1;
sum=0;
for(i=1;i<n;i++)
{
double min=1.0*INF;
for(j=1;j<=n;j++)
if(visit[j]==0 && dis[j]<min)
{
min=dis[j];
k=j;
}
sum+=min;
visit[k]=1;
for(j=1;j<=n;j++)
if(visit[j]==0 && dis[j]>c[j][k])dis[j]=c[j][k];
}
printf("%.2lf\n",sum);
}
return 0;
}
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>
[align=left]Input[/align]
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>
[align=left]Output[/align]
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>
[align=left]Sample Input[/align]
3
1.0 1.0
2.0 2.0
2.0 4.0
[align=left]Sample Output[/align]
3.41代码:#include<stdio.h>
#include<math.h>
#define INF 99999999
#define MAX 101
double c[MAX][MAX];
double dis[MAX],visit[MAX];
typedef struct
{
double x;
double y;
}Point;
double distance(Point a,Point b)
{
double len;
len=sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
return len;
}
int main()
{
Point p[105];
int i,j,k,n;
double sum,l;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
{
if(i==j)c[i][j]=0; //对a[][]进行初始化,一般都要;
else c[i][j]=INF;
}
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{
l=distance(p[i],p[j]);
c[i][j]=c[j][i]=l;
}
for(i=1;i<=n;i++)
{
dis[i]=c[1][i];
visit[i]=0;
}
visit[1]=1;
sum=0;
for(i=1;i<n;i++)
{
double min=1.0*INF;
for(j=1;j<=n;j++)
if(visit[j]==0 && dis[j]<min)
{
min=dis[j];
k=j;
}
sum+=min;
visit[k]=1;
for(j=1;j<=n;j++)
if(visit[j]==0 && dis[j]>c[j][k])dis[j]=c[j][k];
}
printf("%.2lf\n",sum);
}
return 0;
}
相关文章推荐
- Java实现的公平锁
- nginx在linux安装笔记
- c++类型转换
- alloc、init你弄懂50%了吗?
- Https 公钥、私钥、证书
- googleMap本地化(离线)
- php 类中调用另一个类
- 无所不在,见缝插针,这就是营销!
- 多一分的信任和多一分的尊重
- ActiveMQ在C#中的应用示例分析
- Mac 下安装jmeter
- linux 模块编译
- 一段代码,先留着
- JBPM4.4工作流引擎学习(五)
- office文件密码破解方法及软件
- CSS中设置DIV垂直居中的N种方法 兼容IE浏览器
- VoLTE到底是个啥?用完真回不去了
- Android ANR 分析解决方法
- Problem A
- Java 中集合框架知识点的总结