HDU 4463 Outlets 最小生成树Kr~

本文介绍了一个关于最小生成树的问题,特别关注两个指定节点(Nike店和Apple店)必须直接相连的情况。通过调整Prim算法来解决此问题,并提供了一个具体的实现示例。

这个题貌似是12年杭州亚洲赛的题==,题意是求一个最小生成树,但是要求有两个已知点必须直接连着==其实挺水的,prim不好实现  改了一晚上。。。还是不知道自己最开始为啥错了=。=

Description

In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup. 
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores. 
 

Input

There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0. 
 

Output

For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point. 
 

Sample Input

4 2 3 0 0 1 0 0 -1 1 -1 0
 

Sample Output

3.41
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=60*60;
struct point
{
    int x,y;
    double dis;
    point(){}
    point(int _x,int _y,double _d):x(_x),y(_y),dis(_d){}
    bool operator <(const struct point &tmp)const{
        return this->dis<tmp.dis;
    }
}p[maxn];
point pp[100];
int par[60];
void init(){
    for(int i=0;i<60;i++) par[i]=i;
}
int find_par(int x){
    if(x!=par[x]) return par[x]=find_par(par[x]);
    return par[x];
}
bool Union(int x,int y){
    x=find_par(x);
    y=find_par(y);
    if(x!=y){
        par[y]=x;
        return true;
    }
    return false;
}
double calu(point a,point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int n,p1,q1;
    while(~scanf("%d",&n)&&n)
    {
        init();
        scanf("%d%d",&p1,&q1);
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            int x,y;
            scanf("%d%d",&pp[i].x,&pp[i].y);
            for(int j=1;j<i;j++)
            {
                double d=calu(pp[i],pp[j]);
                p[cnt++]=point(i,j,d);
            }
        }
        double ans=0;
        Union(p1,q1);
        ans+=calu(pp[p1],pp[q1]);
        sort(p,p+cnt);
        for(int i=0;i<cnt;i++)
        {
            if(Union(p[i].x,p[i].y))
             ans+=p[i].dis;
        }
        printf("%.2lf\n",ans);
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值