Eddy's picture(prime+克鲁斯卡尔)

Eddy's picture

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 1
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
 类似与畅通工程里面的百岛湖那个题,刚开始没理解题意,写错了,最后又wa两遍,point数组没开大;
克鲁斯卡尔代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int map[110];
struct Point{
    int start,end;
    double len;
}point[10010];
int cmp(Point a,Point b){
    return a.len<b.len;
}
struct dot{
    double x,y;
}dot[110];
int find(int x){
    int r=x;
    while(r!=map[r])r=map[r];
    int i=x,j;
    while(i!=r)j=map[i],map[i]=r,i=j;
    return r;
}
double distant(double x1,double y1,double x2,double y2){
    double x=x2-x1,y=y2-y1,L;
    L=sqrt(x*x+y*y);
    return L;
}
int main(){
    int n,c;
    double distance;
    while(~scanf("%d",&n)){
    //    memset(map,0,sizeof(map));
    //    memset(point,0,sizeof(point));
        for(int i=1;i<=n;++i)map[i]=i;
        for(int i=1;i<=n;++i)scanf("%lf%lf",&dot[i].x,&dot[i].y);c=0;
        for(int i=1;i<=n;++i){
            for(int j=1;j<i;++j){point[c].start=j;point[c].end=i;
                point[c].len=distant(dot[j].x,dot[j].y,dot[i].x,dot[i].y);
                c++;
            }
        }
        sort(point,point+c,cmp);distance=0;
        for(int i=0;i<c;++i){
            int f1,f2;
            f1=find(point[i].start);f2=find(point[i].end);
            if(f1!=f2)map[f1]=f2,distance+=point[i].len;
        }
        printf("%.2lf\n",distance);
    }
    return 0;
}

 prime:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 const int INF=0x3f3f3f3f;
 5 const int MAXN=110;
 6 int n;
 7 double answer;
 8 double map[MAXN][MAXN],low[MAXN];
 9 int vis[MAXN];
10 double gd(double ax,double ay,double bx,double by){
11         double x=bx-ax,y=by-ay;
12         double s=x*x+y*y;
13         return sqrt(s);
14 }
15 void prime(){
16     int k;
17     double temp;
18     memset(vis,0,sizeof(vis));
19     vis[0]=1;
20     for(int i=0;i<n;i++)low[i]=map[0][i];
21     for(int i=0;i<n;i++){
22             temp=INF;
23         for(int j=0;j<n;j++)
24             if(!vis[j]&&temp>low[j])
25                     temp=low[k=j];//k=写错地方了,又错了半天
26     if(temp==INF){
27         printf("%.2lf\n",answer);
28         break;
29     }
30         answer+=temp;
31         vis[k]=1;
32         for(int j=0;j<n;j++)
33             if(!vis[j]&&low[j]>map[k][j])
34             low[j]=map[k][j];
35     }
36 }
37 int main(){
38     double dx[MAXN],dy[MAXN];
39         while(~scanf("%d",&n)){
40                 answer=0;
41                 for(int i=0;i<n;i++)
42                 for(int j=0;j<n;j++)
43                 map[i][j]=INF;
44             for(int i=0;i<n;i++)
45                 scanf("%lf%lf",&dx[i],&dy[i]);
46             for(int i=0;i<n;i++)
47             for(int j=i+1;j<n;j++){
48                     double dis=gd(dx[i],dy[i],dx[j],dy[j]);
49             //printf("%lf\n",dis);
50                 if(dis<map[i][j])
51                         map[i][j]=map[j][i]=dis;
52             }
53             prime();
54         }
55     return 0;
56 }

 

posted @ 2015-07-09 09:38  handsomecui  阅读(238)  评论(0编辑  收藏  举报