HDOJ 1162 Realtime Status 这道题我写的lruskal算法代码该怎么优化?谢谢大牛们回答

DeDeWo 2011-09-01 07:21:18
下面是我的代码,在HDOJ里超时通不过。

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct{
double x,y;
int parent;
}point[105];
struct Edge{
int from,to;
double dis;
}edge[5100];
void init(int n){
for(int i=1;i<=n;i++)
point[i].parent=i;
}
int find(int x){
while(x!=point[x].parent)
x=point[x].parent;
return x;
}
void merge(int x,int y){
x=find(x);
y=find(y);
if(x!=y)
point[x].parent=y;
}
bool compare(Edge a,Edge b){
return a.dis<b.dis;
}
int main(){
int n;
double sum;
while(scanf("%d",&n)){
float a,b;
for(int i=1;i<=n;i++)
scanf("%lf%lf",&point[i].x,&point[i].y);
int p=1;
init(n);
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
edge[p].from=i;
edge[p].to=j;
edge[p].dis= sqrt( ((point[i].x-point[j].x)*(point[i].x-point[j].x))+((point[i].y - point[j].y)*(point[i].y - point[j].y)) );
p++;
}
}

sort(edge+1,edge+p,compare);

int m=n*(n-1)/2;
sum=0.0;
for(int i=1;i<=m&&n>1;i++){
int x=find(edge[i].from),y=find(edge[i].to);
if(x!=y){
merge(x,y);
n--;
sum+=edge[i].dis;
}
}
printf("%.2lf\n",sum);


}
return 0;
}

...全文
69 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolws123 2012-03-20
  • 打赏
  • 举报
回复
最小生成树问题?
DeDeWo 2011-09-03
  • 打赏
  • 举报
回复
分不够只能来这了,这道题我已经AC了
仙度瑞城 2011-09-02
  • 打赏
  • 举报
回复
额..这个怎么成非技术问题了..?
AAA20090987 2011-09-02
  • 打赏
  • 举报
回复
ACM题最好去算法区问吧
DeDeWo 2011-09-01
  • 打赏
  • 举报
回复
这是题目
Eddy's picture
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 17 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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

Author
eddy

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧