您的位置:首页 > 其它

hdoj 1162 Eddy's picture 【最小生成树】

2015-02-09 22:52 316 查看

Eddy's picture

[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7397 Accepted Submission(s): 3753

[/b]

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



kruskal+prime

kruskal算法:
//kruskal
#include<stdio.h>
#include<math.h>
#include<algorithm>
#define max 100*50+10
using namespace std;
int set[110]; 
struct line
{
    int start;
    int end;
    double dis;
}num[max];
bool cmp(line a,line b)
{
    return a.dis<b.dis; 
}
int find(int p)
{
    int child=p;
    int t;
    while(p!=set[p])
    p=set[p];
    while(child!=p)
    {
        t=set[child];
        set[child]=p;
        child=t;
    }
    return p;
}
void merge(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    set[fx]=fy;
}
int main()
{
    double need,a[110],b[110];//a,b分别表示横 纵坐标   
    int point,road;
    int i,j,x,y,t;
    while(scanf("%d",&point)!=EOF)
    {
        for(i=1;i<=point;i++)
        {
            set[i]=i;
            scanf("%lf %lf",&a[i],&b[i]);
        }
        t=0;
        for(i=1;i<=point;i++)
        {
            for(j=i+1;j<=point;j++)
            {
                num[t].start=i;
                num[t].end=j;
                num[t].dis=sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]));
                t++;
            }
        }
        sort(num,num+t,cmp);
        need=0;
        for(i=0;i<t;i++)
        {
            if(find(num[i].start)!=find(num[i].end))
            {
                merge(num[i].start,num[i].end);
                need+=num[i].dis;
            }
        }
        printf("%.2lf\n",need);
    }
    return 0;
}


prime算法:

//prime
#include<stdio.h>
#include<math.h>
#define INF 0x3f3f3f
#define max 100+10
int visit[max];
double low[max],map[max][max];
int point;
void prime()
{
    int i,j,next;
    double min,mincost=0;
    for(i=1;i<=point;i++)
    {
        low[i]=map[1][i];
        visit[i]=0;
    }
    visit[1]=1;
    for(i=2;i<=point;i++)
    {
        min=INF;
        for(j=1;j<=point;j++)
        {
            if(!visit[j]&&min>low[j])
            {
                min=low[j];
                next=j;
            }
        }
        visit[next]=1;
        mincost+=min;
        for(j=1;j<=point;j++)
        {
            if(!visit[j]&&low[j]>map[next][j])
            {
                low[j]=map[next][j];
            }
        }
    }
    printf("%.2lf\n",mincost);
}
int main()
{
    int i,j;
    double a[max],b[max];
    while(scanf("%d",&point)!=EOF)
    {
        for(i=1;i<=point;i++)
        {
            for(j=1;j<=point;j++)
            {
                if(i==j)
                map[i][j]=0;
                else
                map[i][j]=INF;
            }
        }
        for(i=1;i<=point;i++)
        {
            scanf("%lf %lf",&a[i],&b[i]);
        }
        for(i=1;i<=point;i++)
        {
            for(j=i+1;j<=point;j++)
            {
                map[i][j]=map[j][i]=sqrt((a[i]-a[j])*(a[i]-a[j])+(b[i]-b[j])*(b[i]-b[j]));
            }
        }
        prime();
    } 
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: