HDU 1162 Eddy's picture (最小生成树)

问题描述:
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

大致题意:
在二维坐标系中给出n个点。求让所有点连通的最小花费。

思路分析:
基本的最短路。
prim或Kruskal算法都能解决。

ac代码:

#include<bits/stdc++.h>
using namespace std;
const long MAXN=10000;
long father[MAXN];
long m,n;//点数 边数
//m标号从1开始
typedef struct
{
    long from;
    long to;
    double cost;
}Edge;

bool operator <(const Edge &a, const Edge &b)
{
    return a.cost>b.cost;
}
priority_queue<Edge> q;
Edge e[100005];
void MakeSet()
{
    long i;
    for (i=0;i<=m;++i)
    {
        father[i]=i;
    }
}
long Find(long i)
{
    long r=i;
    while (father[r]!=r)
    {
        r=father[r];
    }
    while (father[i]!=r)
    {
        long j=father[i];
        father[i]=r;
        i=j;
    }
    return r;
}
void Unition(long x,long y)
{
    long fx=Find(x);
    long fy=Find(y);
    if (fx!=fy)
    {
        father[fx]=fy;
    }
}
void Init()
{
    while (!q.empty())
    {
        q.pop();
    }
    MakeSet();
    Edge a;
    double x[105],y[105];
    int i,j,k;
    for(i=0;i<m;i++)
    {
        cin>>x[i]>>y[i];
    }
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            if(i==j)
                continue;
            a.from=i,a.to=j;
            a.cost=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
            q.push(a);
        }
    }
}
void print(double cost)
{
    printf("%.2f\n",cost);
}
void Kruskal()
{
    Init();
    long t=0;//表示合并次数
    double cost=0;
    Edge e;
    while (!q.empty()&&t<m-1)
    {
        e=q.top();
        long v1=e.from;
        long v2=e.to;
        if (Find(v1)!=Find(v2))
        {
            Unition(v1,v2);
            cost+=e.cost;
            ++t;
        }
        q.pop();
    }
    print(cost);
}

int main()
{
    while(~scanf("%ld",&m))//输入点与边
    {
        Kruskal();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值